gpt4 book ai didi

使用App时Java内存使用量增加,但不使用时不减少

转载 作者:IT王子 更新时间:2023-10-28 23:33:22 26 4
gpt4 key购买 nike

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

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

System.gc 不工作。

最佳答案

正如评论中所指出的,并不确定垃圾收集器在处理对象时是否会将内存归还给系统。

也许 Tuning Garbage Collection Outline为您的问题提供解决方案:

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 - 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 捆绑的工具)中监视堆的方式、对象和位置来确定对象泄漏的方式、内容和位置。您可以通过该程序执行堆转储并获得对象内存消耗的直方图:

enter image description here

关于使用App时Java内存使用量增加,但不使用时不减少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3022427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com