• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

garbage collection

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic