Prateek Rawal wrote:
Thus it is sure that gc() is running everytime.....
Vinayagar Karpagam wrote:In theory, the free memory can go down after a GC; a very common example being the presence of another thread that creates Objects in parallel with the thread that invoked the GC. In my opinion there is no final answer to the question. Only thing that can be said is that you cant conclude that GC ran and freed memory by just checking that the free memory goes up after GC.
Prateek Rawal wrote:From the output it is clear that
The free memory(after the objects have been created) and before GC ran is less than the free memory after GC ran(and also possess exact values), thus some memory is being freed. Thus it is sure that gc() is running everytime.....
Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
The reason that it is not guaranteed, is because there are cases where it can't run, and the code just doesn't decide arbitrary to not run. It will try it best to perform the gc() on request. It may not run because there is already a gc in progress (obviously, the parallel new gen gc). Or it may not run because you configured it (via a -XX switch) to not run.
In this example, there isn't enough allocation to trigger a gc (running out of new heap), nor is the -XX flag set, so the gc should be honored every time.
when we run the program next time, why the following values is same as before
Total free memory before GC(even though last time the free memory increased,and when again we run,it comes down to the same value,why so?)
Prateek Rawal wrote:
Since the free memory after gc() is invoked increased, can we conclude that Garbade Collection happenned? and if not, what reason would you give of the increase in free memory?
and is it happening that every time we run the program a fixed number of objects are created(thus a fixed memory is used) thus the free memory comes out to be same,is it the case?