• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Garbage Collection

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;
public class Q9Ch4
{

public static void prt(String s)
{
System.out.println(s);
}


Q9Ch4()
{
//this.prt("using this"); //it display 2nd
}




public static void main(String[] args)
{
prt("hello 2"); //first this display

Q9Ch4 a=new Q9Ch4(); //constructor call

Q9Ch4 b=new Q9Ch4();

Q9Ch4 c=new Q9Ch4();

Q9Ch4 d=new Q9Ch4();

Q9Ch4 e=new Q9Ch4();

Q9Ch4 f=new Q9Ch4();

System.gc();

System.runFinalization();


}

}

the code runs well but the programmer dont know when the objects are garbage collected. And for runFinalization method is it necessary that we should have our finalization method? can we explicitly do Garbage collection or not. we should know how many objects are garage collected
------------------
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai farrukh,
The system garbage collects the objects on a periodic basics.u can explicitly garabage collect them by assigning null to the object
Praveen
 
farrukh mahmud
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i my question is can we know when garbage collector of java doing his work. First object garbage then second..... How reference counting scheme works here?

------------------
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by praveen ankireddy:
The system garbage collects the objects on a periodic basics.


Not exactly. About the only guarantee you get is that the system will try to garbage collect before giving an out of memory error. Almost everything else is implementation detail that may vary from one JVM to the next. You are discouraged from making any assumptions.

u can explicitly garabage collect them by assigning null to the object


There is no way to explicitly garbage collect an object. Assigning null only makes an object eligible for garbage collection (if you don't have a hard reference to the object anywhere else, that is). The garbage collector keeps track of your references to figure out when an object can be garbage collected.
Whether that garbage collection will happen sooner or later, or indeed whether it will happen at all, is up to the JVM.
As I understand it, gc's use more sophisticated algorithms than just a simple reference count. After all, if you count references, a circular reference (object A refers to B, B refers back to A) would prevent garbage collection.
- Peter

[This message has been edited by Peter den Haan (edited February 19, 2001).]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition each Vendors JVMs works differently. Sun publishes the specifications for the JVM, and each browser vendor has to code their own JVM, following those rules. Exactly HOW the vendor chooses to implement garbage collection is left up to them. All that you know is that it must do it.
The fact that you do NOT need to worry about memory leaks from un collected objects is one of Java's strengths compared to C.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to Peter's answer, you should always assign null to object refernces that you dont need any more. Otherwise you are allowing memory leaks by not letting these locaitons eligible for garbage collection.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!