Program explain concept like:
2) If any one referenced remain memory will not get cleaned.
1) How after completion of static method the object created inside the block becomes un-referenced in Stack area.
2) If any one referenced remain memory will not get cleaned.
Code:
Output:
Code:
package Atharva;
public class Garbage
{
static int a = 0;
@Override
public void finalize()
{
System.out.println((++a)+" Memory Cleaning By: "+Thread.currentThread().getName());
}
static void m1()
{
Garbage obj2 = new Garbage();
Garbage obj3 = new Garbage();
}
static Garbage m2()
{
Garbage obj4= new Garbage();
Garbage obj5 = new Garbage();
return obj5;
}
static Garbage m3()
{
Garbage obj6 = new Garbage();
Garbage obj7 = new Garbage();
return obj7;
}
public static void main( String args[] ) throws InterruptedException
{
Garbage obj1 = new Garbage(); // Used Memory / Referenced Object Creation / Named Memory
new Garbage(); // UnUsed Objecy / UnReferenced / Anonymous memory
obj1 = null;
m1();
m2();
Garbage obj8 = m3();
obj8 = null;
System.gc();
Thread.sleep(1000);
System.out.println(" Program Ends ");
}
}
public class Garbage
{
static int a = 0;
@Override
public void finalize()
{
System.out.println((++a)+" Memory Cleaning By: "+Thread.currentThread().getName());
}
static void m1()
{
Garbage obj2 = new Garbage();
Garbage obj3 = new Garbage();
}
static Garbage m2()
{
Garbage obj4= new Garbage();
Garbage obj5 = new Garbage();
return obj5;
}
static Garbage m3()
{
Garbage obj6 = new Garbage();
Garbage obj7 = new Garbage();
return obj7;
}
public static void main( String args[] ) throws InterruptedException
{
Garbage obj1 = new Garbage(); // Used Memory / Referenced Object Creation / Named Memory
new Garbage(); // UnUsed Objecy / UnReferenced / Anonymous memory
obj1 = null;
m1();
m2();
Garbage obj8 = m3();
obj8 = null;
System.gc();
Thread.sleep(1000);
System.out.println(" Program Ends ");
}
}
Output:
Code:
package Atharva;
public class Garbage
{
static int a = 0;
@Override
public void finalize()
{
System.out.println((++a)+" Memory Cleaning By: "+Thread.currentThread().getName());
}
Garbage obj;
public static void main( String args[] ) throws InterruptedException
{
Garbage obj1 = new Garbage(); // Used Memory
Garbage obj2 = new Garbage(); // Referenced Object Creation
Garbage obj3 = new Garbage(); // Named Memory
obj1.obj = obj2;
obj2.obj = obj3;
obj3.obj = obj1;
obj1 = null;
obj2 = null;
obj3 = null;
System.gc();
Thread.sleep(1000);
System.out.println(" Program Ends ");
}
}
public class Garbage
{
static int a = 0;
@Override
public void finalize()
{
System.out.println((++a)+" Memory Cleaning By: "+Thread.currentThread().getName());
}
Garbage obj;
public static void main( String args[] ) throws InterruptedException
{
Garbage obj1 = new Garbage(); // Used Memory
Garbage obj2 = new Garbage(); // Referenced Object Creation
Garbage obj3 = new Garbage(); // Named Memory
obj1.obj = obj2;
obj2.obj = obj3;
obj3.obj = obj1;
obj1 = null;
obj2 = null;
obj3 = null;
System.gc();
Thread.sleep(1000);
System.out.println(" Program Ends ");
}
}
Output: