runtime.gc().
JVM , , .
> 1. 1,25, . " ".
, , "maxMemory()".
public class Mem2 {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
runtime.gc();
long freeMemory = runtime.freeMemory();
double factor = 1.29;
int size = (int) (factor * freeMemory);
System.out.println(" freememory is " + freeMemory);
System.out.println(" size is " + size);
System.out.println("the total memory is " + runtime.totalMemory());
System.out.println(" the max memory is " + runtime.maxMemory());
byte[] testArray = new byte[size];
}
}
:
freememory is 84466864
size is 108962254
the total memory is 85000192
the max memory is 129957888
Process finished with exit code 0
, , 20 .
, totalMemory() - , JVM, freeMemory() - , , maxMemory() - .
totalMemory() freememory() .
public class Mem3 {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
for (int i = 0; true; i++) {
runtime.gc();
int size = i * 10000000;
System.out.println(" i is " + i);
System.out.println(" size is " + size);
System.out.println("b freememory is " + runtime.freeMemory());
System.out.println("b the total memory is " + runtime.totalMemory());
System.out.println("b the max memory is " + runtime.maxMemory());
byte[] testArray = new byte[size];
System.out.println(" array " + testArray.length);
System.out.println("a freememory is " + runtime.freeMemory());
System.out.println("a the total memory is " + runtime.totalMemory());
System.out.println("a the max memory is " + runtime.maxMemory());
System.out.println(" ");
}
}
}
, , . , 6 7:
i is 6
size is 60000000
b freememory is 84300496
b the total memory is 85000192
b the max memory is 129957888
array 60000000
a freememory is 24300472
a the total memory is 85000192
a the max memory is 129957888
i is 7
size is 70000000
b freememory is 84300496
b the total memory is 85000192
b the max memory is 129957888
array 70000000
a freememory is 59258168
a the total memory is 129957888
a the max memory is 129957888
6 , 60M 24M. 7 . ( totalMemory), freeMemory 60M.