fast-serialization
fast-serialization copied to clipboard
Non-fatal Exception: java.lang.OutOfMemoryError
Hi, I get this from Crashlitics:
Non-fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 1296258136 byte allocation with 6291456 free bytes and 510MB until OOM, max allowed footprint 8128976, growth limit 536870912 at org.nustaq.serialization.FSTConfiguration.decodeFromStream(FSTConfiguration.java:1273) at com.gilapps.smsshare2.theming.ThemesManager$2.call(ThemesManager.java:169) at com.gilapps.smsshare2.theming.ThemesManager$2.call(ThemesManager.java:145) at com.github.rongi.async.Task.run(Task.java:28) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764)
There is absolutly NO WAY that this object files are 1296258136 bytes
Here is some code:
public class Serializer {
private static FSTConfiguration serializer;
public static FSTConfiguration getSerializer() {
if (serializer==null) {
serializer = FSTConfiguration.createAndroidDefaultConfiguration();
serializer.registerSerializer(Bitmap.class,new FSTBitmapSerializer(),true);
}
return serializer;
}
private static class FSTBitmapSerializer extends FSTBasicObjectSerializer {
@Override
public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException {
Bitmap bitmap = (Bitmap)toWrite;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
byte[] byteArray = stream.toByteArray();
out.writeInt(byteArray.length);
out.write(byteArray);
}
@Override
public void readObject(FSTObjectInput in, Object toRead, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy) throws Exception {
}
@Override
public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception {
int bufferLength = in.readInt();
byte[] byteArray = new byte[bufferLength];
in.readFully(byteArray, 0, bufferLength);
return BitmapFactory.decodeByteArray(byteArray, 0, bufferLength);
}
}
}
InputStream fis;
long fileSize;
if (themePath.contains(":/")) {
fis = context.getContentResolver().openInputStream(Uri.parse(themePath));
if (fis==null) throw new Exception("error opening theme: " + themePath);
fileSize = (long)fis.available()*8L;
} else if (themePath.contains("/")) {
File file = new File(themePath);
fis = new FileInputStream(file);
fileSize = file.length();
}
else {
fis = context.getAssets().open(Const.THEMES_FOLDER + "/" + themePath);
fileSize = (long)fis.available()*8L;
}
Theme2 theme = (Theme2) Serializer.getSerializer().decodeFromStream(fis);
seems like read / write asymmetry. if the array length is read from a wrong position, stuff like this happens (or if the stream is somehow corrupt)