简体   繁体   中英

Are ByteBuffers a good way to store > 1MB of data in Android?

I have an application that needs to store 3 ~1MB byte arrays in a queue at any given moment. It seems what ends up happening is I get this message

I/dalvikvm-heap﹕ Grow heap (frag case) to 4.827MB for 1058412-byte allocation

And then my byte[] is allocated with a size of 0.

I have tried setting android:LargeHeap="true" in the manifest but this still occurs.

If I use ByteBuffers instead of byte[] is it possible to store these 3 byte[] objects without memory problems?

From the symptoms (and without seeing your code) it sounds like the problem is not the total size of the heap; rather it is that Android can't find a single, contiguous block of free memory large enough for your 1MB array. That's not all that surprising considering the resource constraints of Android devices. If I'm right about what's going on, then I doubt very much that ByteBuffer will solve the problem; it will still need to allocate a single byte array that is large enough.

If your use case permits, you might consider splitting up each 1MB byte[] into smaller chunks. (There's a nice blog post here about one way to do that.) The system will have a much easier time finding 16 chunks of free memory at 64KB each (for instance) than it will finding a single chunk of size 1MB. I've successfully used this kind of approach (albeit using a different design than in the above link) to load data sets a bit larger than 2.5MB, even on older devices (eg, Android 2.2) that have much smaller total heap size than more recent devices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM