Java 如何设置JVM的最大内存使用量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1493913/
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
How to set the maximum memory usage for JVM?
提问by erotsppa
I want to limit the maximum memory used by the JVM. Note, this is not just the heap, I want to limit the total memory used by this process.
我想限制JVM使用的最大内存。注意,这不仅仅是堆,我想限制这个进程使用的总内存。
采纳答案by Prabhu R
use the arguments -Xms<memory>
-Xmx<memory>
. Use M
or G
after the numbers for indicating Megs and Gigs of bytes respectively. -Xms
indicates the minimum and -Xmx
the maximum.
使用参数-Xms<memory>
-Xmx<memory>
。在数字之后使用M
或G
分别表示字节的 Megs 和 Gigs。-Xms
表示最小值和-Xmx
最大值。
回答by jjnguy
You shouldn't have to worry about the stack leaking memory (it is highly uncommon). The only time you can have the stack get out of control is with infinite (or really deep) recursion.
您不必担心堆栈泄漏内存(这种情况非常罕见)。唯一能让堆栈失控的情况是无限(或非常深)递归。
This is just the heap. Sorry, didn't read your question fully at first.
这只是堆。抱歉,一开始没有完全阅读您的问题。
You need to run the JVM with the following command line argument.
您需要使用以下命令行参数运行 JVM。
-Xmx<ammount of memory>
Example:
例子:
-Xmx1024m
That will allow a max of 1GB of memory for the JVM.
这将为 JVM 提供最多 1GB 的内存。
回答by SmoothieMonster
The answer above is kind of correct, you can't gracefully control how much native memory a java process allocates. It depends on what your application is doing.
上面的答案是正确的,您无法优雅地控制 java 进程分配多少本机内存。这取决于您的应用程序在做什么。
That said, depending on platform, you may be able to do use some mechanism, ulimit for example, to limit the size of a java or any other process.
也就是说,根据平台,您可以使用某种机制(例如 ulimit)来限制 java 或任何其他进程的大小。
Just don't expect it to fail gracefully if it hits that limit. Native memory allocation failures are much harder to handle than allocation failures on the java heap. There's a fairly good chance the application will crash but depending on how critical it is to the system to keep the process size down that might still suit you.
如果达到该限制,请不要指望它会优雅地失败。本机内存分配失败比 java 堆上的分配失败更难处理。应用程序崩溃的可能性相当大,但这取决于对系统来说保持可能仍然适合您的进程大小的重要性。
回答by SmoothieMonster
The NativeHeap can be increasded by -XX:MaxDirectMemorySize=256M (default is 128)
NativeHeap 可以通过 -XX:MaxDirectMemorySize=256M 增加(默认为 128)
I've never used it. Maybe you'll find it useful.
我从来没有用过。也许你会发现它很有用。
回答by vsingh
If you want to limit memory for jvm (not the heap size ) ulimit -v
如果你想限制 jvm 的内存(不是堆大小) ulimit -v
To get an idea of the difference between jvm and heap memory , take a look at this excellent article http://blogs.vmware.com/apps/2011/06/taking-a-closer-look-at-sizing-the-java-process.html
要了解 jvm 和堆内存之间的区别,请查看这篇优秀文章 http://blogs.vmware.com/apps/2011/06/taking-a-closer-look-at-sizing-the- java-process.html