根據以下文檔http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html#par_gc.ergonomics.default_size,使用公式MIN(內存/ 4,1GB)選擇並行GC的默認最大堆大小。從公式中可以明顯看出,但文檔仍然指出「不管計算機上安裝了多少內存,默認的最大堆大小不會超過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上執行這個程序。
[email protected] allocation]$ /usr/java/jdk1.6.0_26/bin/java Allocate 2048 & [1] 9291 [[email protected] 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 [[email protected] allocation]$
2GB陣列被分配到老一代。 MaxHeapSize是4GB,是系統內存的1/4。爲什麼JVM爲堆預留4GB?