Why does the native library use 1.5 times more memory when using java than when using C-program under Linux?
I wrote a library in C, which consumed a lot of memory (millions of small pieces) I wrote a program that uses this library I wrote a java program using the same library Java programs are very thin layers around libraries Basically, there is only one called native method, all work and return after a few hours Using the Java call interface, there is no further communication between Java and the native library No Java objects consume a lot of memory
So C programs are very similar to Java programs The entire computation / memory allocation takes place in the native library Still When executed, the C program consumes 3gb of memory But Java programs consume 4.3GB! (virt amount reported at the top)
I checked the memory mapping of the java process (using PMAP) The library only uses 40MB Therefore, other libraries loaded by Java are not the reason
Does anyone have an explanation for this behavior?
Editor: Thank you for your answers so far To make it clearer, java code does nothing but call the native library! The Java heap is standard size (possibly 60MB) and unused (except for one class that contains the main method and another class that calls the native library)
The native library method is a long-running method and can execute a large number of malloc and frees Fragmentation is also an explanation I came up with However, since no java code is active, the fragmentation behavior of Java programs and C programs should be the same Because it is different, I also assume that the malloc implementation used when running in C programs or Java programs is different
Solution
Guess: when running in the JVM, you may be using a non default malloc implementation, which can adjust the specific requirements of the JVM and incur more overhead than the general malloc in the ordinary libc implementation