• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object garbage collection.

 
jazy smith
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Now, once I allocated obj2 to obj1, there is no use of obj2 anymore in memory. Then don't you think it should get garbage collected ? If my fundamental is not clear, please correct me.
 
jazy smith
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

The above code, after gc(), still prints the value of obj2. Now, once I allocated obj2 to obj1, there is no use of obj2 anymore in memory. Then don't you think it should get garbage collected ? If my fundamental is not clear, please correct me.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Incorrect.

There are two references to obj2 now. One named obj1 and one named obj2.

The original obj1 is garbage collected.
 
jazy smith
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
once class gets loaded into memory, it will return the starting address of the memory where it resides. this starting address is what an object of the class holds. am I correct ?
 
Paul Clapham
Sheriff
Posts: 21111
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jazy smith wrote:once class gets loaded into memory, it will return the starting address of the memory where it resides. this starting address is what an object of the class holds. am I correct ?

No, loading a class into memory returns a Class object, if you do it with a ClassLoader's loadClass() method. However in regular programming you won't ever do that, so loading a class into memory just results in a class being in memory.

And no, an object of the class doesn't hold the address of that class. Internally it probably holds a reference to the class, but you won't ever see that in ordinary programming. What it really holds, from the programmer's point of view, is all static members of the class.

You seem to be focusing on all of the wrong things and thereby making up a completely inaccurate mental model of how things work. In particular "addresses of memory" is a useless concept for understanding Java. It's true that internally things know the addresses of other things, but for Java programming we don't care about that. Instead objects can contain references to other objects. Those may look like memory addresses but they might be more complicated than that; but again, we don't care about that.
 
jazy smith
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

you are absolutely correct. I think I am unnecessarily going into deep. I really appreciate you gave pretty perfect explaination. thanks a ton
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling System.gc() doesn't necessarily force GC, either.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic