Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

gc on exam

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having a problem determining what the gc questions on the exam are looking for.
When is an object available for gc?
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
An Object becomes eligible for Garbage collection when the reference to a particular object is lost.
Here are the points to remember.
1.You can make an Object eligible for garbage collection.
2.You cannot force garbage collection.
3.The finalize() method is guaranteed to be called only once on any object before being garbage collected.
Signature of finalize() method is:
protected void finalize() throws Throwable {}.
4.Since finalize() method belongs to java.lang.Object, any Object in Java can be garbage collected. ( including Threads )
Example:
public class GarbageCollection
{
public static void main (String[] args)
{
String s1 = "OracleDB"; // 1
String s2 = "ASP"; // 2
String s1 = new String("JDBC"); // 3
String s2 = new String("JSP"); // 4
String s2 = null; // 5
}
}

Here at line 3, s1 is made to refer to "JDBC" and hence the original reference to "OracleDB" is lost. Hence,after execution of line 3,the string object "OracleDB" becomes "eligible" for garbage collection.
Similarly at line 4, s2 is made to refer to "JSP" and hence the original reference to the sting object "ASP" is lost.
So after line 4 , the string object "ASP" becomes "eligible" for garbage collection.
At line 5, s2 is set to null and hence after execution of line 5, the string object "JSP" becomes "eligible" for garbage collection.
Hence in the above example, you can say that " 3 objects namely, "OracleDB", "ASP" and "JSP" are eligible for garbage collection".
- Suresh Selvaraj
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a question, when you say line 3, s1 points to JDBC now and loses the reference to Oracle, then Oracle is eligible for gc, but since Oracle is String literal and is in the pool, are you sure we are really gc it or we gc it when the whole program terminates (or maybe i should say when it's out of scope)?
 
Suresh Selvaraj
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
After execution of line 3, the String Object "OracleDB" will become an " eligible " member for garbage collection.
The important point to remember here is that, after line 3 "OracleDB" "is NOT immediately" garbage collected.
Also there is no guarantee that "OracleDB" will be garbage collected when the program ends. There is every chance that Garbage Collection may not take place even after the program ends.
The only thing that is guaranteed is, finalize() method will be invoked once on objects "eligible" for garbage collection.
- Suresh Selvaraj
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic