I am testing memory consumption of a block of code by using:
long totalMemory = Runtime.getRuntime().totalMemory();
long freeMemory = Runtime.getRuntime().freeMemory();
Used memory is the difference between totalMemory and freeMemory. I launched and run the same program three times with exactly same conditions. But the used memory amount differs a lot:
1120M
802M
312M
What might cause this? And should I take the average of the three as the memory usage of the code? First time doing this, and thanks for any insights on this.
jvisualvmorjconsole).