是的,有一个最大值,但它取决于系统。试试看,加倍,直到你达到一个极限,然后向下搜索。至少在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计算机上保留2686M。但它确实暗示,如果操作系统允许您解决这么多问题,则32位VM的最大大小是4G而不是2G。