• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection question

 
Tarun Mohandas
Ranch Hand
Posts: 33
Eclipse IDE Hibernate Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tarun Mohandas
Ranch Hand
Posts: 33
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are always welcome Tarun.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object
Untitled.jpg
[Thumbnail for Untitled.jpg]
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tarun Mohandas wrote:I came across this question when I was simply browsing through some questions online.


Please QuoteYourSources...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic