-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGarbageCollection.java
More file actions
56 lines (51 loc) · 1.95 KB
/
GarbageCollection.java
File metadata and controls
56 lines (51 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.Scanner;
//An object is eliible for garbage collection if its ref variable is lost from the program
//during execution.
//1.Object created inside a method
//2.Reassigning the reference variable
//3.Nullifying the reference variable
//4.Anonymous object
public class GarbageCollection {
String obj_name;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int type=0;
while(type!=5){
type= sc.nextInt();
switch(type){
case 1: System.out.println("Object created inside a method");
method1();
System.gc();
break;
case 2: System.out.println("Reassigning the reference variable");
GarbageCollection g1 = new GarbageCollection("g1");
GarbageCollection g2 = new GarbageCollection("g2");
g1 = g2; //the previous g1 obj can be garbage collected
System.gc();
break;
case 3: System.out.println("Nullifying the reference variable");
GarbageCollection g3 = new GarbageCollection("g3");
g3 = null;
System.gc();
break;
case 4: System.out.println("Anonymous object");
new GarbageCollection("g4");
System.gc();
break;
}
}
}
public GarbageCollection(String obj_name){
this.obj_name=obj_name;
}
static void method1(){
GarbageCollection t1 = new GarbageCollection("g1");
method2();
}
static void method2(){
GarbageCollection t2=new GarbageCollection("g2");
}
protected void finalize() throws Throwable{
System.out.println(this.obj_name+" successfully garbage collected");
}
}