Java 关于增加 JVM 的堆大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9788001/
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-08-16 07:40:03  来源:igfitidea点击:

about increasing heap size of JVM

javajvm

提问by Vinay

i have developed chatting application using threads. but when i start my application system acts very slow and sometime exception occur that heap is full. i want to increase heap size of Java Virtual Machine. how can i do it?

我使用线程开发了聊天应用程序。但是当我启动我的应用程序时,系统的动作非常缓慢,有时会发生堆已满的异常。我想增加 Java 虚拟机的堆大小。我该怎么做?

回答by Mathias Schwarz

Add -Xmx100mto the command when you start your app. This will give you 100 MB heap (you can change the number).

-Xmx100m启动应用程序时添加到命令中。这将为您提供 100 MB 堆(您可以更改数量)。

It sounds strange that a chat app would required more than the standard heap size...

聊天应用程序需要的堆大小超过标准堆大小,这听起来很奇怪……

回答by duffymo

You can increase heap size, but your larger issue is "Why did I get that exception?" Increasing the heap size will only delay the inevitable if your application is not cleaning up after itself properly.

您可以增加堆大小,但更大的问题是“为什么我会收到该异常?” 如果您的应用程序没有正确地自行清理,增加堆大小只会延迟不可避免的事情。

You need to instrument your application with Visual VM and see what's going on. That will give you more of a path forward than simply increasing the heap size.

您需要使用 Visual VM 检测您的应用程序并查看发生了什么。这将为您提供更多的前进道路,而不仅仅是增加堆大小。

回答by óscar López

Just increase the heap size of the JVM. All Java applications, even simple ones, consume a lot of memory. Take a look at this articleexplaining in detail how to increase the amount of memory available for your application; basically you'll need to pass a couple of extra parameters to the JVM when you invoke the javacommand, like this:

只需增加 JVM 的堆大小。所有 Java 应用程序,即使是简单的应用程序,都会消耗大量内存。看看这篇文章,详细解释了如何增加应用程序可用的内存量;基本上,当您调用java命令时,您需要将几个额外的参数传递给 JVM ,如下所示:

java -Xms64m -Xmx256m HelloWorld

In the above command, I'm saying that the HelloWorldprogram should have an initial heap size of 64MB and a maximum of 256MB. Try with these values and fiddle a bit with them until you find a combination of values that works for your application.

在上面的命令中,我是说HelloWorld程序的初始堆大小应该为 64MB,最大为 256MB。尝试使用这些值并稍微调整一下,直到找到适合您的应用程序的值组合。

回答by Zhivko Draganov

Blockquote

块引用

Large server applications often experience two problems with these defaults. One is slow startup, because the initial heap is small and must be resized over many major collections. A more pressing problem is that the default maximum heap size is unreasonably small for most server applications.

大型服务器应用程序通常会遇到这些默认值的两个问题。一种是启动缓慢,因为初始堆很小并且必须在许多主要集合上调整大小。一个更紧迫的问题是,对于大多数服务器应用程序来说,默认的最大堆大小小得不合理。

Blockquote

块引用

You could start your program via command prompt with these parameters java -Xms64m -Xmx256m chat_program. Here Xms64m = 64mb initial heap size and Xmx256m = 256mb maximum heap size

您可以使用这些参数通过命令提示符启动程序 java -Xms64m -Xmx256m chat_program。这里 Xms64m = 64mb 初始堆大小和 Xmx256m = 256mb 最大堆大小