-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCDemo.java
More file actions
41 lines (33 loc) · 1.21 KB
/
GCDemo.java
File metadata and controls
41 lines (33 loc) · 1.21 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
public class GCDemo {
String objectName;
// Constructor
public GCDemo(String name) {
this.objectName = name;
System.out.println("Created: " + this.objectName);
}
// Overriding finalize method
// (Note: This is deprecated since Java 9)
@Override
protected void finalize() throws Throwable {
System.out.println("Finalizing: " + this.objectName + " | Thread: " + Thread.currentThread().getName());
}
public static void main(String[] args) {
// 1. Create objects
GCDemo obj1 = new GCDemo("Object_1");
GCDemo obj2 = new GCDemo("Object_2");
// 2. Make objects eligible for Garbage Collection
// By setting them to null, they have no live references
obj1 = null;
obj2 = null;
// 3. Suggesting the JVM to run Garbage Collector
System.out.println("Requesting JVM to perform Garbage Collection...");
System.gc();
// 4. Giving the GC a small moment to run before the program ends
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Main method execution finished.");
}
}