简体   繁体   English

查找内存使用量峰值

[英]Find Memory Usage Peaks

I am having an issue with my java game, the memory being used by it is jumping a lot. 我的Java游戏出现问题,它使用的内存跳了很多。 I have this method created to find how much memory is being used: 我创建了此方法来查找正在使用的内存量:

public String readable(long bytes, boolean si) {
    int unit = si ? 1000 : 1024;
    if (bytes < unit) {
        return bytes + " B";
    }
    int exp = (int) (Math.log(bytes) / Math.log(unit));
    String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
    return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}

readable(Runtime.getRuntime().totalMemory(), true)

It starts off at 200mb initially, them jumps to 600m then progressively increases. 它最初从200mb开始,然后跳到600m,然后逐渐增加。 How would I monitor why this is happening and look for memory leaks that cause this? 我将如何监视这种情况的发生原因并寻找导致这种情况的内存泄漏? Is there a plugin on eclipse that I can install to view memory usage and resources? 我可以在eclipse上安装一个插件来查看内存使用情况和资源吗?

ideally you should not check this. 理想情况下,您不应对此进行检查。 The better way of doing this would be to see the heap dump. 更好的方法是查看堆转储。 If you are using eclipse, you can download MAT a link ! 如果您使用的是eclipse,则可以下载MAT 链接

totalMemory counts both reachable and unreachable objects. totalMemory计算可访问对象和不可访问对象。 Interpreting the behavior of this quantity requires a lot of understanding of garbage collection. 解释此数量的行为需要对垃圾收集有很多了解。

I suggest you use VisualVM instead. 我建议您改用VisualVM。 Install the VisualGC plugin and watch your application's memory, divided into GC generations, shrink and grow dynamically. 安装VisualGC插件,并观察应用程序的内存(分为GC代),动态收缩和增长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM