This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
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.
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.
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
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.
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
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Identifying Objects eligible to be Garbaged