我想以编程方式确定Java堆内存名称。当我运行以下代码时,即使在两个不同的计算机上安装了相同的java,它也会为java堆内存提供两个不同的名称。
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryType;
public class MemTest {
public static void main(String[]args) {
for (MemoryPoolMXBean mpBean : ManagementFactory.getMemoryPoolMXBeans()) {
System.out.println(mpBean.getType());
System.out.println(mpBean.getName());
}
}
}
机器1的输出
support@vrni-platform:~$ java -version
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
support@vrni-platform:~$ vi MemTest.java
support@vrni-platform:~$ javac MemTest.java
support@vrni-platform:~$ java MemTest
Non-heap memory
Code Cache
Non-heap memory
Metaspace
Non-heap memory
Compressed Class Space
Heap memory
PS Eden Space
Heap memory
PS Survivor Space
Heap memory
PS Old Gen
java设置的输出
java -XshowSettings:vm -XX:+PrintCommandLineFlags -version
-XX:InitialHeapSize=32773376 -XX:MaxHeapSize=524374016 -XX:+PrintCommandLineFlags -XX:+UseCompressedClassPointers -XX:+UseCompressedOops
VM settings:
Max. Heap Size (Estimated): 485.31M
Ergonomics Machine Class: client
Using VM: Java HotSpot(TM) 64-Bit Server VM
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
机器2的输出
support@vrni-platform:~$ java -version
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
ubuntu@vrni-platform:~/logs/tsdb-server$ vim MemTest.java
ubuntu@vrni-platform:~/logs/tsdb-server$ javac MemTest.java
ubuntu@vrni-platform:~/logs/tsdb-server$ java MemTest
Non-heap memory
Code Cache
Non-heap memory
Metaspace
Non-heap memory
Compressed Class Space
Heap memory
Eden Space
Heap memory
Survivor Space
Heap memory
Tenured Gen
java设置的输出
java -XshowSettings:vm -XX:+PrintCommandLineFlags -version
-XX:InitialHeapSize=262920896 -XX:MaxHeapSize=4206734336 -XX:+PrintCommandLineFlags -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC
VM settings:
Max. Heap Size (Estimated): 3.48G
Ergonomics Machine Class: server
Using VM: Java HotSpot(TM) 64-Bit Server VM
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
有人可以让我知道为什么当环境相同(Ubuntu 14.04,Java版本相同)时,为什么给堆内存使用两个不同的名称吗? Heap Memory
的所有可能名称是什么?
答案 0 :(得分:2)
堆内存区域是所使用的垃圾收集器的功能。
计算机之间的差异是由于以下事实引起的:计算机1被分类为客户端计算机,而计算机2被分类为服务器计算机。原因是可用内存量。分类差异导致选择了不同的垃圾收集器(特别是机器1的串行收集器和机器2的并行收集器),这又导致了堆内存区域的差异。
另请参阅Java Platform, Standard Edition HotSpot Virtual Machine Garbage Collection Tuning Guide,特别是Garbage Collector, Heap, and Runtime Compiler Default Selections部分。