Vame
Vame

Reputation: 2053

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

I am writing a function in native C++ where it returns a big string using env->NewStringUTF. The problem is that when the string is big (about 23 Mbytes) I get an out of memory exception in Java.

I already tried getting the data in parts, but it is 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?

Upvotes: 3

Views: 797

Answers (1)

Leonidos
Leonidos

Reputation: 10518

You cant keep such a big string in memory. Heap limit on some android devices is 16MB for whole application.

I suggest you to save this string to a file (or maybe in db) and pass file name to java.

Or maybe you should change application logic and continue keeping string in native space and pass to java only small requested parts...

P.S. You can compress string with GZIP and reduce it's memory footprint in several times, but you still wont be able to keep whole decompressed string in java...

Upvotes: 3

Related Questions