简体   繁体   English

JAVA、JNI 和 C 应用程序的内存占用问题

[英]Memory footprint issues with JAVA, JNI, and C application

I have a piece of an application that is written in C, it spawns a JVM and uses JNI to interact with a Java application.我有一个用 C 编写的应用程序,它生成一个 JVM 并使用 JNI 与 Java 应用程序交互。 My memory footprint via Process Explorer gets upto 1GB and runs out of memory.我通过 Process Explorer 占用的内存高达 1GB 并且内存不足。 Now as far as I know it should be able to get upto 2GB.现在据我所知,它应该能够达到 2GB。 One thing I believe is that the memory the JVM is using isn't visible in the Process Explorer.我相信的一件事是 JVM 使用的内存在 Process Explorer 中不可见。 My xmx is set to 256, I added some statements to watch the java side memory and it is peaking at 256 and GC is doing its job and it is all good on that side.我的 xmx 设置为 256,我添加了一些语句来观察 java 端内存,它在 256 处达到峰值,GC 正在完成它的工作,并且在这方面一切都很好。 So my question is, where is the other 700+ MB being consumed?所以我的问题是,其他 700+ MB 的消耗在哪里? Anyone out there a Java/JNI/C Memory expert?有人是 Java/JNI/C 内存专家吗?

There could be a leak in the JNI code. JNI 代码中可能存在泄漏。

Remember to use (*jni)->DeleteLocalRef() for any object references you get once you are done with them.记住对完成后获得的任何对象引用使用 (*jni)->DeleteLocalRef()。 If you use any native C buffers to create new Java objects, make sure you free them off once the object is created.如果您使用任何本机 C 缓冲区来创建新的 Java 对象,请确保在创建对象后将它们释放。 Check the JNI Specification for further guidelines.查看 JNI 规范以获取更多指南。

Depending on the VM you are using you might be able to turn on JNI checking.根据您使用的虚拟机,您可能能够打开 JNI 检查。 For example, on the IBM JDK you can specify "-Xcheck:jni".例如,在 IBM JDK 上,您可以指定“-Xcheck:jni”。

Try a test app in C that doesn't spawn the JVM but instead tries to allocate more and more memory.尝试使用 C 语言编写的测试应用程序,该应用程序不会生成 JVM,而是尝试分配越来越多的内存。 See whether the test app can reach the 2 GB barrier.查看测试应用程序是否可以达到 2 GB 的障碍。

编写一个 C 测试工具并使用 valgrind/alleyoop 检查 C 代码中的泄漏,并类似地使用 java jvisualvm 工具。

The C and JNI code can allocate memory as well (malloc/free/new/etc), which is outside of the VM's 256m. C 和 JNI 代码也可以分配内存(malloc/free/new/etc),这在 VM 的 256m 之外。 The xMX only restricts what the VM will allocate itself. xMX 仅限制 VM 将分配给自己的内容。 Depending on what you're allocating in the C code, and what other things are loaded in memory you may or may not be able to get up to 2GB.根据您在 C 代码中分配的内容,以及内存中加载的其他内容,您可能会或可能无法获得最多 2GB 的空间。

If you say that it's the Windows process that runs out of memory as opposed to the JVM, then my initial guess is that you probably invoke some (your own) native methods from the JVM and those native methods leak memory.如果您说是 Windows 进程耗尽内存而不是 JVM,那么我最初的猜测是您可能从 JVM 调用了一些(您自己的)本机方法,而这些本机方法会泄漏内存。 So, I concur with @John Gardner here.所以,我在这里同意@John Gardner。

Well thanks to all of your help especially @alexander I have discovered that all the extra memory that isn't visible via Process Explorer is being used by the Java Heap.好吧,感谢您的所有帮助,尤其是@alexander,我发现 Java 堆正在使用通过 Process Explorer 不可见的所有额外内存。 In fact via other tests that I have run the JVM's memory consumption is included in what I see from the Process Explorer.事实上,通过我运行的其他测试,JVM 的内存消耗包含在我从 Process Explorer 看到的内容中。 So the heap is taking large amounts of memory, I will have to do some more research about that and maybe ask a separate question.所以堆占用了大量内存,我将不得不对此进行更多研究,并且可能会提出一个单独的问题。

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

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