jmnemohistosyne
jmnemohistosyne copied to clipboard
wrong object sizes reported
simple test like this
MemoryHistogram diff = Histogramer.getDiff(() -> {
String aa = "text";
return aa;
});
HistogramEntry nodes = diff.get("String");
System.out.println(nodes.getInstances());
System.out.println(nodes.getSize());
returns random values, e.g.:
700
16800
The problem is that what you are printing contains strings created by classes and objects from the JVM.
If this code is executed only once, it will include all code related to class loading and compilation which creates also strings.
The utility takes a histogram of memory before and after your lamda. It doesn't instrument your code and doesn't know what is happening inside. Internally, the JVM does a lot of things to compile and execute your lambda, and concurrently does other things (GCs for example...)
You must "warm" the JVM executing inspected code before measuring it to ensure that all necessary objects have been created. I explain it better in this post: https://medium.com/@jerolba/measuring-actual-memory-consumption-in-java-jmnemohistosyne-5eed2c1edd65#b742
I'll add this explanation to the README.md
But I see another problem: if you execute these code repeatedly in the nth execution, the result will be 0 bytes because "text" is created as a constant by the java compiler and is instantiated outside your code (and reused every time you reference to "text" string).
To test correctly these code you must force the creation of a new String:
MemoryHistogram diff = Histogramer.getDiff(() -> {
String aa = new String("text");
return aa;
});