wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes object 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 » Java » Beginning Java
Bookmark "object garbage collection." Watch "object garbage collection." New topic
Author

object garbage collection.

jazy smith
Ranch Hand

Joined: Nov 18, 2009
Posts: 101
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

Joined: Nov 18, 2009
Posts: 101
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

Joined: Sep 14, 2009
Posts: 1659
    
  11

Incorrect.

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

The original obj1 is garbage collected.


When you do things right, people won't be sure you've done anything at all.
jazy smith
Ranch Hand

Joined: Nov 18, 2009
Posts: 101
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Nov 18, 2009
Posts: 101
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

Joined: Sep 29, 2008
Posts: 12617

Calling System.gc() doesn't necessarily force GC, either.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: object garbage collection.
 
Similar Threads
assigning one primitve variable to another primitive variable..
equlas()
islands of isolation(Garbage Collection)
Static
Doubt in equals()