如何计算(并指定)java进程允许的总内存空间? [英] How to calculate (and specify) the total memory space allowed for java process?

查看:187
本文介绍了如何计算(并指定)java进程允许的总内存空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不能为Java进程提供超过1.5 Gb的系统.因此,我需要一种精确的方法来指定Java进程设置,包括Java内部的所有内存类型以及可能的fork.

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.

一个特定的Java流程和系统来说明我的问题:

One specific java process and system to illustrate my problem:

我当前的环境是Ubuntu Linux 9.10下的Java 1.6.0_18.

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

我使用以下JVM选项启动大型Java服务器进程: "-Xms512m -Xmx1024m -XX:PermSize = 256m -XX:MaxPermSize = 512m"

I start large java server process with following JVM Options: "-Xms512m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=512m"

现在,"top"命令报告该进程使用了​​1.6gb内存...

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

问题:

1-如何计算Java进程使用的最大空间?如果可能,请提供确切的公式. (Smth.like:max.heap + max.perm + stack + jvm space = maximum space)

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-就我而言,在Linux下臭名昭著的fork行为是什么?分叉的JVM是否会占用额外的1.6 gb(总共3.2 Gb的已用内存)?

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-必须使用哪些选项来绝对确保在任何时候使用的容量都不超过1.5gb?

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

谢谢

@rancidfishbreath:"ulimit"将确保Java占用的内存不能超过指定的数量.我的目的是确保Java永远不会尝试这样做.

@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 报告1.6GB,因为PermSize是堆大小最大堆大小的ON TOP.在您的情况下,将MaxPermSize设置为512m,将Xmx设置为1024m.总计1536m.就像在其他语言中一样,除非您确切知道启动了多少线程,使用了多少文件句柄等,否则无法计算出绝对精确的数字.每个线程的堆栈大小取决于OS和JDK版本,在这种情况下,其大小取决于操作系统和JDK版本. 1024k(如果是64位计算机).因此,如果您有10个线程,则将额外使用10240k,因为堆栈不是从堆(Xmx)中分配的.设置较低的堆栈和MaxPermSize时,大多数表现良好的应用程序都能完美运行.尝试将ThreadStackSize设置为128k,如果遇到StackOverflowError(即,如果进行了很多深度递归),则可以逐步增加它,直到问题消失.

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.

因此,我的回答基本上是您无法将其控制在MB范围内,Java进程将使用多少内存,但是您可以通过设置-Xmx1024m -XX:MaxPermSize = 384m和-XX:ThreadStackSize = 128k-来达到相当接近的效果- XX:+ UseCompressedOops.即使您有很多线程,在达到1.5GB之前,您仍然会有很多的余量. UseCompressedOops告诉VM即使在64位JVM上运行时也要使用狭窄的指针,从而节省了一些内存.

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.

这篇关于如何计算(并指定)java进程允许的总内存空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