Consider the following code:

public class Test {
  static volatile Integer discard;
  public static void main(String [] args) throws InterruptedException {
    printMemory();
    System.gc();
    printMemory();
    int iterations = 1000;
    int[] vals = new int[100_000_000];
    while (args.length == 0) {
      printMemory();
      System.gc();
      Thread.sleep(200);
      discard = iterations++;
    }
  }

  private static void printMemory() {
    System.out.println(Runtime.getRuntime().totalMemory() - 
Runtime.getRuntime().freeMemory());
  }
}

I am surprised to see the memory used by this code starts at about 200MB, 
goes up to 600MB, but never seems to go back down.  The large int array 
accounts for the memory usage jump, but it never seems to be garbage 
collected.  Why?   The variable is never read after it is allocated.  It 
cannot be reordered by the compiler to be after the while loop, because I 
can see the memory jump.  I am intentionally allocating memory in the loop 
by boxing the integer.  Enabling +PrintCompilation and PrintInlining never 
shows main() being inlined; perhaps it is not being compiled?

-- 
You received this message because you are subscribed to the Google Groups 
"mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to