启动命令如下:java -server -Xms128m -Xmx128m -jar xxxx.jar
top命令显示如下: PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
15332 root 17 0 555m 242m 11m S 0.0 12.1 1:46.95 java
疑问:为什么VIRT和RES大于128m呢?难道-Xms -Xmx没起作用?
解答:这两个参数只是限值Java程序的heap
大小,如果你了解heap
和stack
那么应该知道,一般来说只有运行时动态创建的对象
才存在于heap
中;
$ java -X
...
-Xms<size> set initial Java `heap size`
-Xmx<size> set maximum Java `heap size`
...
另外,通过top
命令查看到的资源情况是针对整个Java进程的,JVM在执行你的Java代码时还需要载入整个Java Runtime
,有自己的开销,这些都不包括在-Xms/-Xmx
中
- Note that the JVM uses more memory than just the heap. For example
- Java methods, thread stacks and native handles are allocated in memory
- separate from the heap, as well as JVM internal data structures.
借鉴地址
java 虚拟机设置 Xms Xmx PermSize MaxPermSize
JAVA8中不再有PermSize,替换为MetaspaceSize