大页的优点在于可以使得堆常驻内存,不会被交换到磁盘。其次,OS管理的页面数大大减少,对于20GB内存来说,4K页面需要5,242,880个页面,2MB的话只需要10,240,相当于前者的99.8%,所以操作系统负载会大大降低。
大多数测试大页面发现对于eden区的GC可以大大提升性能(不过大都基于JDK 1.5-1.7测试居多)。LZ做了简单的测试:
private static void testEdenGc() {
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
e.printStackTrace();
}
UUID uuid = UUID.randomUUID();
for (int f=0;f<10;f++) {
new Thread(new Runnable() {
@Override
public void run() {
long res = 0;
for (int i = 0; i < 100; i++) {
Map<String, Object> bigMap = new HashMap();
for (int x = 0; x < 100000; x++) {
bigMap.put(uuid.toString() + uuid.toString() + uuid.toString() + uuid.toString(), uuid);
}
res = res + bigMap.size();
}
}
}).start();
}
}
大页面模式:
[root@hs-test-10-20-30-16 ~]# time java -Xms3000m -Xmx3000m -XX:+UseLargePages -XX:LargePageSizeInBytes=4m -cp . com.hundsun.ta.base.service.CompletionServiceTest
Java HotSpot(TM) 64-Bit Server VM warning: Setting LargePageSizeInBytes has no effect on this OS. Large page size is 2048K. # 在Linux下大页面大小由os决定,而非参数决定。
real 1m21.333s
user 10m20.109s
sys 0m1.539s
普通页面模式:
[root@hs-test-10-20-30-16 ~]# time java -Xms3000m -Xmx3000m -cp . com.hundsun.ta.base.service.CompletionServiceTest
real 1m12.794s
user 8m58.983s
sys 0m1.694s
在centos 6.5下使用jdk 1.8u191测试,各自重复运行了三次,依然是普通页面比大页面快10%以上。由此可见,大页面并不总是可以提高性能。当然也可能场景过于简单。
但是openj9下,启用大页面则比不启用大页面要快10%。
除了linux,windows下也支持大页面的,可参见如下:

由于LZ环境不会使用windows作为server,故有兴趣同学可以自行测试。
参考:
https://oracle-base.com/articles/linux/configuring-huge-pages-for-oracle-on-linux-64#configuring-1g-hugepagesize
https://www.iteye.com/blog/agapple-809707
https://www.oracle.com/technetwork/java/javase/tech/largememory-jsp-137182.html
https://access.redhat.com/documentation/en-us/jboss_enterprise_application_platform/5/html/performance_tuning_guide/sect-performance_tuning_guide-java_virtual_machine_tuning-large_page_memory
