Found a way to return complex models (Object with sub objects and ArrayList with Object items) from C to Java. But I have another problem: Memory leaks.
Currently, the code I'm working on is parsing a data (on C) and should return the data parsed to a Java Class.
The main Java Class contains more Java Classes (ints, String, boolean, Obj, ArrayList of Objects), and I'm returning the parsed data using this pattern for Object instances:
// 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);
The code's too long and I don't know if I should separate other objects to another function and just pass the parsed data to each different functions, I think it would consume more memory and I'm already having issues on freeing used ones.
I've read some ways on how to free spaces:
Memory leak using JNI to retrieve String's value from Java code
Memory leak when calling java code from C using JNI
But applying those aren't enough. It can only allow one parsing of data but after that, my program's crashing.
Are there any other recommended codes that can reduce memory usage for this?
I'm adding my error log below:
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)