aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Identifying Objects eligible to be  Garbaged  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Identifying Objects eligible to be  Garbaged  " Watch "Identifying Objects eligible to be  Garbaged  " New topic
Author

Identifying Objects eligible to be Garbaged

Nilesh Raje
Ranch Hand

Joined: Aug 02, 2005
Posts: 153
Hello,

Can any one tell me some formula :-) or any short and sweet method to identify the number of objects legible for garbage collection..!
Also please tell me how can we conclude that a particular object is legible for garbage collection.I have been through long notes and articles about this topic but dint understand it completly yet..! I tried attempting the question on this topic and 50% of question on identifying such objects in complex codes are wrongly worked out by me..! Please help me to come over my weakness in this topic and throw some light on this..!
Thanks a Ton in advance..!

Cheers..!


Thanks and Regards,<br />Nilesh<br />SCJP 1.4, SCWCD 1.4
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Can any one tell me some formula :-) or any short and sweet method to identify the number of objects legible for garbage collection..!
Also please tell me how can we conclude that a particular object is legible for garbage collection.
An object is eligible for garbage collection when it is inaccessible by any code. As far as quiz questions go, you can be safe in assuming that GC-eligible objects will include any object aside from String literals that don't have any references pointing to them.

I've found that actually drawing out pictures and arrows for references and objects can help me make the distinction.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Or if you think about objects being in this cloud called the Heap, and reference variables are in the waiting line outside the cloud pointing to their friend, the object in the Heap that they refer to. Now also see that the Objects in the Heap may point to another object in the heap. So if you follow the lines and see that it goes outside the cloud to something on the stack still pointing in that direction, then it is not eligible yet.

Onyl when you cannot trace it back to the stack is an object eligible for garbage collection.

String objects are the exception, and have other rules.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
Good question Nilesh, this is one of the slightly more tricky objectives. One of the terms used about garbage collection is the idea of an object becoming "unreachable". I have written about this at

http://www.jchq.net/certkey/0301certkey.htm

If you look at the bottom of that page you will see some links to some other useful resources on the subject.

Marcus


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
shyam ramineni
Ranch Hand

Joined: Jul 08, 2005
Posts: 43
Hi,

This is my understanding of how Strings are garbage Collected.

1. String literals will be eligible for garbage collection only after the main thread has exited.
2. where as String objects are eilgible for garbage collection , when are no references pointing to that object.

please confirm if the above statements are correct.

Thanks in advance
Shyam Ramineni


<a href="http://hyderabaditeducation.com" target="_blank" rel="nofollow">http://hyderabaditeducation.com</a>
Santana Iyer
Ranch Hand

Joined: Jun 13, 2005
Posts: 335
Shyam I think you are right.
String literals only after main complete or app ends so that OS reclaims
memory.

String object can be GC.

Nilesh read kathy's books if you already have read than I guess
drawing diagram and solving a bit simple qst than tough is good idea.

If you have not read kathy and bert's book, if u have HF java read that.

bye.
Devender Thareja
Ranch Hand

Joined: Jul 14, 2005
Posts: 187
Originally posted by shyam ramineni:
Hi,

This is my understanding of how Strings are garbage Collected.

1. String literals will be eligible for garbage collection only after the main thread has exited.
2. where as String objects are eilgible for garbage collection , when are no references pointing to that object.

please confirm if the above statements are correct.

Thanks in advance
Shyam Ramineni



My understanding is that on the heap there is no distinction between String literals and String Objects. Consider following:
String dkt= new String("dkt");
this code will cause generation of two String objects on heap. On the other hand:
String dkt= "dkt" will create one String object. But it's still an object.

My unserstanding is that there is same garbage collection rule for String objects, regardless of how they were created. (i.e. either by explisit call to new String or by just providing String litteral.)
That rule may differ between different JVM's.
Only thing to keep in mind is that in case of String objects, even if object s are eligible for garbase collection and garbasge collector kicks in, it may not collect String objects and they may be reused.
The reuse may happen when you assign a litteral to a variable.


Devender Thareja
SCEA, SCBCD, SCJP
Nilesh Raje
Ranch Hand

Joined: Aug 02, 2005
Posts: 153
Hi Friends,

Thanks Folks for your valuable inputs in helping me out..! Will try out your suggestions..! Appreciate your efforts..!

Thanks a Ton..!Have a wonderful day ahead..!

Cheerss.!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Identifying Objects eligible to be Garbaged