-2

I am facing OOM issue when i try to convert a large file > 50MB to string (using StringBuilder).

Have seen similar questions but none of the answer helped .

End motive is append the file data to xml attribute and transfer this to server .

Trying it on android device .

Code used is form this post : How do I create a Java string from the contents of a file?

Community
  • 1
  • 1
  • 2
    Are you getting the error on the actual device or on emulator? – PM 77-1 May 09 '14 at 15:32
  • 3
    It would be easier to answer your question with a working code sample. Please post your code as an [minimal example](http://stackoverflow.com/help/mcve) that demonstrates your problem. – Duncan Jones May 09 '14 at 15:33
  • Can we see your code where you use the StringBuilder and attempt to convert the file? Can we also see the code relating to appending the file data to XML? There may be a way we can help adjust your code in order to this without getting out-of-memory issues – But I'm Not A Wrapper Class May 09 '14 at 15:37
  • @PM77-1 actual android device – user2717141 May 09 '14 at 15:58

3 Answers3

0

Make a list<string> and while reading the file, save every e.g. 200 characters to the list as a new string. Then you can write it the same way to your XML.

I hope it helps.

Sorashi
  • 931
  • 2
  • 9
  • 32
0

To avoid having the whole file in Heap at once, it would be better to use Multipart messages to send it to the server. One part would contain the XML, the file would be in a separate part. This can be implemented using a webservice, but it depends on the requirements and how much the server implementation can be changed.

If the server is "fixed" and can not be changed to accept multipart messages, and you must stay with sending all in one XML, you would probably need to avoid using DOM to generate the XML.

bourbert
  • 308
  • 3
  • 14
0

Try the Apache Commons IO library with IOUtils:

public static void main(String[] args) throws Exception {
    IOUtils.write("string", new FileOutputStream("file"));
} 
Gabriel Ruiu
  • 2,753
  • 2
  • 19
  • 23