Herong's Tutorial Notes on JVM
Dr. Herong Yang, Version 3.00, 2007

Memory Management

Part:   1  2 

(Continued from previous part...)

Output on JRockit 7.0 JVM with -Xms2m -Xmx64m

Having 0 MB and adding 10 MB...
 Free memory: 8717040
Total memory: 19419136
Having 10 MB and adding 10 MB...
 Free memory: 8698800
Total memory: 29904896
Having 20 MB and adding 10 MB...
 Free memory: 7607344
Total memory: 39297024
Having 30 MB and adding 10 MB...
 Free memory: 6951960
Total memory: 49127424
Having 40 MB and adding 10 MB...
 Free memory: 6558720
Total memory: 59219968
Having 50 MB and adding 10 MB...
java.lang.OutOfMemoryError

Note that

  • My 10MB array is slightly more than 10MB because of the array overhead.
  • I also tested with JRockit with -Xmx512m, it got 200MB before running out of memory. My system has: 128MB physical memory, and 288MB paging file size.

Garbage Collection

I wrote the following program to watch how the garbage collection process works:

/**
 * GarbageCollection.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
class GarbageCollection {
   public static void main(String[] a) {
      int max = 10000;
      int min = 32;
      Object[] arr = new Object[min];
      Runtime rt = Runtime.getRuntime();
      System.out.println("Free/total memory:");
      for (int m=0; m<max; m++) {
      	 for (int n=0; n<min-1; n++) arr[min-n-1] = arr[min-n-2];
         arr[0] = getOneMega();
         System.out.println(rt.freeMemory()+"   "+rt.totalMemory());
      	 try {
      	    Thread.sleep(1000/10);
      	 } catch (InterruptedException e) {
            System.out.println("Interreupted...");
      	 }
      }
   }
   private static Object getOneMega() {
      // return new long[1024*128]; // 1MB
      Object[] lst = new Object[10];
      lst[0] = new long[256*128];
      lst[1] = new int[256*256];
      lst[2] = new double[256*128];
      lst[3] = new float[64*256];
      lst[4] = new byte[64*1024];
      String[] l = new String[64*64];
      for (int i=0; i<64*64; i++) 
      	 l[i] = new String("12345678"); // 16B
      lst[5] = l;
      lst[6] = new char[64*512];
      return lst;
   }
}

Output on HotSpot Client VM with -Xms2m -Xmx64m

Free/total memory:
......
30568544   65839104
29469976   65839104
28371408   65839104
27272840   65839104
26176960   65839104
25078392   65839104
23979824   65839104
22881256   65839104
21782688   65839104
20687480   65839104
19588912   65839104
18490344   65839104
17391776   65839104
16295896   65839104
15197328   65839104
14098760   65839104
13000192   65839104
11901624   65839104
10806416   65839104
9707848   65839104
8609280   65839104
7510712   65839104
6414832   65839104
5316264   65839104
4217696   65839104
3119128   65839104
2020560   65839104
30568544   65839104
......

I am only showing one segment of the output here. You can see that the garbage collector only starts to work when the free memory is getting close the specified maximum, 64MB.

Part:   1  2 

Dr. Herong Yang, updated in 2007
Herong's Tutorial Notes on JVM - Memory Management