gpt4 book ai didi

java - 调用 JNI_CreateJavaVM 函数后应用程序内存增加了 Giga Bytes

转载 作者:行者123 更新时间:2023-11-30 17:27:28 25 4
gpt4 key购买 nike

在我的 C++ 应用程序中,我需要通过 JNI 调用“jar”文件内的一些 java 函数。

然而,在调用 JNI_CreateJavaVM 函数之后,我的应用程序内存增加了 3、4 GB。我怀疑这是由于 java VM 加载到我的应用程序中造成的。但是,3.4 GB 在我的环境中是 Not Acceptable 。有人可以告诉我有哪些参数可用于减少内存需求。

我的示例代码:

#include <jni.h>
#include <iostream>

using namespace std;

int main(const int argc, const char** argv)
{
JNIEnv* p_JNIEnv = NULL;

JavaVM* jvm;
JavaVMInitArgs args;
JavaVMOption options[1];

args.version = JNI_VERSION_1_2;
args.nOptions = 1;

const char* zSystemLibs = "/x01/nptsapp/sujith/ds3api";
if (!zSystemLibs)
{
cout << "SYSTEM_LIBS environment variable not set" << endl;
return false;
}

// ClientAPI, Security and DS3Authenticator class made by us
char zPath[1024];

snprintf(zPath, 1024, "-Djava.class.path=%s/clientapi.jar:%s/security-1.2.jar:%s/RSAPasswordEncryptLib.jar:%s", zSystemLibs,
zSystemLibs, zSystemLibs, zSystemLibs);

cout << "Class Path=" << zPath << endl;

options[0].optionString = zPath;
args.options = options;
args.ignoreUnrecognized = JNI_FALSE;

p_JNIEnv = NULL;

JNI_CreateJavaVM(&jvm, (void**)&p_JNIEnv, &args);

if (!p_JNIEnv)
{
cout << "JNIEnv Not Created" << endl;
return false;
}

cout << "JNIEnv Created Successful:" << p_JNIEnv << endl;

cout << getpid() << " Enter:";

int iValue;
cin >> iValue;

return 0;
};

编译运行

[sujith@nc-110 ~/ds3api]$ ./a.out 
Class Path=-Djava.class.path=/x01/nptsapp/sujith/ds3api/clientapi.jar:/x01/nptsapp/sujith/ds3api/security-1.2.jar:/x01/nptsapp/sujith/ds3api/RSAPasswordEncryptLib.jar:/x01/nptsapp/sujith/ds3api
JNIEnv Created Successful:0x10869e8
21152 Enter:

应用程序停止前的 pmap 输出。我添加了 cin 来停止应用程序,直到获得 pmap 输出。

