Search code examples
androidcandroid-ndkashmem

How to release ashmem


Allocating and using ashmem works well:

ashmemFD = open("/dev/ashmem", O_RDWR);
int ret = ioctl(ashmemFD, ASHMEM_SET_NAME, "vf");
ioctl(ashmemFD, ASHMEM_SET_SIZE, size);
ashmap = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ashmemFD, 0);

But the question is, is it enough to just unmap it after use, or do I need to do more to release the memory again?

munmap((void*) ashmap,size);

Solution

  • You need to close all file descriptors to the ashmem region after unmapping it.

    Ashmem regions can be seen as memory-based files handled by the kernel. As long as there is an open file descriptor to one of them, it will be kept in memory.

    Hope this helps.