Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi All,
I've just read about Java garbage collector and have some question associated with it :
What mean these quotation :
"Java’s garbage-collection mechanism may recover only memory that is definitely
unused. It is not possible to force garbage collection reliably. It is not possible 
to predict when a piece of unused memory will be collected, only to say when it 
becomes eligible for collection.  Garbage collection does not prevent memory leaks; 
they can still occur if unused references are not cleared to null or destroyed."
Posted
Updated 15-Jun-11 4:02am
v3
Comments
Sergey Alexandrovich Kryukov 6-Jun-11 17:35pm    
This is not a question. Yes, these statements are correct, so what's the problem?
--SA
Nagy Vilmos 15-Jun-11 10:03am    
RE-quoted question and it makes a little more sense.

 
Share this answer
 
Garbage collection can be tuned on the JVM - see this article[^] - rather then in code. You can call collection from code but it is not the best approach as the JVM is, at the end of everything, the final arbiter of what is garbage.

To answer the individual points:

arsenk02 wrote:
Java’s garbage-collection mechanism may recover only memory that is definitely unused.

When an object can no longer be accessed it becomes 'collectable'. In this it means accessible from a live thread. This is important as it allows for cyclicle objects to be collected, see the following:
Java
Foo a = new Foo();
Foo b = new Foo();
a.setBar(b);
b.setBar(a);
// now (a) references (b) and (b) references (a)
a = null;
b = null;
// the objects, though still referenced cannot be reached so are good to go


arsenk02 wrote:
It is not possible to force garbage collection reliably.

Collection is specific to the JVM. There are some rules about what can be collected, but not how.

arsenk02 wrote:
It is not possible to predict when a piece of unused memory will be collected, only to say when it becomes eligible for collection.

Again, the programmer does not know which JVM will be running and so you cannot say precisely how or when collection will take place. In most JVM's the collection is done on a separate GC thread that tries to reduce the impact on the runtime making a compromise between the frequency of collection and the amount of available memory. If you allocate a lot of memory to your JVM, it won't bother collecting too often as it won't need to.


arsenk02 wrote:
Garbage collection does not prevent memory leaks; they can still occur if unused references are not cleared to null or destroyed.

If you have a reference to an object that is still accessible it will not be collected, it can't be. Soo if youare in the habit of creating lots functional objects that get held until they finish, but never actually complete, you'll run into memory leak.

My advice is to read the articles you've been pointed to and if you are still concerned, put a monitor on your app, such as jconsole[^] and watch where the memory is going to.
 
Share this answer
 
v2
there is some alogorithm which is used by JVM to clean unused memory.

see this link.


http://www.artima.com/insidejvm/ed2/gcP.html[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900