This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • 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 wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Important Question regarding Garbage Collection

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is: Why is there no Garbage Collection after
b1 = null; ???

Result: Compiles and runs with:
C:\Java\EigeneJavaProgramme>java ObjectGarbageCollectionTest1b
Object1 Object2
Object2 Object2

WHY ist there now a Garbage Collection after
b1= null??

public class ObjectGarbageCollectionTest1b {
int i;
ObjectGarbageCollectionTest1b(int i) {this.i=i;} //Constructor
// String toString() {return "Object"+ i;}
public String toString() {return "Object"+ i;}

public static void main (String[] argv) {
ObjectGarbageCollectionTest1b b1 = new ObjectGarbageCollectionTest1b(8);
ObjectGarbageCollectionTest1b b2 = new ObjectGarbageCollectionTest1b(2);
b1 = null;
// b1 = b2;
System.out.println("Object1 "+b1.toString());
System.out.println("Object2 "+b2.toString());
}
}
Result: Compilation Error:
C:\Java\EigeneJavaProgramme>java ObjectGarbageCollectionTest1b
java.lang.NullPointerException
at ObjectGarbageCollectionTest1b.main(ObjectGarbageCollectionTest1b.java:14)
Exception in thread "main"
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First there is no way to force garbage collection and this is the most important thing to know about Java garbage collection.
The only way to see when your object is being garbage collected is to override the finalize() method and put a trace there. The following thread provides a solution:
https://coderanch.com/t/238828/java-programmer-SCJP/certification/GC
[ August 21, 2002: Message edited by: Valentin Crettaz ]
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tom:
Important thing to note here is that garbage
collection is about objects (such as Object8 and Object2
in your example) not about references (b1 and b2).
Initially, b1 was referencining Object8, then null, then Object2. In the end, b1 and b2
both referenced Object2. The object Object8 is not
being referenced by anything in the end and there
fore it is candidate for GC. It is possible that it is
GCed by the time program was printing. The
outcome of the print statements do not prove (
disprove) that Object8 is GCed.
Thanks
Barkat
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to know what the gc is doing just run the java interpreter with the -verbose:gc switch.
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a good link that discusses garbage collection.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic