简体   繁体   English

我可以获得我的程序剩余多少内存?

[英]Can I get how much memory for my program remains?

My program has many works that need a lot of memory that I can't exactly know when I need to stop it, but in case there's very few memory left, I can force it stop using resources. 我的程序有许多需要大量内存的工作,当我需要停止它时我无法确切知道,但是如果内存很少,我可以强制它停止使用资源。 So can I get how many remaining (in byte) memory that my program can use? 那么我可以获得我的程序可以使用多少剩余(字节)内存?

P/s: There's NO way to release the process memory. P / s:没有办法释放进程内存。 They need memory, as much as possible, and that is how it works (and, no trash for collector, since old ones still be need). 他们需要尽可能多的内存,这就是它的工作方式(而且,收藏者没有垃圾,因为旧的仍然需要)。

Try something like: 尝试类似的东西:

Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo();
Debug.getMemoryInfo(memoryInfo);

String memMessage = String.format("Memory: Pss=%.2f MB,
    Private=%.2f MB, Shared=%.2f MB",
    memoryInfo.getTotalPss() / 1000,
    memoryInfo.getTotalPrivateDirty() / 1000,
    memoryInfo.getTotalSharedDirty() / 1000);

You can read more at this blog: http://huenlil.pixnet.net/blog/post/26872625 您可以在此博客上阅读更多内容: http//huenlil.pixnet.net/blog/post/26872625

public static long getCurrentFreeMemoryBytes() {
     long heapSize =  Runtime.getRuntime().totalMemory();
     long heapRemaining = Runtime.getRuntime().freeMemory();   
     long nativeUsage  =  Debug.getNativeHeapAllocatedSize();

     return Runtime.getRuntime().maxMemory() - (heapSize - heapRemaining) - nativeUsage;    
}

While not perfect it should do the trick for the most part. 虽然不完美,但它应该在大多数情况下完成。

http://www.javaspecialists.eu/archive/Issue029.html http://www.exampledepot.com/egs/java.lang/GetHeapSize.html

在此处查看Android为内存跟踪提供的工具。

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

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