If you change the code so that it calculates and prints out the amount of memory used inside the loop after the gc() call, do the results make sense? [ September 20, 2006: Message edited by: Ryan McGuire ]
I suppose totalMemory() minus freeMemory() will give approximately the amount of memory in use by objects on the heap. When a full GC runs, all objects on the heap that are no longer referenced get freed. That will mean freeMemory() increases, so the difference reduces, like you saw.
Note first that calling gc() doesn't guarantee to do a full GC, or indeed anything, even if you call it hundreds of times (what's the idea of that anyway?). Second, note that, depending on JVM settings, the totalMemory() may change, as the JVM adapts the heap size to the program's requirements. [ September 21, 2006: Message edited by: Peter Chase ]
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Indeed. And never forget that a JVM, even one running a very simple user program like this, is a multithreaded process. Other stuff is going on in the background, and that other stuff may very well be allocating (and disposing) memory.