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

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

Mesh: Compacting Memory Management for C/C++ Applications Bobby Powers David Tench College of Information and Computer Sciences College of Information and Computer Sciences University of Massachusetts Amherst University of Massachusetts Amherst Amherst, MA, USA Amherst, MA, USA [email protected] [email protected] Emery D. Berger Andrew McGregor College of Information and Computer Sciences College of Information and Computer Sciences University of Massachusetts Amherst University of Massachusetts Amherst Amherst, MA, USA Amherst, MA, USA [email protected] [email protected] Abstract integers, store flags in the low bits of aligned addresses, per- Programs written in C/C++ can suffer from serious memory form arithmetic on addresses and later reference them, or fragmentation, leading to low utilization of memory, de- even store addresses to disk and later reload them. This graded performance, and application failure due to memory hostile environment makes it impossible to safely relocate exhaustion. This paper introduces Mesh, a plug-in replace- objects: if an object is relocated, all pointers to its original ment for malloc that, for the first time, eliminates fragmen- location must be updated. However, there is no way to safely tation in unmodified C/C++ applications. Mesh combines update every reference when they are ambiguous, much less novel randomized algorithms with widely-supported virtual when they are absent. memory operations to provably reduce fragmentation, break- Existing memory allocators for C/C++ employ a variety of ing the classical Robson bounds with high probability. Mesh best-effort heuristics aimed at reducing average fragmenta- generally matches the runtime performance of state-of-the- tion [17]. However, these approaches are inherently limited. art memory allocators while reducing memory consumption; In a classic result, Robson showed that all such allocators can in particular, it reduces the memory of consumption of Fire- suffer from catastrophic memory fragmentation [26]. This fox by 16% and Redis by 39%. increase in memory consumption can be as high as the log of the ratio between the largest and smallest object sizes allo- CCS Concepts • Software and its engineering → Allo- cated. For example, for an application that allocates 16-byte cation / deallocation strategies; and 128KB objects, it is possible for it to consume 13× more Keywords Memory management, runtime systems, un- memory than required. managed languages Despite nearly fifty years of conventional wisdom indicat- ing that compaction is impossible in unmanaged languages, 1 Introduction this paper shows that it is not only possible but also practical. It introduces Mesh, a memory allocator that effectively and Memory consumption is a serious concern across the spec- efficiently performs compacting memory management to trum of modern computing platforms, from mobile to desk- reduce memory usage in unmodified C/C++ applications. arXiv:1902.04738v2 [cs.PL] 16 Feb 2019 top to datacenters. For example, on low-end Android devices, Crucially and counterintuitively, Mesh performs com- Google reports that more than 99 percent of Chrome crashes paction without relocation; that is, without changing the are due to running out of memory when attempting to dis- addresses of objects. This property is vital for compatibility play a web page [15]. On desktops, the Firefox web browser with arbitrary C/C++ applications. To achieve this, Mesh has been the subject of a five-year effort to reduce its memory builds on a mechanism which we call meshing, first intro- footprint [28]. In datacenters, developers implement a range duced by Novark et al.’s Hound memory leak detector [23]. of techniques from custom allocators to other ad hoc ap- Hound employed meshing in an effort to avoid catastrophic proaches in an effort to increase memory utilization [25, 27]. memory consumption induced by its memory-inefficient allo- A key challenge is that, unlike in garbage-collected en- cation scheme, which can only reclaim memory when every vironments, automatically reducing a C/C++ application’s object on a page is freed. Hound first searches for pages memory footprint via compaction is not possible. Because whose live objects do not overlap. It then copies the contents the addresses of allocated objects are directly exposed to of one page onto the other, remaps one of the virtual pages programmers, C/C++ applications can freely modify or hide to point to the single physical page now holding the contents addresses. For example, a program may stash addresses in Bobby Powers, David Tench, Emery D. Berger, and Andrew McGregor free free allocated allocated virtual memory virtual memory physical memory physical memory munmap (a) Before: these pages are candidates for “meshing” (b) After: both virtual pages now point to the first physical page; because their allocated objects do not overlap. the second page is now freed. Figure 1. Mesh in action. Mesh employs novel randomized algorithms that let it efficiently find and then “mesh” candidate pages within spans (contiguous 4K pages) whose contents do not overlap. In this example, it increases memory utilization across these pages from 37.5% to 75%, and returns one physical page to the OS (via munmap), reducing the overall memory footprint. Mesh’s randomized allocation algorithm ensures meshing’s effectiveness with high probability. of both pages, and finally relinquishes the other physical export LD_PRELOAD=libmesh.so). Our empirical evalua- page to the OS. Figure 1 illustrates meshing in action. tion demonstrates that our implementation of Mesh is both Mesh overcomes two key technical challenges of mesh- fast and efficient in practice. It generally matches the per- ing that previously made it both inefficient and potentially formance of state-of-the-art allocators while guaranteeing entirely ineffective. First, Hound’s search for pages to mesh the absence of catastrophic fragmentation with high prob- involves a linear scan of pages on calls to free. While this ability. In addition, it occasionally yields substantial space search is more efficient than a naive O¹n2º search of all possi- savings: replacing the standard allocator with Mesh auto- ble pairs of pages, it remains prohibitively expensive for use matically reduces memory consumption by 16% (Firefox) to in the context of a general-purpose allocator. Second, Hound 39% (Redis). offers no guarantees that any pages would ever be meshable. Consider an application that happens to allocate even one 1.1 Contributions object in the same offset in every page. That layout would This paper makes the following contributions: preclude meshing altogether, eliminating the possibility of • saving any space. It introduces Mesh, a novel memory allocator that acts malloc Mesh makes meshing both efficient and provably effec- as a plug-in replacement for . Mesh combines tive (with high probability) by combining it with two novel remapping of virtual to physical pages (meshing) with randomized algorithms. First, Mesh uses a space-efficient randomized allocation and search algorithms to enable randomized allocation strategy that effectively scatters ob- safe and effective compaction without relocation for jects within each virtual page, making the above scenario C/C++ (§2, §3, §4). • provably exceedingly unlikely. Second, Mesh incorporates It presents theoretical results that guarantee Mesh’s an efficient randomized algorithm that is guaranteed with efficiency and effectiveness with high probability (§5). • high probability to quickly find candidate pages that are It evaluates Mesh’s performance empirically, demon- likely to mesh. These two algorithms work in concert to en- strating Mesh’s ability to reduce space consumption able formal guarantees on Mesh’s effectiveness. Our analysis while generally imposing low runtime overhead (§6). shows that Mesh breaks the above-mentioned Robson worst case bounds for fragmentation with high probability [26]. 2 Overview We implement Mesh as a library for C/C++ applications This section provides a high-level overview of how Mesh running on Linux of Mac OS X. Mesh interposes on memory works and gives some intuition as to how its algorithms management operations, making it possible to use it without and implementation ensure its efficiency and effectiveness, code changes or even recompilation by setting the appro- before diving into detailed description of Mesh’s algorithms priate environment variable to load the Mesh library (e.g., (§3), implementation (§4), and its theoretical analysis (§5). Mesh 2.1 Remapping Virtual Pages 2.3 Finding Spans to Mesh Mesh enables compaction without relocating object ad- Given a set of spans, our goal is to mesh them in a way that dresses; it depends only on hardware-level virtual memory frees as many physical pages as possible. We can think of support, which is standard on most computing platforms this task as that of partitioning the spans into subsets such like x86 and ARM64. Mesh works by finding pairs of pages that the spans in each subset mesh. An optimal partition and merging them together physically but not virtually: this would minimize the number of such subsets. merging lets it relinquish physical pages to the OS. Unfortunately, as we show, optimal meshing is not feasible Meshing is only possible when no objects on the pages (§5). Instead, the algorithms in Section 3 present practical occupy the same offsets. A key observation is that as frag- methods for finding high-quality meshes under real-world mentation increases (that is, as there are more free objects), time constraints. We show that solving a simplified version the likelihood of successfully finding pairs of pages that mesh of the problem (§3) is sufficient to achieve reasonable meshes also increases. with high probability (§5). Figure 1 schematically illustrates the meshing process. Mesh manages memory at the granularity of spans, which 3 Algorithms are runs of contiguous 4K pages (for purposes of illustration, Mesh comprises three main algorithmic components: allo- the figure shows single-page spans). Each span only contains cation (§3.1), deallocation (§3.2), and finding spans to mesh same-sized objects.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    14 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us