如何缩小java堆空间? [英] How to shrink java heap space?

查看:406
本文介绍了如何缩小java堆空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java控制台应用程序,它使用DOM处理大型xml文件。基本上它从数据库中获取的数据创建xml文件。
现在,你猜它正在使用大量的内存,但令我惊讶的是,它与糟糕的代码无关,而是与java堆空间不缩小有关。
我尝试使用这些JVM参数从Eclipse运行我的应用程序:

I have a Java console app that's processing big xml files using DOM. Basically it creates xml files from data it takes from the DB. Now, as you guess it's using large amount of memory but, to my surprise, it's not related to bad code but to "java heap space not shrinking". I tried running my app from Eclipse using these JVM params:

-Xmx700m -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20

我甚至添加了

-XX:-UseSerialGC

因为我发现了parallel GC忽略MinHeap和MaxHeap选项。即使我的应用程序内存使用的所有选项图如下所示:

as i found out that parallel GC ignores "MinHeap" and "MaxHeap" options. Even with all those options graph of my app's memory use looks like this:

正如您所看到的,我的应用程序在某一时刻需要大约400 MB的堆空间,堆增长到大约650 MB,但是几秒钟之后(当xml生成完成时)应用程序降至12MB的已用堆,但堆大小仍为~650 MB。我的RAM需要650 MB!这是不好的,你不觉得吗?

As you can see, at one point my app takes ~400 MB of heap space, heap grows to ~650 MB, but few seconds later (when xml generation is done) my app goes down to 12MB of used heap, but "heap size" remains at ~650 MB. It takes 650 MB of my ram! It's bizzare, don't you think?

**有没有办法强制JVS将可用堆大小缩小到,比如当前使用堆的150%?**就像,如果我的应用程序需要15 MB内存,堆大小约为20MB,当我的应用程序要求400 MB内存时,堆增长到~600 MB,并且当我的应用程序完成重装操作后,DROPS回到~20 MB ?

**Is there a way to force JVS to shrink availabe heap size to, like 150% of current used heap?**Like, if my app needs 15 MB of ram, heap size is ~20MB, when my app asks for 400 MB of ram, heap grows to ~600 MB and DROPS back to ~20 MB as soon as my app finish heavy-lifting operation?

推荐答案

您应该使用Parallel collection并使用-XX:-UseAdaptiveSizePolicy。文档适用于Java 1.5,但我找不到更新的内容。

You should probably use Parallel collection and use -XX:-UseAdaptiveSizePolicy. Docs are for Java 1.5 but I can't find anything more recent.


-XX:+ UseAdaptiveSizePolicy的实现默认使用$ b $ -bX:+ UseParallelGC垃圾收集器已更改为考虑三个
目标:

The implementation of -XX:+UseAdaptiveSizePolicy used by default with the -XX:+UseParallelGC garbage collector has changed to consider three goals:


  • 所需的最大GC暂停目标

  • 所需的应用程序吞吐量目标

  • 最小占用空间

实施检查(按此顺序):

The implementation checks (in this order):


  1. 如果GC暂停时间大于暂停时间目标,则减少代数为了更好地实现目标。

  2. 如果满足暂停时间目标,则考虑应用程序的吞吐量目标。如果未满足应用程序的吞吐量目标
    ,则将代数的大小增加到
    ,以更好地实现目标。

  3. 如果同时满足暂停时间目标和吞吐量目标,则减少代数的大小以减少占用空间。


编辑

根据OP的建议添加 - 。

Added "-" per OP's suggestion.

这篇关于如何缩小java堆空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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