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 Garbage Collection?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection??" Watch "Garbage Collection??" New topic
Author

Garbage Collection??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Guys,

The below statement is from here

Misconception Number One: A Reference to Oneself

One big misconception is that an object that maintains a reference to itself will not protect itself from being junked by the garbage collector. Some programmers place a reference to the object as a member variable, hoping that it will count. Let me assure you that it does not. Remember that an object already has an implicit reference to itself via the

this

keyword. Unless an object is referenced by other objects, which are in turn referenced, it does not satisfy the rule.

Guys can anyone help me to realize the above statement through code?? In particular I'm not able to follow the line in bold...Anyone please??


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
One big misconception is that an object that maintains a reference to itself will not protect itself from being junked by the garbage collector.


I think that the not in this sentence should not be there.

Suppose you put your finger on your nose. That's not going to prevent you from being washed away if you fall into the river and are not tied to the riverside.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Barry, are they trying to say, an object which has reference to itself is Garbage Collected?? How can I realize this through code?? Can you please provide me with an example?
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Suppose you put your finger on your nose. That's not going to prevent you from being washed away if you fall into the river and are not tied to the riverside.


Interesting...


~Sanjeev Singh<br />SCJP 1.5
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
The class has a member of the class's type that points to itself:


The objects of Ego are the noses, the ego variable is the finger.

Look at this self-reference as the smallest possible island of isolation.

On my system it prints three times "ouch" showing the ego objects are finalized.
(results of call to System.gc are system dependent)


Yours,
Bu.
[ December 18, 2006: Message edited by: Burkhard Hassel ]

all events occur in real time
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Thanks Bu for the code realization of the statement that I posted.
 
 
subject: Garbage Collection??
 
Similar Threads
Garbage Collector doubt
Garbage collection
GC ... Explain Code....
garbage collection assigning null value
Tricky GC