-
-
Notifications
You must be signed in to change notification settings - Fork 217
/
MemoryFiller.java
55 lines (48 loc) · 1.72 KB
/
MemoryFiller.java
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
import java.util.ArrayList;
import static java.lang.Math.pow;
public class MemoryFiller {
private static final Runtime runtime = Runtime.getRuntime();
public static void main(String[] args) {
printTotalMemory();
ArrayList<long[]> list = new ArrayList<>();
int size = Double.valueOf(pow(2, 20)).intValue();
while (true) {
list.add(new long[size]);
printFreeMemory();
}
}
private static String formatPrintMemory(long memory) {
int divisor = Double.valueOf(pow(1024, 3)).intValue();
int gigabytes = (int) (memory / divisor);
memory = memory % divisor;
divisor = Double.valueOf(pow(1024, 2)).intValue();
int megabytes = (int) (memory / divisor);
memory = memory % divisor;
divisor = 1024;
int kilobytes = (int) (memory / divisor);
int bytes = (int) (memory % divisor);
StringBuilder formattedMemory = new StringBuilder();
if (gigabytes != 0) {
formattedMemory.append(gigabytes + " GB ");
}
if (megabytes != 0) {
formattedMemory.append(megabytes + " MB ");
}
if (kilobytes != 0) {
formattedMemory.append(kilobytes + " KB ");
}
if (bytes != 0) {
formattedMemory.append(bytes + " B");
}
return formattedMemory.toString();
}
private static void printMemory(String text, long memory) {
System.out.println(text + formatPrintMemory(memory));
}
private static void printTotalMemory() {
printMemory("Total Memory: ", runtime.totalMemory());
}
private static void printFreeMemory() {
printMemory("Free Memory: ", runtime.freeMemory());
}
}