Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Garbage Collection for objects created within a method.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I had a question regarding when an object that has been created locally within a method call will be garbage collected. I know we can't force GC, but is it safe to assume that usually a locally created object is up for collection as soon as you return from the method call?
 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can force GC.

yes you can safely assume, as soon as the object is out of scope. it is ready to be garbage collected. but don't assume that it will be done immediately. JVM is very clever. it takes care of everything.

Java Strength is not its language. Java's strength is JVM.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

As Ani said we can't force the garbage collector, even if we call: Using the function to call the GC does not assure that the objects are collected, It depends on the VM.

 
Sheriff
Posts: 22512
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ani pillai wrote:is it safe to assume that usually a locally created object is up for collection as soon as you return from the method call?


Unless you use a reference to it as the return value of the method, or store a reference in any other way.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Garbage collector can remove the objects which can no longer be accessed or referenced.
 
Bartender
Posts: 2900
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Belief is that
garbage collection is indeed a costly issue,

Just because objects need to be destroyed does not mean that should be done instantaneously

System.gc(); is merely a request not a command.

The JVM would probably wait [just guessing] for a batch operation rather than clearing memory after every statement block
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic