• Post Reply Bookmark Topic Watch Topic
  • New Topic

garbage collection  RSS feed

 
Arun Prasath
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object is created within a method, and after(execution) coming out of local method,, whether that object will be garbage collected or not?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects can be garbage collected only if there are no references pointing to the object in any live threads of the program anymore.

It doesn't have anything to do with where the object is created - in a method, constructor, static or instance initializer block etc.

If you write a method with a local variable that refers to an object, and the method ends, then the variable also disappears. If that variable was the only reference to the object, then yes, the object is ready for garbage collection.

Note that there are no guarantees about when exactly objects will be garbage collected. It could be right at the moment when an object becomes ready for garbage collection, or much later, or even not at all.
[ August 31, 2006: Message edited by: Jesper Young ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper Young:
Note that there are no guarantees about when exactly objects will be garbage collected. It could be right at the moment when an object becomes ready for garbage collection, or much later, or even not at all.


Well, it is guaranteed to garbage-collect objects before throwing OutOfMemoryError, isn't it?
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Thats true, that Object will targeted for GC when there is no reference from any live thread and it does not matter where that object has been created.

Assume following case :


public void k()
{
String s="HellO";

<< Do SOme operation >> ;

}

After this method, s will be targeted for GC. as there is no reference for that one.

Next:

public String k()
{
String k="Hello";

<do Some Stuff> ( dont make k null ... ).

return k;
}

Then k will not be targeted for GC. because there is a reference pointing from calling method.

2) Object will be targeted to GC when it sets to null too and other objects are not referencing.

And there is no clear cut out that when GC is invoked. One can invoke the GC from program in two ways,

1) System.gc();

2) Runtime.getRuntime().gc();

Both will throw checked exceptions.

Try experimenting with GC, will get more insight.

All the best,
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
Well, it is guaranteed to garbage-collect objects before throwing OutOfMemoryError, isn't it?


No. A java application can run out of memory. The garbage collection system attempts to remove objects from the memory when they are not used. If you have too many live objects, the system can run out of memory.

Garbage collection can't ensure there is enough memory available for your application.

Naseem
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!