How to keep efficiently continuous memory in RAM for Kotlin Multiplatform mobile project

Viewed 94

A mobile app receives series of 4 bytes long packets. There can be about ~1M of those packets received in one session. The app can analyse them once all packets are received, so there is a need to store them somehow. I wonder what is the best approach in terms of memory size efficiency for Kotlin Multiplatform Mobile project.

I considered:

  • Common implementation for Android and iOS and use:

    • ByteArray
    • List<Long> - keep 2 packets in the biggest primitive - Long (8B) to reduce number of internal items in the list, hence reduce memory overhead consumed by each object.
  • Make an abstract collection and implement it for each platform. As an internal memory model use:

    • Android: ByteBuffer or ByteArray or List<Long>
    • iOS (Swift): ByteArray or Data

Minimal memory usage is the most important factor.

What is the best approach in terms of memory consumption?

1 Answers

Not a List of Int, for sure. ByteArray is ok, or maybe IntArray if that's easier to manage the data (as ints).

If you know 1M is the max, you'll have an easier time initializing a single array and reusing it. If not, you can figure out a more flexible structure, but it'll be something built with a data array underneath (I'd guess).

You could probably do something slightly more efficient with platform specific structures, but I'd start with ByteArray/IntArray and work from there

Related