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

Object being garbage Collected by programmer or by JVM

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Please tell me who makes Garbage Collection .

IF i have a Object for example a Connection Object in JDBC with instance name as conn.

IF i say conn==null.


with this line conn==null

is the programmer making the Garbage collection ??

Or this will help JVM to make the conn Object Garbage Collected ??

share your knowledge .

Thanks in advance .
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Removing all references to an object (setting those variables to null) will make that object eligible, but there's no guarantee it will be collected while the program is running.

You can't force or guarantee garbage collection. (Note that calling System.gc() only "suggests that the Java Virtual Machine expend effort toward recycling unused objects.")
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you say conn=null, then its an indication to JVM is that, the object which conn is pointing is eligible for garbage collection(provided there no other reference to that object)
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects are garbage collected when no live thread can access then. One way of achieving that is by nulling all the references to that object.
But that doesn't work always:



There are still references to the Test objects however no live thread can access them. The JVM recognizes these Islands of Objects and GC'd them.
Run:

wouter@wouter-laptop:~/temp$ javac Test.java
wouter@wouter-laptop:~/temp$ java -verbose:gc Test
Start
[GC 232K->120K(32256K), 0.0009260 secs]
[Full GC 120K->103K(32256K), 0.0037630 secs]
End

 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By making reference to null, you are telling JVM that i do not need this object anymore and you may garbage collect it at your own convenient time unless the no more reference is given to this object else where(this includes finalized method).
 
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

Wouter Oet wrote:Objects are garbage collected when no live thread can access then.


Well, not necessarily--that's when they're *eligible*, but not necessarily when they're actually collected.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all , for sharing knowledge.
 
machines help you to do more, but experience less. Experience this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic