如何确定要为java堆分配多少物理内存? [英] How is it decided that how much physical memory is to be allocated to java heap?

查看:558
本文介绍了如何确定要为java堆分配多少物理内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有16G RAM的机器.我运行带有参数-Xms9G -Xmx9G的Java应用程序. 当我运行top命令时,我看到我的Java进程正在使用 13.8g VIRT ,但是只有 4.6g RES .

I have machine with 16G RAM. I run a java application with arguments -Xms9G -Xmx9G. When I run top command I see that my java process is taking 13.8g VIRT, but only 4.6g of RES.

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 5019 root      20   0 13.8g 4.7g  18m S  0.7 30.7   3:28.39 java                                     

在运行pmap命令时,我看到只有〜3.9g 堆作为 RES 存在,其余的 5.7g位于虚拟中>.

On running pmap command, I see that only ~3.9g of heap is present as RES, rest 5.7g is in virtual.

Address           Kbytes     RSS   Dirty Mode   Mapping
0000000580000000 9452384 4074228 4074228 rw---    [ anon ]

jvmtop监视 HPCUR 时,我观察到 HPCUR达到约3g 时会触发GC.

Upon monitoring HPCUR with jvmtop, I observe that the GC is triggered when the HPCUR reaches about 3g.

PID MAIN-CLASS        HPCUR HPMAX NHCUR NHMAX    CPU     GC    VM USERNAME   #T DL
 5019 .1-SNAPSHOT.jar  408m 9216m  192m   n/a  0.25%  0.00% O8U20   webapp  823

我观察到该进程的 RES逐渐增加,RES中的堆内存(通过pmap)也逐渐增加.结果,GC阈值增加.

I observe that the RES for the process increases gradually, heap memory in RES (by pmap) also increases gradually too. As a result GC threshold increases.

我对此行为有几个疑问.

I have several questions about this behavior.

  1. 是否仅使用RES中存在的堆,而不使用VIRT?
  2. 如果我分配了9G的最小堆(-Xms),那么最初为什么只分配3.9g RES.这与保持-Xms低不一样吗?那么,保持-Xms = -Xmx的意义何在?
  3. 在什么基础上决定RES中应该有多少堆?阅读某处由操作系统管理的地方,但有任何粗略的逻辑吗?
  4. 有什么方法可以确保分配的堆已被实际使用?
  1. Is only the heap that is present in RES used, and not the VIRT?
  2. If I have allotted 9G min heap (-Xms), then initially why only 3.9g RES is allotted. Isn't this same as keeping -Xms low? What is the point of keeping -Xms=-Xmx then?
  3. On what basis is it decided that how much heap should be in RES? Read somewhere that its managed by OS, but any rough logic?
  4. Any way to make sure, that the allotted heap is actually used?

推荐答案

  1. HEAP是RES(只要它是可以放入主存储器中)
  2. 此答案表明-Xms在什么时候是GC的提示一个完整的垃圾收集是必要的.使用本地JVM进行测试,我可以确认它不会立即保留内存.您得到的是,您的JVM不会阻塞大量未使用的内存,但是随着GC的运行减少,它会更快地达到下界.
  3. 由1解答:RES始终包含所有HEAP
  4. 我的理解是,您的选择将实现您的预​​期目标.您的应用程序将获取9GB的内存,然后更频繁地开始垃圾回收.还有其他选项,例如MinHeapFreeRatio和取决于使用的垃圾收集器的选项您需要更多优化.
  1. Yes HEAP is RES (as long as it fits in main memory)
  2. This answer suggests that -Xms is a hint to GC at what point a full garbage collection is necessary. Testing with my local JVM I can confirm that it does not immediately reserve the memory. What you gain is that your JVM does not block a lot of unused memory but will reach you lower bound faster as there are less GC runs.
  3. Answered by 1. : RES always contains all of HEAP
  4. My understanding is that your options will achieve what you intend to. Your application will grab 9GB of memory and then start garbage collection more frequently. There are other options like MinHeapFreeRatio and options that depend on the gargbage collector used if you need more optimization.

您是否打印了GC日志并验证是否有问题?如果您看到在达到9G之前在应用程序启动时运行了很多GC,那么我会进一步检查.如果在达到9G之前几乎没有运行任何GC,我会说一切都很好.

Did you print GC logs and verify that something is wrong? If you see a lot of GC runs on application startup before 9G is reached I would look further. If there is hardly any GC run before hitting 9G I would say everything is fine.

这篇关于如何确定要为java堆分配多少物理内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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