File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About Garbage Collection" Watch "About Garbage Collection" New topic
Author

About Garbage Collection

Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

I manage to get it wrong almost always in the mocks. I have read this from mughal and Marcus Green tutorial. But I am unable to solve questions based on this topic.
Can somebody guide me how to approach this topic and questions based on it confidently?
TIA,
- Manish
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
General guide lines:
G.C. cannot be forced only requested.
The G.C. doens�t prevent an OutOfMemoryError
.
The g.c. of an object is not guaranted.
The finalize method of an object will be only called (automatically) if the g.c. is running. It is not guaranted its execution though. However it is guaranted that if executed, it will be executed only once for an object, just before its memory is reclaimed. Also it is guaranted that if an object is recycled its finalized method has already been executed.
An object is eligable for g.c. if it is unreachable by all the reachable objects. This means that A is eligable for g.c. if there aren't any references to it, except from, for instance, from B object which is also unreachable.
Be careful with aliasing the objects. If two or more variables points to the same object, typical mock exams try to confuse by only seeting to null, or copying new values to them, to only some of the variables.
Just keep posting the Ranch the questions you find difficult in the mocks
[ March 17, 2002: Message edited by: Jose Botella ]
[ March 17, 2002: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Macon Pegram
Greenhorn

Joined: Mar 01, 2002
Posts: 12
There is a MOUNTAIN of questions and answers on this very board about GC. Please try searching and reading those. I did quick look and over the last 3 days alone I found the following GC related posts.
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=015170
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=015218
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=015172
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=015183
Macon
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

Thanks a lot, both of you
Also, I found this approach very useful -
1)Draw the object and references pointing to it on paper
2) Modify the references as per the code and examine if the object still has any valid ref., if not it *is* eligible for GC.
I managed to get both the GC questions correct in the last mock using this approach. Just thought I'll share it.
Regards,
- Manish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Garbage Collection