如何改变JVM的默认的HEAP大小

本文介绍了如何通过命令行选项调整Java程序的内存分配。当Java程序消耗大量内存时,可以通过增加堆大小来避免出现OutOfMemoryError错误。文章详细解释了不同Java版本中用于设置初始和最大堆大小的选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Setting Heap Sizes
If your Java program requires a large amount of memory, it's possible that the virtual machine will begin to throw OutOfMemoryError instances when attempting to instantiate objects. In some cases, this may be the result of a programming error, but in others, it's simply a result of your program legitimately using more memory than is available. In this latter case, you can increase the "heap size" allocated by the Java Virtual Machine (JVM) by using command line options. When not specified, the heap size defaults to 1 MB, and can increase to as much as 16 MB if your program requires more memory. To set the initial amount of memory allocated for your program, use the -ms option with a 1.1 JVM, and the -Xms option with a 1.2 (also known as Java 2) JVM. To set the maximum amount of memory that can be allocated for your program, use the -mx or -Xmx for Java 1.1 or 1.2, respectively. For example:

java -ms32m -mx128m MyClassName (Java 1.1)
java -Xms32m -Xmx128m MyClassName (Java 1.2 / 2.0)

In each case, the options specify that 32 MB of memory should be allocated initially for the program to run in, and that up to 128 MB may be allocated if necessary.
You should note the presence of the "X" on the Java 1.2 / 2.0 options. It indicates that these are non-standard options that might not function the same way or could even be removed in future JVMs. In the meantime, you may find it necessary to take advantage of these options if your program requires a large amount of memory.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值