-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGarbageCollector.java
More file actions
86 lines (70 loc) · 2.5 KB
/
Copy pathGarbageCollector.java
File metadata and controls
86 lines (70 loc) · 2.5 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;
public class GarbageCollector {
static void initialGC() {
heap = new ArrayList<Integer>();
countReference = new ArrayList<Integer>();
countTotalSpace = 0;
varType = new HashMap<String, Stack<Integer>>();
}
public static List<Integer> heap;
public static List<Integer> countReference;
public static int countTotalSpace;
public static HashMap<String, Stack<Integer>> varType;
//when encounter a new int declaration
static void intVariableDeclInScope(String var) {
if (!varType.containsKey(var)) {
varType.put(var, new Stack<Integer>());
}
varType.get(var).push(0);
}
//when encounter a new reference declaration
static void refVariableDeclInScope(String var) {
if (!varType.containsKey(var)) {
varType.put(var, new Stack<Integer>());
}
varType.get(var).push(1);
}
//countReference-1 if x refers to other value, when use "new" or "define" in Assign
static void subCountReference(String var) {
Integer oldPositionInHeap = Executor.varGet(var); //get position of var in the heap
if(oldPositionInHeap!=null)
{
// System.out.println("count reference for"+ var+ "is"+countReference.get(oldPositionInHeap));
countReference.set(oldPositionInHeap,
countReference.get(oldPositionInHeap) - 1);
if (countReference.get(oldPositionInHeap) == 0) {
countTotalSpace--;
// System.out.println("CHECK WHETHER IS 0");
// if(countTotalSpace>=0)
// {
System.out.println("gc:" + countTotalSpace);
// }
}
}
}
static void IncCountReference(String var) {
Integer oldPositionInHeap = Executor.varGet(var); //get position of var in the heap
if(oldPositionInHeap!=null)
{
countReference.set(oldPositionInHeap,
countReference.get(oldPositionInHeap) + 1);
}
}
static void subCountReference(String var,HashMap<String, Integer> map) {
Integer oldPositionInHeap = map.get(var); //get position of var in the heap
if(oldPositionInHeap!=null)
{
countReference.set(oldPositionInHeap,
countReference.get(oldPositionInHeap) - 1);
if (countReference.get(oldPositionInHeap) == 0) {
countTotalSpace--;
// if(countTotalSpace>=0){
System.out.println("gc:" + countTotalSpace);
//}
}
}
}
}