2

I wrote a java class and run it in linux. JVM always reserve nearly 1G memory No matter how big the heap size is. I tried

java -Xms512m -Xmx512m  MemTe
java -Xms100m -Xmx100m  MemTe
java -Xms2048m -Xmx2048m  MemTe

There is always a line in pmap,Value is 1048064 104768.....

000000000204c000     132      12      12 rw---   [ anon ]
00000000e0000000  524800    4368    4368 rw---   [ anon ](It's heap size)
0000000100080000 1048064       0       0 -----   [ anon ]

00000000018f9000     132      12      12 rw---   [ anon ]
00000000f9c00000  102912    2320    2320 rw---   [ anon ](It's heap size)
0000000100080000 1048064       0       0 -----   [ anon ]


000000000176d000     132      12      12 rw---   [ anon ]
0000000080000000 2098048   13104   13104 rw---   [ anon ](It's heap size)
00000001000e0000 1047680       0       0 -----   [ anon ]

Linux server has 6VCPU 16GB memory

Anyone can explain why? Thanks

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MemTe {
  static volatile boolean RUN = false;
  static String s = null;
  static int tcnt;

  public static void main(String args[]) throws {
    List<String> st = new ArrayList<>();
    while (System.in.read() > 0) {
      RUN = false;
      Thread t = new Thread(MemTe::go);
      t.start();
      tcnt++;
      System.out.println("Threads: " + tcnt);
      System.out.println(System.in.read());
      RUN = true;

      st.add(s);
    }

  }

  private static void go() {
    while (true) {
      while (RUN) {           
      System.out.println(Thread.currentThread().getId());
        s = new String("s");

        try {
          Thread.sleep(100000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }

  }
}
FFMan
  • 73
  • 5

1 Answers1

3

This is Compressed Class Space.
Its default size is exactly 1G; can be modified with -XX:CompressedClassSpaceSize.

The virtual memory for the whole Compressed Class Space is initially reserved, but not committed, i.e. it does not take physical memory. The pages are committed on demand as the usage of Compress Class Space grows. That's why in pmap output you'll see two neighbor regions: the first (with rw protection attributes) is committed, and the rest is reserved (RSS=0).

0000000100000000     512     260     260 rw---   [ anon ]
0000000100080000 1048064       0       0 -----   [ anon ]

More about JVM virtual memory

apangin
  • 92,924
  • 10
  • 193
  • 247
  • Thinks you for your helpful answer. Another question is why the RSS and dirty is 0 ? I tried create difference class instance in main method. But the RSS value is not changed . always 0 as your pasted link. – FFMan Mar 19 '19 at 07:07
  • I think the Compress Class Space committed size should not be 0 when the main function running because classloader has loaded some classes to the space . But it's 0 actually. That‘s my new question. – FFMan Mar 20 '19 at 05:48
  • @FFMan It's not zero. In your case committed space starts at `100000000` and ends at `100080000` or `1000e0000`. Since it is adjacent to heap and has the same protection attributes, it is shown as the single region together with heap. – apangin Mar 20 '19 at 07:38