my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Garbage collection question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Garbage collection question" Watch "Garbage collection question" New topic
Author

Garbage collection question

Tarun Mohandas
Ranch Hand

Joined: Feb 12, 2013
Posts: 33

I came across this question when I was simply browsing through some questions online.



After which line does the object created in line 5 becomes available for Garbage collection? I answered Line 9 but the answer was given line 8. How is it line 8? In line 7 a new object is assigned for o so A(null) doesnt affect o at all. So only after dereferencing o at line 9, it will become available for garbage collection right? If the answer is Line 8 can anyone tell me why?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

A(whatever) can never affect o at all anyway, because Java is only pass-by-value. That is, when a local variable is passed to a method, any changes the method makes to that variable are not seen by the caller. Each has its own independent copy of the variable. (If it's a reference variable though, then both copies point to the same object, so changing the state (contents) of that object through the variable can be seen by both method and caller.)

Also, your comment suggests that you think objects can be null. This is not the case. Only references can ever be null.

6: copy the reference in variable o, pass it to A()
6.1 A() copies the reference value from its obj parameter into member variable o
6.2 Now local variable o in B() and member variable o both point to the Object from line 5.

7. Local variable o gets a new reference value copied into it, pointing to the object that is created on that line.
7.1 Now there is only one reference to the object from 5--the member variable 0

8. pass null to A().
8.1 A() sets the reference value in member variable o to null. (Note that this does not change the contents of any object in any way.)
8.2 There are no no references point to the object from 5, so it is eligible for GC.
Tarun Mohandas
Ranch Hand

Joined: Feb 12, 2013
Posts: 33

Sorry I made a silly mistake of considering o for garbage collection when we have to consider the object created for GC. So is it safe to say that the object created at line 7 is available for GC after line 9 since local variable o is referenced to null and the object created at line 7 is dereferenced?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Tarun Mohandas wrote:So is it safe to say that the object created at line 7 is available for GC after line 9 since local variable o is referenced to null and the object created at line 7 is dereferenced?


Yes, the line 7 object is GC-able after line 9, because the only variable that ever pointed to it (local o) has been given a different value (null). That's not what "dereference" means though. To dereference a variable or reference or pointer means to access the thing that it is pointing to. So, for instance, when we do someVar.someField, the dot operator dereferences the someVar variable (or the reference value it contains).
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Tarun

I would suggest you take a piece of paper and make figures of objects and their respective references. It makes the picture much more clear. I also used to get confused in the beginning when dealing with such questions. Always better to draw the figures and then go about answering. This is a simple scenario of calculating number of objects available for GC. The waters get murkier when String literals and objects come into picture.


~ Mansukh
Tarun Mohandas
Ranch Hand

Joined: Feb 12, 2013
Posts: 33

Got it! Thanks Jeff Verdegan

Mansukhdeep Thind wrote:Tarun

I would suggest you take a piece of paper and make figures of objects and their respective references. It makes the picture much more clear. I also used to get confused in the beginning when dealing with such questions. Always better to draw the figures and then go about answering. This is a simple scenario of calculating number of objects available for GC. The waters get murkier when String literals and objects come into picture.


Yea I started doing that. You're right, makes it easy and simple. Thanks The trick is to just separate local variables (in stack) to objects and instance variables (in heap).

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

You are always welcome Tarun.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38483
    
  23
Tarun Mohandas wrote: . . . The trick is to just separate local variables (in stack) to objects and instance variables (in heap).
The sooner you get used to using pencil and paper the better; that was a good suggestion. But you do not need to know which variable lives on the heap and which on the stack to be able to answer such questions correctly.
sourav jain
Greenhorn

Joined: Nov 19, 2012
Posts: 22
HI READ YOUR SERIOUSLY , AND I FIND SOLOUTION , PLEASE CHECK ATTACHMENT,

I MADE PICTURE HOW LINE 8 WILL CAUSE FOR GARBAGE COLLECTION
sourav jain
Greenhorn

Joined: Nov 19, 2012
Posts: 22
Object


[Thumbnail for Untitled.jpg]

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

I can't understand what that drawing is supposed to represent. Also, please don't write in all caps. It's like shouting.
Tarun Mohandas
Ranch Hand

Joined: Feb 12, 2013
Posts: 33

Campbell Ritchie wrote:The sooner you get used to using pencil and paper the better; that was a good suggestion. But you do not need to know which variable lives on the heap and which on the stack to be able to answer such questions correctly.


The data structures are not necessary but it is helpful if I am able to distinguish the storage of everything to understand this concept
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40

Tarun Mohandas wrote:I came across this question when I was simply browsing through some questions online.


Please QuoteYourSources...


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection question