I am working on memory optimization of a piece of code. which is responsible for transferring an image file to other computer. Image file is 240Mb, and current app heap size is 1536Mb.
Current code is
byte[] buf = new byte[size];
while ((num = is.read(buf)) > 0) {
...
String str = new String(buf, 0, num));
...
sendToPc(str);
}
This creates a lot of string objects and when i try to push the image to more than 5 PC's it runs out of heap. So i thought of using string builder (I do not care about synchronization)
But string builder do not have a variant like
byte[] buf = new byte[size];
StringBuilder str = new StringBuilder(size);
while ((num = is.read(buf)) > 0) {
...
str.insert(0,buf); --> Apparently can not append byte array.
...
str.delete(0, str.length());
}
And even if i try str.insert(0,new String(buf, 0, num)) it do not make any difference.
Any ideas, how can i squeeze the number of objects since i can not use string builder or string buffer.
Regards
Dheeraj Joshi