This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes object garbage collection. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 1611
    
  10

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: 16483
    
    2

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://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: object garbage collection.
 
Similar Threads
assigning one primitve variable to another primitive variable..
Static
islands of isolation(Garbage Collection)
equlas()
Doubt in equals()