Passing huge string from native to java - out of memory error

I am writing a function in native C ++ where it returns a large string using env->NewStringUTF

. The problem is that when the string is large (about 23MB) I get an out of memory exception in Java.

I already tried to get the data piece by piece, but it's very slow.

This is my code:

char* d = data.c_str();
jstring str = env->NewStringUTF(d);
return str;

      

How can I pass this string to Java?

+3


source to share


1 answer


You cannot store such a large string in memory. The heap limit on some Android devices is 16MB for the entire app.

I suggest you store this line in a file (or perhaps db) and pass the filename to java.



Or maybe you need to change your application logic and continue storing the string in its own space and pass java only the small requested parts ...

PS You can compress the string with GZIP and reduce the memory size by several times, but you still cannot save the whole unpacked string in java ...

+3


source







All Articles