有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java告诉JVM使用了多少本机内存

我的问题与Java-Native-Code-Background密切相关:

You cannot control native memory from Java really well. You might have heard about native buffers, but what they haven’t told you is that Java doesn’t properly garbage native buffers. The problem is that Java doesn’t take the size of the native buffers into account, meaning that it will at some point free a native buffer, but if you have really many of those, then it probably won’t be before you run out of memory. This means that all data has to be stored in Java objects.

因此,为了告诉JVM它应该运行GC,是否可能以某种方式告知或注册使用的本机内存量? 如果我理解正确,那么Dalvik does this,但是在所有JVM上都有一个可靠的方法这样做会很好

谢谢


共 (1) 个答案

  1. # 1 楼答案

    你无法告诉JVM使用了多少本机内存,JVM根本没有任何接口。但如果你只是不想因为JVM没有注意到你使用了多少内存而耗尽内存,我可以想出两种方法:

    1. 每当您认为JVM应该运行时,强制使用^{}运行GCSmarter people than me are suggesting it.别忘了GC会影响JVM性能
    2. 为应用程序的峰值总使用量分配一个足够大的直接缓冲区,使其持久化,并对其进行自己的内存管理This is suggested by the NIO doc itself,因为直接缓冲区的de/alloc成本较高。重复使用它。在大缓冲区的偏移处读写部分字节缓冲区