简体   繁体   English

Java JNA包装器和内存消耗

[英]Java JNA wrapper and memory consumption

I have a question how JVM processes with JNA wrappers show memory consumption. 我有一个疑问,带有JNA包装器的JVM进程如何显示内存消耗。

For example I run Java application which uses OpenCV JNA wrapper. 例如,我运行使用OpenCV JNA包装器的Java应用程序。 Java application itself consumes for example 1GB of RAM and OpenCV native libs consume 3GB of RAM. Java应用程序本身消耗例如1GB的RAM,而OpenCV本机库消耗3GB的RAM。 So totally, when I will find appropriate Java JVM process(via ps command) it will show 1GB of RAM or 4GB(1 + 3) of RAM? 所以总的来说,当我找到合适的Java JVM进程(通过ps命令)时,它将显示1GB的RAM或4GB(1 + 3)的RAM?

Your ps output will show 1GB for RSS (the Resident Set Size -- how much memory is allocated to that process and is in RAM.) The Java process will not directly show the used Native memory; 您的ps输出将显示1GB的RSS(“驻留集大小-分配给该进程并在RAM中的内存量”。)Java进程不会直接显示已用的本机内存; however, it will appear as a portion of the VSZ (Virtual Memory Size -- all memory that the process can access, including memory that is swapped out, memory that is allocated, but not used, and memory that is from shared libraries.) 但是,它将显示为VSZ的一部分(虚拟内存大小-进程可以访问的所有内存,包括换出的内存,已分配但未使用的内存以及来自共享库的内存。)

For example, I wrote the following code: 例如,我编写了以下代码:

import com.sun.jna.Memory;

public class TinyJavaBigC {
    public static void main(String[] args) {
        // Grab 1 GiB of memory
        Memory buf = new Memory(1 << 30);
        // Sleep long enough to grab ps
    }
}

Regardless of the amount of Native memory reserved using new Memory(bytes) (which effectively calls malloc ) the Java application consistently used the same amount memory in RSS, and limiting the Java heap size using -Xmx did not prevent allocation of native memory beyond this limit. 不管使用new Memory(bytes)保留了多少本机内存(有效调用malloc ),Java应用程序始终在RSS中使用相同数量的内存,并且使用-Xmx限制Java堆大小并不会阻止超出此范围的本机内存分配限制。 The 1 GiB of native memory clearly disappeared from the OS's "available" memory, however. 但是,本机内存的1 GiB显然已从操作系统的“可用”内存中消失了。

I placed the above code in a loop incrementing the shift left value for allocation, and ran it using -Xmx512m which should have limited JVM heap to 512 MiB. 我将上面的代码放在循环中以增加左移值以进行分配,并使用-Xmx512m对其进行了-Xmx512m ,该-Xmx512m应将JVM堆限制为512 MiB。 The RSS, which includes all JVM Stack and Heap Memory, remained in the ~50 MiB range. RSS(包括所有JVM堆栈和堆内存)保持在大约50 MiB范围内。 The allocated memory does show up in VSZ associated with the process. 分配的内存确实显示在与该进程关联的VSZ中。 As this also includes other types of memory it's not a direct measure, and vastly exceeds the available RAM and swapfile size constraints but it does at least give some indication of increased allocation. 由于这还包括其他类型的内存,因此这不是直接的衡量标准,并且大大超出了可用RAM和交换文件大小的限制,但至少确实表明了分配增加的迹象。

Native Memory          RSS          VSZ
       1 byte     42.0 MiB      9.6 GiB
      2 bytes     45.8 MiB      9.6 GiB
      4 bytes     46.0 MiB      9.6 GiB
      8 bytes     46.1 MiB      9.6 GiB
     16 bytes     46.3 MiB      9.6 GiB
     32 bytes     46.5 MiB      9.6 GiB
     64 bytes     47.0 MiB      9.6 GiB
    128 bytes     47.5 MiB      9.6 GiB
    256 bytes     47.6 MiB      9.6 GiB
    512 bytes     48.9 MiB      9.6 GiB
        1 KiB     49.1 MiB      9.6 GiB
        2 KiB     49.2 MiB      9.6 GiB
        4 KiB     49.3 MiB      9.6 GiB
        8 KiB     49.3 MiB      9.6 GiB
       16 KiB     49.8 MiB      9.6 GiB
       32 KiB     50.1 MiB      9.6 GiB
       64 KiB     50.1 MiB      9.6 GiB
      128 KiB     50.6 MiB      9.6 GiB
      256 KiB     51.4 MiB      9.6 GiB
      512 KiB     51.3 MiB      9.6 GiB
        1 MiB     51.4 MiB      9.6 GiB
        2 MiB     51.4 MiB      9.6 GiB
        4 MiB     51.4 MiB      9.6 GiB
        8 MiB     51.4 MiB      9.6 GiB
       16 MiB     51.3 MiB      9.7 GiB
       32 MiB     51.3 MiB      9.7 GiB
       64 MiB     51.7 MiB      9.8 GiB
      128 MiB     51.7 MiB      9.9 GiB
      256 MiB     51.6 MiB     10.1 GiB
      512 MiB     51.6 MiB     10.5 GiB
        1 GiB     51.7 MiB     11.3 GiB
        2 GiB     51.8 MiB     12.8 GiB
        4 GiB     51.9 MiB     15.8 GiB
        8 GiB     51.9 MiB     21.8 GiB
       16 GiB     52.0 MiB     33.8 GiB
       32 GiB     52.0 MiB     57.8 GiB
       64 GiB     52.1 MiB    105.8 GiB
      128 GiB     52.1 MiB    201.8 GiB
      256 GiB     52.5 MiB    393.8 GiB
      512 GiB     52.6 MiB    777.8 GiB
        1 TiB     52.7 MiB      1.5 TiB
        2 TiB     52.7 MiB      3.0 TiB
        4 TiB     52.8 MiB      6.0 TiB
        8 TiB     52.9 MiB     12.0 TiB
       16 TiB     53.1 MiB     24.0 TiB
       32 TiB     53.2 MiB     48.0 TiB
Exception in thread "main" java.lang.OutOfMemoryError: Cannot allocate 70368744177664 bytes

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

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