the program first executes a start
method to take up a lot of memory, and then executes endEmpty
to call System.gc ()
to run, the task manager can see that the memory footprint of the program immediately reaches 4G, but the memory footprint of the start
method does not decrease
after it is destacked. How can I free this memory?
and if you call System.gc ()
within the start method, but it works, when the method is useful and when it is invalid.
static final long BEGIN_MILLIS = System.currentTimeMillis();
public static void main(String[] args) {
HelloWorld h = new HelloWorld();
h.start();
h.endEmpty();
}
public void start() {
System.out.println("start");
ArrayList<Object> list = new ArrayList<Object>();
while (System.currentTimeMillis() - BEGIN_MILLIS < TimeUnit.SECONDS.toMillis(10)) {
list.add(System.getProperties().clone());
// System.gc();
}
}
public void endEmpty() {
System.out.println("end");
while (true) {
System.gc();
}
}