是的,有一个最大值,但这取决于系统。试试看,翻一番,直到达到极限,然后向下搜索。至少对于linux上的Sun JRE 1.6,您会发现有趣的错误消息(peregrino是运行32位ubuntu的上网本,2G RAM,无交换):
peregrino:$ java -Xmx4096M -cp bin WheelPrimes
Invalid maximum heap size: -Xmx4096M
The specified size exceeds the maximum representable size.
Could not create the Java virtual machine.
peregrino:$ java -Xmx4095M -cp bin WheelPrimes
Error occurred during initialization of VM
Incompatible minimum and maximum heap sizes specified
peregrino:$ java -Xmx4092M -cp bin WheelPrimes
Error occurred during initialization of VM
The size of the object heap + VM data exceeds the maximum representable size
peregrino:$ java -Xmx4000M -cp bin WheelPrimes
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.
(experiment reducing from 4000M until)
peregrino:$ java -Xmx2686M -cp bin WheelPrimes
(normal execution)
大多数都是不言而喻的,除了-Xmx4095M,它相当奇怪(可能是有符号/无符号比较?),并声称在一台2GB的机器上预留26.86亿美元,无需交换。但这也暗示了32位虚拟机的最大大小是4G,而不是2G,如果操作系统允许你处理那么多。