I'm running an IO intensive app , and when I run the jvisualvm profiler , I see that the NumberAwareComparator occupies 33% of the memory and 55% of all objects!
Is it normal? Why would it do that? Where can I start looking to solve this?
/* later edition - code sample*/
//Using GPars
(0..length - 1).eachParallel { i ->
def final h = [:]
distHash[i] = h
println("Calculating distances ")
if (i + 1 < length - 1) {
((i + 1)..length - 1).each {j ->
def d = distanceCalc.getDistance(paragraphs[i], paragraphs[j])
if (d < 0.25) {
h[j] = d
}
}
}
}
Thanks!
I think I have some explanation , but I'm still not sure about the mechnisem. I had a function that did some basic numeric calculations , and when it ran , I had numerous instances of the NumberAwareComparator :
(0..length - 1).eachParallel { i ->
def final h = [:]
distHash[i] = h
println("Calculating distances ")
if (i + 1 < length - 1) {
(开发者_高级运维(i + 1)..length - 1).each {j ->
def d = distanceCalc.getDistance(paragraphs[i], paragraphs[j])
if (d < 0.25) {
h[j] = d
}
}
}
}
Once I've declared the type for each variable (as follows) , all the comparators were gone , and were replaced with floats/ints
(0..length - 1).eachParallel { i ->
final LinkedHashMap<Integer, Float> h = new LinkedHashMap<Integer, Float>()
distHash[i] = h
println("Calculating distances ")
if (i + 1 < length - 1) {
((i + 1)..length - 1).each {j ->
float d = distanceCalc.getDistance(paragraphs[i], paragraphs[j])
if (d < 0.25) {
h[j] = d
}
}
}
}
It looks like this class is probably stateless (but you should confirm this by looking at the source code). If so, you should be able to safely replace all those instances with a single instance.
If this is a Grails app you could do it using something like the following in Bootstrap.groovy
def singleton = new NumberAwareComparator()
NumberAwareComparator.metaClass.constructor << { ->
singleton
}
Alternatively you could put the code above in the doWithDynamicMethods
closure of a Grails plugin.
If this is not a Grails app, you could use the DelegatingMetaClass
精彩评论