使用App时Java内存使用量会增加,但在不使用时不会减少 [英] Java memory usage increases when App is used, but doesnt decrease when not being used

查看:327
本文介绍了使用App时Java内存使用量会增加,但在不使用时不会减少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java应用程序,在使用时会占用大量内存,但是当程序没有被使用时,内存使用量不会下降。

I have a java application that uses a lot of memory when used, but when the program is not being used, the memory usage doesnt go down.

是否有一种强制Java释放内存的方法?因为当时不需要这个内存,我可以理解保留少量内存,但Java只保留它曾经使用过的所有内存。它也会在以后重用这个内存,但是必须有一种方法可以强制Java在不需要时释放它。

Is there a way to force Java to release this memory? Because this memory is not needed at that time, I can understand to reserve a small amount of memory, but Java just reserves all the memory it ever uses. It also reuses this memory later but there must be a way to force Java to release it when its not needed.

System.gc无效。

System.gc is not working.

推荐答案

正如评论中所指出的那样,当垃圾收集器处理对象时,它不能确定它会给系统回馈内存。

As pointed out in the comments, it's not certain that, while the garbage collector disposes objects, it gives back memory to the system.

也许调整垃圾收集大纲提供了解决问题的方法:

Perhaps Tuning Garbage Collection Outline provides the solution to your problem:


默认情况下,JVM会在每个GC上增大或缩小堆,以保持可用空间与活动对象的比率指定范围内的每个集合。

By default the JVM grows or shrinks the heap at each GC to keep the ratio of free space to live objects at each collection within a specified range.


  • -XX:MinHeapFreeRatio - 当百分比时如果一代人的自由空间低于这个值,那么这一代人将会扩大以满足这个百分比。默认值为40

  • -XX:MaxHeapFreeRatio - 当一代中的可用空间百分比超过此值时,生成将缩小以满足这个价值。默认值为70

  • -XX:MinHeapFreeRatio - when the percentage of free space in a generation falls below this value the generation will be expanded to meet this percentage. Default is 40
  • -XX:MaxHeapFreeRatio - when the percentage of free space in a generation exceeded this value the generation will shrink to meet this value. Default is 70






否则,如果您怀疑您正在泄漏引用,您可以弄清楚对象泄漏的方式,内容和位置是监视JVisualVM(与标准SDK捆绑在一起的工具)中的堆。您可以通过此程序执行堆转储并获取对象内存消耗的直方图:


Otherwise, if you suspect that you're leaking references you can figure out how, what and where objects are leaked is to monitor the heap in JVisualVM (a tool bundled with the standard SDK). You can, through this program, perform a heap-dump and get a histogram over object memory consumption:

这篇关于使用App时Java内存使用量会增加,但在不使用时不会减少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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