[sujith@nc-110 ~]$ pmap -d 21152
21152: ./a.out
Address Kbytes Mode Offset Device Mapping
0000000000400000 4 r-x-- 0000000000000000 000:00019 a.out
0000000000601000 4 rw--- 0000000000001000 000:00019 a.out
000000000107d000 684 rw--- 0000000000000000 000:00000 [ anon ]
0000000721200000 21504 rw--- 0000000000000000 000:00000 [ anon ]
0000000722700000 62464 rw--- 0000000000000000 000:00000 [ anon ]
0000000726400000 148480 rw--- 0000000000000000 000:00000 [ anon ]
000000072f500000 2229760 rw--- 0000000000000000 000:00000 [ anon ]
00000007b7680000 74752 rw--- 0000000000000000 000:00000 [ anon ]
00000007bbf80000 1114624 rw--- 0000000000000000 000:00000 [ anon ]
0000003f62000000 128 r-x-- 0000000000000000 068:00002 ld-2.12.so
0000003f6221f000 4 r---- 000000000001f000 068:00002 ld-2.12.so
0000003f62220000 4 rw--- 0000000000020000 068:00002 ld-2.12.so
0000003f62221000 4 rw--- 0000000000000000 000:00000 [ anon ]
0000003f62400000 524 r-x-- 0000000000000000 068:00002 libm-2.12.so
0000003f62483000 2044 ----- 0000000000083000 068:00002 libm-2.12.so
0000003f62682000 4 r---- 0000000000082000 068:00002 libm-2.12.so
0000003f62683000 4 rw--- 0000000000083000 068:00002 libm-2.12.so
0000003f62800000 1628 r-x-- 0000000000000000 068:00002 libc-2.12.so
0000003f62997000 2048 ----- 0000000000197000 068:00002 libc-2.12.so
0000003f62b97000 16 r---- 0000000000197000 068:00002 libc-2.12.so
0000003f62b9b000 4 rw--- 000000000019b000 068:00002 libc-2.12.so
0000003f62b9c000 20 rw--- 0000000000000000 000:00000 [ anon ]
0000003f62c00000 8 r-x-- 0000000000000000 068:00002 libdl-2.12.so
0000003f62c02000 2048 ----- 0000000000002000 068:00002 libdl-2.12.so
0000003f62e02000 4 r---- 0000000000002000 068:00002 libdl-2.12.so
0000003f62e03000 4 rw--- 0000000000003000 068:00002 libdl-2.12.so
0000003f63000000 92 r-x-- 0000000000000000 068:00002 libpthread-2.12.so
0000003f63017000 2044 ----- 0000000000017000 068:00002 libpthread-2.12.so
0000003f63216000 4 r---- 0000000000016000 068:00002 libpthread-2.12.so
0000003f63217000 4 rw--- 0000000000017000 068:00002 libpthread-2.12.so
0000003f63218000 16 rw--- 0000000000000000 000:00000 [ anon ]
0000003f63800000 28 r-x-- 0000000000000000 068:00002 librt-2.12.so
0000003f63807000 2044 ----- 0000000000007000 068:00002 librt-2.12.so
0000003f63a06000 4 r---- 0000000000006000 068:00002 librt-2.12.so
0000003f63a07000 4 rw--- 0000000000007000 068:00002 librt-2.12.so
0000003f6a800000 88 r-x-- 0000000000000000 068:00002 libgcc_s-4.4.6-20110824.so.1
0000003f6a816000 2044 ----- 0000000000016000 068:00002 libgcc_s-4.4.6-20110824.so.1
0000003f6aa15000 4 rw--- 0000000000015000 068:00002 libgcc_s-4.4.6-20110824.so.1
0000003f6e000000 928 r-x-- 0000000000000000 068:00002 libstdc++.so.6.0.13
0000003f6e0e8000 2048 ----- 00000000000e8000 068:00002 libstdc++.so.6.0.13
0000003f6e2e8000 28 r---- 00000000000e8000 068:00002 libstdc++.so.6.0.13
0000003f6e2ef000 8 rw--- 00000000000ef000 068:00002 libstdc++.so.6.0.13
0000003f6e2f1000 84 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f10000000 132 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f10021000 65404 ----- 0000000000000000 000:00000 [ anon ]
00007f1f14000000 132 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f14021000 65404 ----- 0000000000000000 000:00000 [ anon ]
00007f1f18000000 132 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f18021000 65404 ----- 0000000000000000 000:00000 [ anon ]
00007f1f1c000000 132 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f1c021000 65404 ----- 0000000000000000 000:00000 [ anon ]
00007f1f20000000 132 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f20021000 65404 ----- 0000000000000000 000:00000 [ anon ]
00007f1f2616f000 96836 r---- 0000000000000000 068:00002 locale-archive
00007f1f2c000000 132 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f2c021000 65404 ----- 0000000000000000 000:00000 [ anon ]
00007f1f30000000 132 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f30021000 65404 ----- 0000000000000000 000:00000 [ anon ]
00007f1f34000000 132 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f34021000 65404 ----- 0000000000000000 000:00000 [ anon ]
00007f1f39090000 114112 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f40000000 132 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f40021000 65404 ----- 0000000000000000 000:00000 [ anon ]
00007f1f470ae000 4 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f470af000 4 ----- 0000000000000000 000:00000 [ anon ]
00007f1f470b0000 1024 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f471b0000 12 ----- 0000000000000000 000:00000 [ anon ]
00007f1f471b3000 1016 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f472b1000 12 ----- 0000000000000000 000:00000 [ anon ]
00007f1f472b4000 1016 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f473b2000 12 ----- 0000000000000000 000:00000 [ anon ]
00007f1f473b5000 1016 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f474b3000 12 ----- 0000000000000000 000:00000 [ anon ]
00007f1f474b6000 1016 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f475b4000 12 ----- 0000000000000000 000:00000 [ anon ]
00007f1f475b7000 1016 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f476b5000 12 ----- 0000000000000000 000:00000 [ anon ]
00007f1f476b8000 1016 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f477b6000 4 ----- 0000000000000000 000:00000 [ anon ]
00007f1f477b7000 8484 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f48000000 132 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f48021000 65404 ----- 0000000000000000 000:00000 [ anon ]
00007f1f4c08d000 2488 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f4c2fb000 1788 r--s- 00000000039e5000 068:00002 rt.jar
00007f1f4c4ba000 8408 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f4ccf0000 4 ----- 0000000000000000 000:00000 [ anon ]
00007f1f4ccf1000 1024 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f4cdf1000 4 ----- 0000000000000000 000:00000 [ anon ]
00007f1f4cdf2000 1068 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f4cefd000 120 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f4cf1b000 292 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f4cf64000 4356 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f4d3a5000 44 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f4d3b0000 120 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f4d3ce000 292 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f4d417000 4352 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f4d857000 148 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f4d87c000 2176 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f4da9c000 44 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f4daa7000 728 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f4db5d000 2496 rwx-- 0000000000000000 000:00000 [ anon ]
00007f1f4ddcd000 46656 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f50b5d000 104 r-x-- 0000000000000000 068:00002 libzip.so
00007f1f50b77000 2048 ----- 000000000001a000 068:00002 libzip.so
00007f1f50d77000 4 rw--- 000000000001a000 068:00002 libzip.so
00007f1f50d78000 44 r-x-- 0000000000000000 068:00002 libnss_ldap.so.2
00007f1f50d83000 2044 ----- 000000000000b000 068:00002 libnss_ldap.so.2
00007f1f50f82000 4 rw--- 000000000000a000 068:00002 libnss_ldap.so.2
00007f1f50f83000 48 r-x-- 0000000000000000 068:00002 libnss_files-2.12.so
00007f1f50f8f000 2048 ----- 000000000000c000 068:00002 libnss_files-2.12.so
00007f1f5118f000 4 r---- 000000000000c000 068:00002 libnss_files-2.12.so
00007f1f51190000 4 rw--- 000000000000d000 068:00002 libnss_files-2.12.so
00007f1f511a8000 164 r-x-- 0000000000000000 068:00002 libjava.so
00007f1f511d1000 2048 ----- 0000000000029000 068:00002 libjava.so
00007f1f513d1000 8 rw--- 0000000000029000 068:00002 libjava.so
00007f1f513d3000 52 r-x-- 0000000000000000 068:00002 libverify.so
00007f1f513e0000 2044 ----- 000000000000d000 068:00002 libverify.so
00007f1f515df000 8 rw--- 000000000000c000 068:00002 libverify.so
00007f1f515e1000 28 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f515f4000 32 rw-s- 0000000000000000 068:00002 21152
00007f1f515fc000 4 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f515fd000 4 r---- 0000000000000000 000:00000 [ anon ]
00007f1f515fe000 4 rw--- 0000000000000000 000:00000 [ anon ]
00007f1f515ff000 11712 r-x-- 0000000000000000 068:00002 libjvm.so
00007f1f5216f000 2044 ----- 0000000000b70000 068:00002 libjvm.so
00007f1f5236e000 788 rw--- 0000000000b6f000 068:00002 libjvm.so
00007f1f52433000 260 rw--- 0000000000000000 000:00000 [ anon ]
00007fffac53e000 12 ----- 0000000000000000 000:00000 [ anon ]
00007fffac542000 1008 rw--- 0000000000000000 000:00000 [ stack ]
00007fffac7ff000 4 r-x-- 0000000000000000 000:00000 [ anon ]
ffffffffff600000 4 r-x-- 0000000000000000 000:00000 [ anon ]
mapped: 4655428K writeable/private: 3860404K shared: 1820K
[sujith@nc-110 ~]$

最佳答案

我想,最明显的第一件事就是使用虚拟机的命令行选项来设置最大内存使用量。

This question有很多讨论(100%限制VM的内存使用并不容易),但最简单的尝试就是限制堆。添加 -Xmx100M 将堆最大值设置为 100 MB。

关于java - 调用 JNI_CreateJavaVM 函数后应用程序内存增加了 Giga Bytes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26378274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com