避免内存泄漏的JNI C到/从Java

发现了一种从C复杂的模型(与子对象和使用对象的项目ArrayList对象)返回到Java。 但我有一个问题:内存泄漏。

目前,我工作的代码分析(基于C)的数据,并应返回解析为一个Java类的数据。

主要的Java类中含有较多的Java类(整数,字符串,布尔,OBJ,对象的ArrayList),和我使用这个模式的对象实例返回解析的数据:

// Instance:LyricsLineVo jclass findClsLyricsLine = (*env)->FindClass(env, "com/example/jni/vo/LyricsLineVo"); jmethodID initLyricsLine = (*env)->GetMethodID (env, findClsLyricsLine, initMethod,"()V"); jobject objLyricsLine = (*env)->NewObject(env, findClsLyricsLine, initLyricsLine); jclass classLyricsLine = (*env)->GetObjectClass(env, objLyricsLine);

代码太长,我不知道我是否应该其他对象分开到另一个功能,只是解析数据传递给每个不同的功能,我认为它会占用更多的内存,我已经对释放用过的问题。

我读过关于如何释放空间的一些方法:

使用JNI内存泄漏检索Java代码字符串的值

使用JNI从C调用Java代码时内存泄漏

但运用这些是不够的。 它只能允许一个数据解析但是在那之后,我的程序崩溃。

是否有可以减少内存使用任何其它的推荐码?

我下面将我的错误日志:

07-11 19:23:31.640: WARN/dalvikvm(301): ReferenceTable overflow (max=512) 07-11 19:23:31.650: WARN/dalvikvm(301): Last 10 entries in JNI local reference table: 07-11 19:23:31.650: WARN/dalvikvm(301): 502: 0x44df0930 cls=Ljava/lang/Class; 'Lcom/demo/project/vo/DurationVo;' (172 bytes) 07-11 19:23:31.650: WARN/dalvikvm(301): 503: 0x44df51a8 cls=Lcom/demo/project/vo/DurationVo; (20 bytes) 07-11 19:23:31.660: WARN/dalvikvm(301): 504: 0x44df0930 cls=Ljava/lang/Class; 'Lcom/demo/project/vo/DurationVo;' (172 bytes) 07-11 19:23:31.660: WARN/dalvikvm(301): 505: 0x4002afc8 cls=Ljava/lang/Class; 'Ljava/util/ArrayList;' (172 bytes) 07-11 19:23:31.660: WARN/dalvikvm(301): 506: 0x44df5280 cls=Ljava/util/ArrayList; (28 bytes) 07-11 19:23:31.660: WARN/dalvikvm(301): 507: 0x44df0338 cls=Ljava/lang/Class; 'Lcom/demo/project/vo/MeasureTrackPairVo;' (172 bytes) 07-11 19:23:31.660: WARN/dalvikvm(301): 508: 0x44df52a0 cls=Lcom/demo/project/vo/MeasureTrackPairVo; (28 bytes) 07-11 19:23:31.660: WARN/dalvikvm(301): 509: 0x44df0338 cls=Ljava/lang/Class; 'Lcom/demo/project/vo/MeasureTrackPairVo;' (172 bytes) 07-11 19:23:31.660: WARN/dalvikvm(301): 510: 0x4002afc8 cls=Ljava/lang/Class; 'Ljava/util/ArrayList;' (172 bytes) 07-11 19:23:31.660: WARN/dalvikvm(301): 511: 0x44df5320 cls=Ljava/util/ArrayList; (28 bytes) 07-11 19:23:31.680: WARN/dalvikvm(301): JNI local reference table summary (512 entries): 07-11 19:23:31.690: WARN/dalvikvm(301): 322 of Ljava/lang/Class; 172B (11 unique) 07-11 19:23:31.690: WARN/dalvikvm(301): 7 of Ljava/lang/Integer; 12B (7 unique) 07-11 19:23:31.690: WARN/dalvikvm(301): 10 of Ljava/lang/String; 28B (10 unique) 07-11 19:23:31.690: WARN/dalvikvm(301): 33 of Ljava/util/ArrayList; 28B (33 unique) 07-11 19:23:31.690: WARN/dalvikvm(301): 1 of Lcom/demo/project/vo/HeaderVo; 60B 07-11 19:23:31.700: WARN/dalvikvm(301): 1 of Lcom/demo/project/vo/LyricsVo; 20B 07-11 19:23:31.700: WARN/dalvikvm(301): 1 of Lcom/demo/project/vo/LyricsLineVo; 20B 07-11 19:23:31.700: WARN/dalvikvm(301): 18 of Lcom/demo/project/vo/MeasureHeaderVo; 36B (18 unique) 07-11 19:23:31.710: WARN/dalvikvm(301): 1 of Lcom/demo/project/vo/TrackHeaderVo; 60B 07-11 19:23:31.710: WARN/dalvikvm(301): 14 of Lcom/demo/project/vo/MeasureTrackPairVo; 28B (14 unique) 07-11 19:23:31.710: WARN/dalvikvm(301): 52 of Lcom/demo/project/vo/BeatVo; 60B (52 unique) 07-11 19:23:31.710: WARN/dalvikvm(301): 52 of Lcom/demo/project/vo/DurationVo; 20B (52 unique) 07-11 19:23:31.710: WARN/dalvikvm(301): Memory held directly by native code is 8540 bytes 07-11 19:23:31.720: ERROR/dalvikvm(301): Failed adding to JNI local ref table (has 512 entries)

