• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

object garbage collection.

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Calling System.gc() doesn't necessarily force GC, either.
 
reply
    Bookmark Topic Watch Topic
  • New Topic