2011-07-06 115 views
1

找到一種方法將C到Java的複雜模型(包含子對象和帶有對象項的ArrayList)返回給Java。但我有另一個問題:內存泄漏。避免JNI C中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); 

代碼的太長了,我不知道是否應該將其他對象分隔到另一個函數,並將解析的數據傳遞給每個不同的函數,我認爲它會消耗更多的內存,而且我已經解決了使用過的問題。

我讀過關於如何釋放空間的一些方法:

Memory leak using JNI to retrieve String's value from Java code

Memory leak when calling java code from C using JNI

但是應用這些是不夠的。它只能允許一個數據解析,但之後,我的程序崩潰了。

還有其他推薦的代碼可以減少內存使用嗎?

我加入我下面的錯誤日誌:

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) 
+0

你能控制Android上的堆大小嗎?違反直覺,這裏的解決方案是* [減小堆大小](http://stackoverflow.com/questions/1744533/jna-bytebuffer-not-getting-freed-and-causing-c-heap-to-run-out -of存儲器)*。 –

回答

0

我使用

 (*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