--------------解决方案-------------

我在用着

(*env)->DeleteLocalRef(env, classLyricsLine);
(*env)->DeleteLocalRef(env, objLyricsLine);
(*env)->DeleteLocalRef(env, findClsLyricsLine);

并且它似乎已经解决了ReferenceTable溢出的问题。 不过,我得到另一个错误,不是因为这么多的对象实例,因为数据的访问,但。

Song *song // struct.
....
song->measureTracks[m][n].beats[0].notes[0].noteEffects->bend->bendPoints

样品错误日志:

07-14 16:57:00.725: INFO/DEBUG(28): Build fingerprint: 'generic/sdk/generic/:2.1-update1/ECLAIR/35983:eng/test-keys'
07-14 16:57:00.725: INFO/DEBUG(28): pid: 297, tid: 307 >>> com.synapticstuff.guitartabs <<<
07-14 16:57:00.735: INFO/DEBUG(28): signal 11 (SIGSEGV), fault addr 0000001c
07-14 16:57:00.735: INFO/DEBUG(28): r0 00000034 r1 00127f28 r2 00000000 r3 00000000
07-14 16:57:00.735: INFO/DEBUG(28): r4 80a06ad4 r5 ad02cab9 r6 41878d20 r7 41867de4
07-14 16:57:00.735: INFO/DEBUG(28): r8 46d68d50 r9 41867ddc 10 41867dc4 fp 00139838
07-14 16:57:00.735: INFO/DEBUG(28): ip ad080214 sp 46d68a30 lr ad03bad1 pc 80a02bb2 cpsr 00000030
07-14 16:57:00.905: INFO/DEBUG(28): #00 pc 00002bb2 /data/data/com.synapticstuff.guitartabs/lib/libgpparser.so
07-14 16:57:00.915: INFO/DEBUG(28): #01 pc 0000f1f4 /system/lib/libdvm.so
07-14 16:57:00.915: INFO/DEBUG(28): #02 pc 00038018 /system/lib/libdvm.so
07-14 16:57:00.925: INFO/DEBUG(28): #03 pc 000316a2 /system/lib/libdvm.so
07-14 16:57:00.925: INFO/DEBUG(28): #04 pc 0003d31c /system/lib/libdvm.so
07-14 16:57:00.925: INFO/DEBUG(28): #05 pc 00013f58 /system/lib/libdvm.so
07-14 16:57:00.925: INFO/DEBUG(28): #06 pc 00019888 /system/lib/libdvm.so
07-14 16:57:00.935: INFO/DEBUG(28): #07 pc 00018d5c /system/lib/libdvm.so
07-14 16:57:00.935: INFO/DEBUG(28): #08 pc 0004d6d0 /system/lib/libdvm.so
07-14 16:57:00.945: INFO/DEBUG(28): #09 pc 0004d702 /system/lib/libdvm.so
07-14 16:57:00.945: INFO/DEBUG(28): #10 pc 00041c78 /system/lib/libdvm.so
07-14 16:57:00.945: INFO/DEBUG(28): #11 pc 00010000 /system/lib/libc.so
07-14 16:57:00.955: INFO/DEBUG(28): #12 pc 0000fad4 /system/lib/libc.so
07-14 16:57:00.955: INFO/DEBUG(28): code around pc:
07-14 16:57:00.975: INFO/DEBUG(28): 80a02ba0 68d918d3 1c139a2e 189b009b 18cb00db
07-14 16:57:00.975: INFO/DEBUG(28): 80a02bb0 7f1b6a5b 9b05931c 23bc681a 58d5005b
07-14 16:57:00.975: INFO/DEBUG(28): 80a02bc0 99259805 447b4b8c 4b8c1c1a 47a8447b
07-14 16:57:00.975: INFO/DEBUG(28): code around lr:
07-14 16:57:00.975: INFO/DEBUG(28): ad03bac0 18282200 f890f7f9 21079802 ffcef004
07-14 16:57:01.042: INFO/DEBUG(28): ad03bad0 1c30b005 46c0bdf0 000444c8 fffe57c4
07-14 16:57:01.045: INFO/DEBUG(28): ad03bae0 fffea17e fffea125 6900b5f0 1c0db087
07-14 16:57:01.045: INFO/DEBUG(28): stack:
07-14 16:57:01.055: INFO/DEBUG(28): 46d689f0 00129300 [heap]
07-14 16:57:01.055: INFO/DEBUG(28): 46d689f4 44e0eab8 /dev/ashmem/mspace/dalvik-heap/2 (deleted)
07-14 16:57:01.065: INFO/DEBUG(28): 46d689f8 80a056d8 /data/data/com.synapticstuff.guitartabs/lib/libgpparser.so
07-14 16:57:01.065: INFO/DEBUG(28): 46d689fc ad02cb49 /system/lib/libdvm.so
07-14 16:57:01.065: INFO/DEBUG(28): 46d68a00 ad038f65 /system/lib/libdvm.so
07-14 16:57:01.076: INFO/DEBUG(28): 46d68a04 00129300 [heap]
07-14 16:57:01.076: INFO/DEBUG(28): 46d68a08 44e0eb68 /dev/ashmem/mspace/dalvik-heap/2 (deleted)
07-14 16:57:01.076: INFO/DEBUG(28): 46d68a0c ad02a095 /system/lib/libdvm.so
07-14 16:57:01.076: INFO/DEBUG(28): 46d68a10 80a06ad4 /data/data/com.synapticstuff.guitartabs/lib/libgpparser.so
07-14 16:57:01.085: INFO/DEBUG(28): 46d68a14 80a05090 /data/data/com.synapticstuff.guitartabs/lib/libgpparser.so
07-14 16:57:01.085: INFO/DEBUG(28): 46d68a18 ad02d935 /system/lib/libdvm.so
07-14 16:57:01.085: INFO/DEBUG(28): 46d68a1c 80a06ad4 /data/data/com.synapticstuff.guitartabs/lib/libgpparser.so
07-14 16:57:01.085: INFO/DEBUG(28): 46d68a20 ad02cab9 /system/lib/libdvm.so
07-14 16:57:01.085: INFO/DEBUG(28): 46d68a24 41878d20 /dev/ashmem/dalvik-LinearAlloc (deleted)
07-14 16:57:01.085: INFO/DEBUG(28): 46d68a28 df002777
07-14 16:57:01.085: INFO/DEBUG(28): 46d68a2c e3a070ad
07-14 16:57:01.095: INFO/DEBUG(28): #00 46d68a30 00000000
07-14 16:57:01.095: INFO/DEBUG(28): 46d68a34 3fe00000
07-14 16:57:01.095: INFO/DEBUG(28): 46d68a38 44dfe710 /dev/ashmem/mspace/dalvik-heap/2 (deleted)
07-14 16:57:01.095: INFO/DEBUG(28): 46d68a3c 44dfe9a8 /dev/ashmem/mspace/dalvik-heap/2 (deleted)
07-14 16:57:01.095: INFO/DEBUG(28): 46d68a40 44dfe1b0 /dev/ashmem/mspace/dalvik-heap/2 (deleted)
07-14 16:57:01.095: INFO/DEBUG(28): 46d68a44 00129300 [heap]
07-14 16:57:01.095: INFO/DEBUG(28): 46d68a48 44dfede8 /dev/ashmem/mspace/dalvik-heap/2 (deleted)
07-14 16:57:01.095: INFO/DEBUG(28): 46d68a4c ffffffff
07-14 16:57:01.095: INFO/DEBUG(28): 46d68a50 00000073
07-14 16:57:01.095: INFO/DEBUG(28): 46d68a54 00000009
07-14 16:57:01.105: INFO/DEBUG(28): 46d68a58 46d68cc4
07-14 16:57:01.105: INFO/DEBUG(28): 46d68a5c afe38e08 /system/lib/libc.so
07-14 16:57:01.105: INFO/DEBUG(28): 46d68a60 46d68aac
07-14 16:57:01.105: INFO/DEBUG(28): 46d68a64 fffffff7
07-14 16:57:01.105: INFO/DEBUG(28): 46d68a68 ad06aae8 /system/lib/libdvm.so
07-14 16:57:01.105: INFO/DEBUG(28): 46d68a6c ad07ff50 /system/lib/libdvm.so

什么似乎是现在的问题吗? 我已经准备所有的对象实例,但每次我尝试如果它太远,它崩溃使用实际的歌曲结构数据。 即使我得到的只是该结构的一部分:

NoteEffects *noteEffects = song->measureTracks[m][n].beats[0].notes[0].noteEffects
// then use noteEffects.bend

分类:java的 时间:2015-03-15 人气:0
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 1.872 (s). 10 q(s)