If you are using the runtime / totalMemory solution that has been posted in many answers here (I have done this a lot), first force two garbage collections if you want to get sufficiently accurate / consistent results.
For effiency, Java usually allows garbage to fill all the memory before forcing the GC, and even then it is usually not a full GC, so your results for runtime.freeMemory () are always somewhere between the "real" amount of free memory and 0.
The first GC doesn't get everything, it gets the most.
The climb is that if you just make a call to freeMemory (), you will get a number that is completely useless and varies a lot, but if it does 2 gc, then this is a very reliable sensor. It also makes the MUCH procedure slower (maybe seconds).
Bill K Sep 16 '08 at 17:22 2008-09-16 17:22
source share