OutOfMemoryError when I decompress RAR file on Android

ⅰ亾dé卋堺 提交于 2019-12-24 15:12:55

问题


I try to do it like this: Decompress Rar file in Android

But one of my rar file cannot be decompressed.

Logs:

01-01 17:41:32.121: E/AndroidRuntime(12799): FATAL EXCEPTION: Thread-771
01-01 17:41:32.121: E/AndroidRuntime(12799): java.lang.OutOfMemoryError
01-01 17:41:32.121: E/AndroidRuntime(12799):    at com.github.junrar.unpack.ppm.SubAllocator.startSubAllocator(SubAllocator.java:146)
01-01 17:41:32.121: E/AndroidRuntime(12799):    at com.github.junrar.unpack.ppm.ModelPPM.decodeInit(ModelPPM.java:216)
01-01 17:41:32.121: E/AndroidRuntime(12799):    at com.github.junrar.unpack.Unpack.readTables(Unpack.java:656)
01-01 17:41:32.121: E/AndroidRuntime(12799):    at com.github.junrar.unpack.Unpack.unpack29(Unpack.java:165)
01-01 17:41:32.121: E/AndroidRuntime(12799):    at com.github.junrar.unpack.Unpack.doUnpack(Unpack.java:120)
01-01 17:41:32.121: E/AndroidRuntime(12799):    at com.github.junrar.Archive.doExtractFile(Archive.java:500)
01-01 17:41:32.121: E/AndroidRuntime(12799):    at com.github.junrar.Archive.extractFile(Archive.java:442)
01-01 17:41:32.121: E/AndroidRuntime(12799):    at com.github.junrar.testutil.ExtractArchive.extractArchive(ExtractArchive.java:73)
01-01 17:41:32.121: E/AndroidRuntime(12799):    at com.github.junrar.testutil.ExtractArchive.extractArchive(ExtractArchive.java:29)
01-01 17:41:32.121: E/AndroidRuntime(12799):    at com.letusread.util.DeCompressUtil.deCompress(DeCompressUtil.java:140)
01-01 17:41:32.121: E/AndroidRuntime(12799):    at com.letusread.activity.FileListActivity$7.run(FileListActivity.java:338)
01-01 17:41:32.121: E/AndroidRuntime(12799):    at java.lang.Thread.run(Thread.java:856)
01-01 17:41:32.324: E/MobclickAgent(12799): onEndSession called before onStartSession

I the file was decompressed but my apps has crashed! other files can be decompressed properly; can you help me to solve this error??


回答1:


It seems that problem is in allocating heap memory for extracting files. You can see that there is OutOfMemoryError on line 146 in file SubAllocator.java. On this line is byte array initialized:

heap = new byte[realAllocSize];

Apparently variable realAllocSize is bigger than avaliable free memory which is on heap in bytes. On Android devices is heap size usualy 32 or 64 MB for each VM (each activity has own VM). It seems that problem is in junrar library which is not optimized to use on Android devices and consumes lot of memory.




回答2:


In fact, it seems to be a bug of the implementation. This is the workaround I have used in order to avoid the problem:

In com.github.junrar.unpack.ppm.ModelPPM.java, line 196: MaxMB = unpackRead.getChar();

the method getChar, in some weird situations, return a very big number. It should be due to a broken header or a header option not supported by junrar.

My workaround was to check if MaxMB was greater than 1 and set to 1. I've been using this fix for a long time without problems.

int MaxMB = 0;
if (reset) {
    MaxMB = unpackRead.getChar();
    if (MaxMB > 1) { //Workaround
        MaxMB = 1;
    }
}


来源:https://stackoverflow.com/questions/14109455/outofmemoryerror-when-i-decompress-rar-file-on-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!