简体   繁体   English

JNI,如何在C运行时在JVM之外创建大量对象。

[英]JNI, how creating a large amount of objects at C runtime, outside the JVM.

I am currently developing a Java/JNI library to be able to use a C library within Java. 我目前正在开发Java / JNI库,以便能够在Java中使用C库。

In a sight, 看来

  • I pass a file (path/filename) from Java to C, 我将文件(路径/文件名)从Java传递到C,
  • the C library opens and reads it, C库打开并读取它,
  • the C library performs a set of memory allocations in order to work with the content of the file, C库执行一组内存分配,以便处理文件的内容,
  • the C library is then requestable based on its (kind of) dictionary. 然后可根据其(种类)字典请求C库。

My issue is the following: 我的问题如下:

The loaded file is about 200mo (2.000.000 lines), and its representation in the C library takes more or less the same room. 加载的文件大约200mo(2.000.000行),并且在C库中的表示形式或多或少占用相同的空间。 All the content of the file must be stored in RAM as a lot of transformation is done by the library. 该文件的所有内容都必须存储在RAM中,因为该库完成了很多转换。

But, when the C library malloc objects, after a certain number (overall 5000 lines), I got a problem because of the RAM consumption. 但是,当C库malloc对象达到一定数目(总共5000行)后,由于RAM消耗,我遇到了问题。 It seems, the C library performs malloc using the JVM (allocated) memory. 看来,C库使用JVM(已分配)内存执行malloc。

When I run the same execution using a C runnable (and then calling the C library), all is ok, the 2.000.000 of lines are loaded and the library works properly. 当我使用C runnable运行相同的执行(然后调用C库)时,一切正常,将加载2.000.000行,并且库正常运行。

So, it is neither a problem of communication between Java and C nor a problem of passing large objects between the two language, it is a problem of large amount of memory allocations in C when using JNI framework. 因此,这既不是Java与C之间的通信问题,也不是在两种语言之间传递大对象的问题,而是使用JNI框架时C中的大量内存分配的问题。

Any suggestion? 有什么建议吗? BRgds, Eddy. BRgds,埃迪。

So far I know, The objects initialized from C are not initialized in the JVM heap. 到目前为止,我知道,从C初始化的对象未在JVM堆中初始化。 They are initialized in the C native heap. 它们在C本机堆中初始化。

As you asked for suggestions, I think loading 2 million lines in RAM is a huge task to do. 当您提出建议时,我认为在RAM中加载200万行是一项艰巨的任务。 You may want to split the file into smaller files and process them one by one. 您可能需要将文件拆分为较小的文件,然后逐一处理它们。

I suggest to create executable wrapper for your C library. 我建议为您的C库创建可执行包装。 In this case standalone executable application will run in another process with own memory space (size depends on operating system). 在这种情况下,独立的可执行应用程序将在另一个具有自己的内存空间(大小取决于操作系统)的进​​程中运行。
Next, you can run this executable using ProcessBuilder : 接下来,您可以使用ProcessBuilder运行此可执行文件:

 ProcessBuilder pb = new ProcessBuilder(
                                path to you executable, 
                                path to processed file, 
                                path to out file);
 Map<String, String> env = pb.environment();
 env.put("VAR1", "myValue");
 env.remove("OTHERVAR");
 env.put("VAR2", env.get("VAR1") + "suffix");
 pb.directory(new File("myDir"));
 Process p = pb.start();

In exemple above executable process will write (return) result via out file . 在上述示例中,可执行程序将通过out文件写入(返回)结果。 Also, you can pass socket address , http url , etc. as command argument to set destination where your java application will obtain result. 另外,您可以将套接字地址http url等作为命令参数传递给设置Java应用程序将获得结果的目的地。

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

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