Mesh: Compacting Memory Management for C/C++ Applications

Introduction

Programs written in C/C++ can suffer from serious memory fragmentation, leading to low utilization of memory, degraded performance, and application failure due to memory exhaustion. This paper introduces Mesh, a plug-in replacement for malloc that, for the first time, eliminates fragmentation in unmodified C/C++ applications.

Background

malloc()

malloc() is a user-space interface for C/C++ programmers to allocate memories from heap, and it requires users to “free” it manually.

glibc malloc() ← ptmalloc()

In user-space, it uses a free-list to manage the allocated memory chunks. Memory chunks are added to free-list when the user invokes free(), but the chunks will not be freed immediately. If neighbors of the chunk can be merged, it may be merged for reducing the fragmentation. If the required memory size cannot be found in the free-list, it uses brk()/sbrk() to acquire desired memory from kernel.

  • jemalloc() from Facebook

Programming Language

Some languages features compacting garbage collection, such as Java, LISP. Other languages like Rust and Go currently does not support compacting garbage collection, and the authors of Mesh claim they may try to integrate into it.

Key Ideas

Mesh

The key idea: Mesh

Random Allocation

Implementation

  • Mesh = MiniHeaps + Shuffle Vectors + Thread Local Heaps + Global Heap + Meshing
  • MiniHeaps

Related OS APIs in the implementation

  • mmap
  • fallocate (FALLOC_FL_PUNCH_HOLE)
  • mallctl
  • memfd_create
  • mprotect

Evaluation

Conclusion

Mesh, a memory allocator that efficiently performs compaction without relocation to save memory for unmanaged languages.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
張家榮 Tiba Chang

張家榮 Tiba Chang

6 Followers

A system software engineer from Taiwan. The posts are about my daily reading and polishing my English skills.