热点默认最大堆大小
根据以下文档 http: //www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html#par_gc.ergonomics.default_size 默认最大值并行 GC 的堆大小是使用公式 MIN(内存 / 4, 1GB) 选择的。从公式中可以明显看出,但文档仍然指出“默认最大堆大小不会超过 1GB,无论机器上安装了多少内存”。为了验证我是否编写了以下程序,
public class Allocate{ public static void main(String[] args) throws Exception { long megabytes = Long.valueOf(args[0]); long bytes = megabytes * 1024 * 1024; int longs = (int) (bytes / 8); long[] arr = new long[longs]; Thread.sleep(Long.MAX_VALUE); System.out.println(arr.length); } }
我在具有 16Gb RAM 的机器上执行了该程序。
smeldris@us4nrsdn01 allocation]$ /usr/java/jdk1.6.0_26/bin/java Allocate 2048 & [1] 9291 [smeldris@us4nrsdn01 allocation]$ /usr/java/jdk1.6.0_26/bin/jmap -heap 9291 Attaching to process ID 9291, please wait... Debugger attached successfully. Server compiler detected. JVM version is 20.1-b02 using thread-local object allocation. Parallel GC with 8 thread(s) Heap Configuration: MinHeapFreeRatio = 40 MaxHeapFreeRatio = 70 MaxHeapSize = 4208984064 (4014.0MB) NewSize = 1310720 (1.25MB) MaxNewSize = 17592186044415 MB OldSize = 5439488 (5.1875MB) NewRatio = 2 SurvivorRatio = 8 PermSize = 21757952 (20.75MB) MaxPermSize = 85983232 (82.0MB) Heap Usage: PS Young Generation Eden Space: capacity = 65798144 (62.75MB) used = 1315976 (1.2550125122070312MB) free = 64482168 (61.49498748779297MB) 2.0000199397721614% used From Space: capacity = 10944512 (10.4375MB) used = 0 (0.0MB) free = 10944512 (10.4375MB) 0.0% used To Space: capacity = 10944512 (10.4375MB) used = 0 (0.0MB) free = 10944512 (10.4375MB) 0.0% used PS Old Generation capacity = 2322923520 (2215.3125MB) used = 2147483664 (2048.000015258789MB) free = 175439856 (167.31248474121094MB) 92.44745449045176% used PS Perm Generation capacity = 21757952 (20.75MB) used = 2606752 (2.485992431640625MB) free = 19151200 (18.264007568359375MB) 11.980686417545181% used [smeldris@us4nrsdn01 allocation]$
2GB的数组是在老年代分配的。 MaxHeapSize为4GB,是系统内存的1/4。为什么JVM为堆保留4GB?
According to the following document http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html#par_gc.ergonomics.default_size the default maximum heap size for Parallel GC is chosen using formula MIN(memory / 4, 1GB). It's obvious from the formula, but the document still notes that "that the default maximum heap size will not exceed 1GB, regardless of how much memory is installed on the machine." To verify that I wrote the following program
public class Allocate{ public static void main(String[] args) throws Exception { long megabytes = Long.valueOf(args[0]); long bytes = megabytes * 1024 * 1024; int longs = (int) (bytes / 8); long[] arr = new long[longs]; Thread.sleep(Long.MAX_VALUE); System.out.println(arr.length); } }
I executed this program on a box with 16Gb of RAM.
smeldris@us4nrsdn01 allocation]$ /usr/java/jdk1.6.0_26/bin/java Allocate 2048 & [1] 9291 [smeldris@us4nrsdn01 allocation]$ /usr/java/jdk1.6.0_26/bin/jmap -heap 9291 Attaching to process ID 9291, please wait... Debugger attached successfully. Server compiler detected. JVM version is 20.1-b02 using thread-local object allocation. Parallel GC with 8 thread(s) Heap Configuration: MinHeapFreeRatio = 40 MaxHeapFreeRatio = 70 MaxHeapSize = 4208984064 (4014.0MB) NewSize = 1310720 (1.25MB) MaxNewSize = 17592186044415 MB OldSize = 5439488 (5.1875MB) NewRatio = 2 SurvivorRatio = 8 PermSize = 21757952 (20.75MB) MaxPermSize = 85983232 (82.0MB) Heap Usage: PS Young Generation Eden Space: capacity = 65798144 (62.75MB) used = 1315976 (1.2550125122070312MB) free = 64482168 (61.49498748779297MB) 2.0000199397721614% used From Space: capacity = 10944512 (10.4375MB) used = 0 (0.0MB) free = 10944512 (10.4375MB) 0.0% used To Space: capacity = 10944512 (10.4375MB) used = 0 (0.0MB) free = 10944512 (10.4375MB) 0.0% used PS Old Generation capacity = 2322923520 (2215.3125MB) used = 2147483664 (2048.000015258789MB) free = 175439856 (167.31248474121094MB) 92.44745449045176% used PS Perm Generation capacity = 21757952 (20.75MB) used = 2606752 (2.485992431640625MB) free = 19151200 (18.264007568359375MB) 11.980686417545181% used [smeldris@us4nrsdn01 allocation]$
The 2GB array was allocated in the old generation. The MaxHeapSize is 4GB, which is 1/4 of system memory. Why did JVM reserve 4GB for the heap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在这里发布了这个问题来打开jdk邮件列表 http://mail.openjdk.java.net/pipermail/hotspot-gc-use/2011-August/000912.html。
GC 人体工程学在 (1.6.0_18) 中已更改,但文档尚未更新。
对应的bug Id https://bugs.java.com/bugdatabase/view_bug?bug_id= 6887571。
发行说明 http://www.oracle.com/technetwork/java/ javase/6u18-142093.html
I posted this question to open jdk mailing list here http://mail.openjdk.java.net/pipermail/hotspot-gc-use/2011-August/000912.html.
The GC ergonomics was changed in the (1.6.0_18), but the documentation hasn't been updated yet.
The corresponding bug Id https://bugs.java.com/bugdatabase/view_bug?bug_id=6887571.
Release notes http://www.oracle.com/technetwork/java/javase/6u18-142093.html