Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a memory mapped file as a swap file?

I'm working in Android where memory is limited and there is no swap file like feature. I've read that people have implemented swap file like functionality using memory mapped files in their apps but I don't understand how this would work. Does anyone know?

I think the general idea is that, if my program wanted to use an int array that was the size of say 20Mb, I could create the data for this array in a 20Mb file and then use the memory mapped file feature to transparently bring into memory the parts of the file I current need to read and write to. The crucial element is that, although I have 20Mb of space to read/write to, only a small fraction of this is ever in memory at a time (Android apps are limited to ~24Mb of memory).

I'd prefer to work in Java for this but a C solution would also work.

Edit: This memory mapped file trick is mentioned here but I don't understand how it would work http://grammerjack.blogspot.com/2009/10/gles-quake-port-of-quake-to-android.html

like image 968
rbcc Avatar asked Nov 14 '22 03:11

rbcc


1 Answers

In Java, use java.nio.MappedByteBuffer.

In C, use the POSIX function mmap(). It works on Android fine.

like image 156
Seva Alekseyev Avatar answered Nov 16 '22 17:11

Seva Alekseyev