java 指定内存,如何计算(并指定)java进程允许的总内存空间?

博客讨论了如何在Linux环境下精确控制Java进程的内存使用,包括最大堆内存、永久区大小和栈空间设置。当JVM选项设置为-Xms512m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=512m时,进程占用1.6GB内存。问题涉及最大内存计算公式、Linux下的 fork 行为及确保不超过1.5GB内存使用的选项。建议降低 PermSize 和栈大小,使用如-XX:MaxPermSize=384m -XX:ThreadStackSize=128k -XX:+UseCompressedOops等选项来更接近目标限制。

I have a system which cannot provide more than 1.5 Gb for Java process. Thus i need an exact way to specify java process settings, including all memory kinds inside java and possible fork.

One specific java process and system to illustrate my problem:

My current environment is java 1.6.0_18 under Ubuntu Linux 9.10.

I start large java server process with following JVM Options:

"-Xms512m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=512m"

Now, "top" command reports that the process uses 1.6gb memory...

Questions:

1 - how the maximal space used by java process is calculated? Please provide exact formula if possible.

( Smth. Like: max.heap + max.perm + stack + jvm space = maximal space )

2 - what is the infamous fork behavior under linux in my case? Will the forked JVM occupy extra 1.6 gb (resulting in total 3.2 Gb of used memory)?

3 - Which options must be used to absolutely ensure that no more than 1.5gb is used at any time?

thank you

@rancidfishbreath: "ulimit" will ensure that java cannot take more than specified amount of memory. My purpose is to ensure that java doesn't ever try to do that.

解决方案

top reports 1.6GB because PermSize is ON TOP of the heap-size maximum heap size. In your case you set MaxPermSize to 512m and Xmx to 1024m. This amounts to 1536m. Just like in other languages, an absolutely precise number can not be calculated unless you know precisely how many threads are started, how many file handles are used, etc. The stack size per thread depends on the OS and JDK version, in your case its 1024k (if it is a 64bit machine). So if you have 10 threads you use 10240k extra as the stack is not allocated from the heap (Xmx). Most applications that behave nicely work perfectly when setting a lower stack and MaxPermSize. Try to set the ThreadStackSize to 128k and if you get a StackOverflowError (i.e. if you do lots of deep recursions) you can increase it in small steps until the problem disappears.

So my answer is essentially that you can not control it down to the MB how much the Java process will use, but you come fairly close by setting i.e. -Xmx1024m -XX:MaxPermSize=384m and -XX:ThreadStackSize=128k -XX:+UseCompressedOops. Even if you have lots of threads you will still have plenty of headroom until you reach 1.5GB. The UseCompressedOops tells the VM to use narrow pointers even when running on a 64bit JVM, thus saving some memory.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值