Showing posts with label Garbage Collection. Show all posts
Showing posts with label Garbage Collection. Show all posts

Monday, 13 February 2012

How many objects will be eligible for garbage collection after line 7?

public class TutorialGC
{

public static void main(String [] args)
{


Object a = new Integer(100);   // Line1
Object b = new Long(100);       // Line2
Object c = new String(“100″);  // Line3
a = null;                                        // Line4
a = c;                                             // Line5
c = b;                                             // Line6
b = a;                                             // Line7
// Rest of the code here


}


}

Choices:



  • A. 0

  • B. 1

  • C. 2

  • D. 3

  • E. Code does not compile


Correct choice:



  • B


Explanation:

Sunday, 13 November 2011

Sun Certified Java Programmer (SCJP)

How many objects will be eligible for garbage collection after line 7?


public class TutorialGC
{


public static void main(String [] args)
{


Object a = new Integer(100);   // Line1
Object b = new Long(100);       // Line2
Object c = new String("100");  // Line3
a = null;                                        // Line4
a = c;                                             // Line5
c = b;                                             // Line6
b = a;                                             // Line7
// Rest of the code here


}


}



Choices:



  • A. 0

  • B. 1

  • C. 2

  • D. 3

  • E. Code does not compile


Correct choice:



  • B


Explanation:

Sun Certified Java Programmer (SCJP)

Garbage collection


Finalization


Java technology allows you to use the finalize() method to do the necessary cleanup before the garbage collector removes the object from memory. This method is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. It is defined in the Object class, thus it is inherited by all classes. A subclass overrides the finalize() method to

Sun Certified Java Programmer (SCJP)

Garbage collection


Eligibility for garbage collection


An object is eligible for garbage collection when no live thread can access it.

An object can become eligible for garbage collection in different ways:

  • If the reference variable that refers to the object is set to null, the object becomes eligible for garbage collection, provided that no other reference is referring to it.

  •  If the reference variable that refers to the object is made to refer to some other object, the object becomes eligible

Sun Certified Java Programmer (SCJP)-10

Garbage collection


Behavior of the garbage collector


A Java programmer does not have to worry about memory management, because it is automatically taken care of by the garbage collector. The Java virtual machine (JVM) decides when to run the garbage collector. The garbage collector is a low priority thread that runs periodically, releasing memory used by objects that are not needed anymore.

The garbage collection (GC) algorithm varies from one JVM to another. There are different algorithms being used, like reference

Sunday, 23 October 2011

Can we force garbage collector to run ?

System.GC.Collect() forces garbage collector to run. This is not recommended but can be used if situations arises.

What is garbage collection?

Garbage collection is a CLR feature which automatically manages memory. Programmers forget to release the objects while coding ..... Laziness (Remember in VB6 where one of the good practices is to set object to nothing). CLR automatically releases objects when they are no longer in use and refernced. CLR runs on non-deterministic to see the unused objects and cleans them. One side effect of this non-deterministic feature is that we cannot assume an object is