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