• Post Reply Bookmark Topic Watch Topic
  • New Topic

object creation and gargage collection  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i create a object( say a hashmap object) , is that object lifecycle limited to the method call or is this object available for garbage collection.
another doubt which springs into my mind...what all objects are really eligible for garbage collection?
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object is eligible for garbage collection if there are no references to it. He is called unreachable.
Read this article
http://java.sun.com/developer/technicalArticles/ALT/RefObj/
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what all objects are really eligible for garbage collection?


First, When you try call System.gc() in your code, it's force Garbage Collection. (This solution have in all Application Server such as WebLogic).

Second, When some object not have referenced or object is null.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.gc() does not force garbage collection.

I'd have thought there were many articles out there that make this fact clear so as to prevent misleading. i.e. I believe I am stating the obvious.
 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

System.gc() does not force garbage collection.


It depends on JVM to force garbage collection.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It NEVER forces garbage collection.
I'd have thought this myth would be dispelled by now?
Clearly, it is still floating around
[ October 22, 2004: Message edited by: Tony Morris ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The javadoc says this about System.gc():

gc

public static void gc()

Runs the garbage collector.

Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

To me, it seems that all objects are removed as far as possible by the JVM. If you are complaining that this is not so, maybe you have some references that are accessible for the JVM, but have become invisble to you. ;-)
Daniel
SCJP 1.4
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

To me, it seems that all objects are removed as far as possible by the JVM.


You missed the word 'suggests'.
In practice, it very seldom works.
Also in practice, calling it is almost always a bad idea.

I can't believe I'm saying this again! I've seen this thread of communication hundreds of times!
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a link to an article regarding garbage collection. Maybe it will help answer some of your questions.

One thing to keep in mind - when it comes to garbage collection, there are virtually no guarantees. You never know when or if garbage collection will take place. You can "suggest" that it take place, but you can't force it to.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!