Another memory problem when building up a String

  • Replies:4
Robert Uomini
  • Forum posts: 10

Apr 13, 2012, 4:21:15 AM via Website

Having another memory problem which, unfortunately, can't be solved this time by saving to disk.

1int byteCount;
2 byte[] tmp = new byte[BUFSIZE];
3 encodedContent = "";
4
5 do {
6 byteCount = is.read(tmp, 0, BUFSIZE);
7 if (byteCount == -1) break;
8 encodedContent += Base64.encodeToString(tmp, 0, byteCount, Base64.NO_WRAP);
9 } while (byteCount != -1);

In the above code, the String encodedContent seems to be taking twice as much space as expected. For example, it grows by at most BUFSIZE (64 KB) with each iteration of the loop, but when it has grown to 500 KB, the VM is allocating memory for 1 MB. When encodedContent is 900 KB in size, the VM is allocating 1.8 MB, etc. Anyone know why encodedContent should take twice it's needed memory and what to do about it?

Reply
Jeremiah
  • Forum posts: 775

Apr 16, 2012, 6:10:39 AM via Website

Read the first paragraph here about Strings: http://developer.android.com/reference/java/lang/String.html
Strings are backed by a char array of UTF-16 values. So each char of the string takes 2 bytes (UTF-16).

What to do about it, really depends on what you need to do with the string information. Are you trying to display email data again?

Reply
Robert Uomini
  • Forum posts: 10

Apr 17, 2012, 12:39:48 AM via Website

No, just trying to send a large mail attachment to a content server. I've decided, as a temporary measure, to limit uploads to 4 MB or less. The long-term solution, I'm pretty sure, is to send it up in chunks, which would require implementing new server requests.

Reply
Jeremiah
  • Forum posts: 775

Apr 17, 2012, 6:35:07 AM via Website

Why can't you save the attachment to a file, and then read the file and send it to the server as your reading it? No need for all of it to be in the apps memory at once that way.

Reply
Robert Uomini
  • Forum posts: 10

Apr 17, 2012, 4:07:22 PM via Website

The problem is that when data is sent to the server, the server returns a pointer to the data in its database. If I send the attachment in chunks, I get a bunch of pointers back and when the data is later fetched, the mail client would need to reconstruct the data from those chunks. I don't want to do that.

Reply