java 如何以编程方式增加java堆大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2073869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:16:34  来源:igfitidea点击:

How to increase java heap size programmatically

javasizeheap

提问by hrl

I have a java desktop application for searching files and it is usually reaching the default heap limit pretty soon. I wont have access to all the systems it will be installed in so I want to increase the JVM heap size in the application itself. Can anybody help me how can I do that programmatically in my application

我有一个用于搜索文件的 Java 桌面应用程序,它通常很快就会达到默认的堆限制。我无法访问将安装它的所有系统,因此我想增加应用程序本身的 JVM 堆大小。任何人都可以帮助我如何在我的应用程序中以编程方式执行此操作

回答by ShabbyDoo

Setting -Xmx to one gig doesn't mean that the JVM will allocate that much memory upon start-up. The JVM will allocate only -Xms (plus overhead) until more heap space is needed. Do you need to protect your users from thrashing virtual memory or a failed memory allocation from the OS? If not, just set Xmx to a large value. Note that Windows 32bit JVMs will often ignore Xmx settings greater than 1.2Gig, so it's best to not request more than a gig or so to be safe.

将 -Xmx 设置为一个 gig 并不意味着 JVM 将在启动时分配那么多内存。JVM 将仅分配 -Xms(加上开销),直到需要更多堆空间。您是否需要保护您的用户免受虚拟内存抖动或操作系统内存分配失败的影响?如果没有,只需将 Xmx 设置为一个较大的值。请注意,Windows 32 位 JVM 通常会忽略大于 1.2Gig 的 Xmx 设置,因此为了安全起见,最好不要请求超过 gig 左右。

回答by Thorbj?rn Ravn Andersen

There is no such standard API to do so.

没有这样的标准 API 来做到这一点。

I would suggest you use Java Web Start to invoke your application (can be used for local applications to in the latest Sun Java 6) as it allows you to specify values for this.

我建议您使用 Java Web Start 来调用您的应用程序(可用于本地应用程序到最新的 Sun Java 6),因为它允许您为此指定值。

You can then have three or four links each pointing to exactly the same files but with "Tiny", "Medium", "Large", "Gigantic" heap sizes.

然后,您可以有三个或四个链接,每个链接指向完全相同的文件,但具有“微小”、“中等”、“大型”、“巨大”堆大小。

回答by Mark Elliot

Take a look at something like launch4jand consider deploying executables where you can control the limit when someone begins the run of your application.

看看launch4j 之类的东西,并考虑部署可执行文件,在有人开始运行您的应用程序时,您可以在其中控制限制。

You won't be able to do this programmatically.

您将无法以编程方式执行此操作。

回答by Michael Brewer-Davis

For desktop applications, I suggest providing a launcher which can then specify your desired memory size. In addition to configuration of your JVM, you can also provide an icon, file associations, etc.

对于桌面应用程序,我建议提供一个可以指定所需内存大小的启动器。除了配置 JVM 之外,您还可以提供图标、文件关联等。

See this SO questionfor deployment alternatives.

有关部署替代方案,请参阅此SO 问题

回答by madshiva

See this post about increase java heap size :

请参阅有关增加 Java 堆大小的帖子:

increase the java heap size permanently?

永久增加java堆大小?

Maybe also you could clean some part of your code to reduce memory usage.

也许您也可以清理代码的某些部分以减少内存使用量。

Best Regards

最好的祝福