PDA

View Full Version : Problem with Hessian on Android. StringBuffer uses too much memory.


dilbert
07-01-2010, 12:50 PM
I ran into this issue while using Hessian on the Android platform. Android version 1.6 is severely affected and the problem is less serious on 2.1. In short the Hessian class com.caucho.hessian.io.Hessian2Input uses (and reuses) a private member StringBuffer _sbuf. Here is a snippet from said class:

case 0x30: case 0x31: case 0x32: case 0x33:
_isLastChunk = true;
_chunkLength = (tag - 0x30) * 256 + read();

_sbuf.setLength(0);

while ((ch = parseChar()) >= 0)
_sbuf.append((char) ch);

return _sbuf.toString();

This piece of code reads a char from the input and appends it to the _sbuf. When finished it returns the string to the caller via the toString() call. Due to an error in the Andriod implementation (for details check: http://code.google.com/p/android/issues/detail?id=9435) it is possible that the call will return a string that occupies far more memory space than needed which is very bad on mobile devices. Also it seems that earlier Java implementations suffered from a similar problem: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4259569
Since this is not strictly a Hessian bug I am reluctant to post a bug report but maybe a replacement of StringBuffer with a local StringBuilder should be considered. I made the replacement in my version and it solved the problem.

emil
07-01-2010, 11:07 PM
Hi,

Yup, that should be a StringBuilder instead, even outside of Android. We've changed it upstream for the next version.

Thanks,
Emil

dilbert
07-02-2010, 10:39 AM
Thanks for the quick response. When can I expect the new version (4.0.8 I suppose)?