Region-based memory management

Memory allocation scheme From Wikipedia, the free encyclopedia

In computer science, region-based memory management is a type of memory management in which each allocated object is assigned to a region. A region, also called a partition, subpool, zone, arena, area, or memory context, is a collection of allocated objects that can be efficiently reallocated or deallocated all at once. Memory allocators using region-based managements are often called area allocators, and when they work by only "bumping" a single pointer, as bump allocators.

Like stack allocation, regions facilitate allocation and deallocation of memory with low overhead; but they are more flexible, allowing objects to live longer than the stack frame in which they were allocated. In typical implementations, all objects in a region are allocated in a single contiguous range of memory addresses, similarly to how stack frames are typically allocated.

In OS/360 and successors, the concept applies at two levels; each job runs within a contiguous partition[a] or region.[b] Storage allocation requests specify a subpool, and the application can free an entire subpool. Storage for a subpool is allocated from the region or partition in blocks that are a multiple of 2 KiB[c] or 4 KiB[d] that generally are not contiguous.

Example

As a simple example, consider the following C++ code, which implements a simple arena, and a simple data type consisting of a string and an integer value.

import std;

using std::bad_alloc;
using std::constructible_from;
using std::span;
using std::string;
using std::unique_ptr;

class Arena {
private:
    unique_ptr<byte[]> storage;
    size_t capacity;
    size_t offset;

    [[nodiscard]]
    void* allocBytes(size_t size, size_t alignment) {
        void* ptr = storage.get() + offset;
        size_t space = capacity - offset;
        if (!std::align(alignment, size, ptr, space)) {
            throw bad_alloc("Failed to allocate bytes!");
        }
        offset = capacity - space + size;
        return ptr;
    }
public:
    explicit Arena(size_t capcity):
        storage{std::make_unique<byte[]>(capacity)}, capacity{capacity}, offset{0} {
        if (!buffer) {
            throw bad_alloc("Failed to allocate buffer!");
        }
    }

    ~Arena() = default;
    Arena(Arena&&) noexcept = default;
    Arena& operator=(Arena&&) noexcept = default;

    Arena(const Arena&) = delete("Delete copy constructor to prevent double frees");
    Arena& operator=(const Arena&) = delete("Delete assignment operator to prevent memory leaks");

    template <typename T, typename... Args>
        requires constructible_from<T, Args...>
    [[nodiscard]]
    T* alloc(Args&&... args) {
        void* mem = allocBytes(sizeof(T), alignof(T));
        return std::construct_at(static_cast<T*>(mem), std::forward<Args>(args)...);
    }

    void reset() noexcept {
        offset = 0;
    }

    [[nodiscard]]
    span<byte> remaining() const noexcept {
        return {storage.get() + offset, capacity - offset};
    }
};

struct Node {
    string name;
    int value;
};

int main() {
    Arena arena(4096);
    Node* a = arena.alloc<Node>("Alpha", 1);
    Node* b = arena.alloc<Node>("Beta", 2);

    std::println("a = ({}, {})", a->name, a->value);
    std::println("b = ({}, {})", b->name, b->value);
    arena.reset();
}

Implementation

Simple explicit regions are straightforward to implement; the following description is based on the work of Hanson.[1] Each region is implemented as a linked list of large blocks of memory; each block should be large enough to serve many allocations. The current block maintains a pointer to the next free position in the block, and if the block is filled, a new one is allocated and added to the list. When the region is deallocated, the next-free-position pointer is reset to the beginning of the first block, and the list of blocks can be reused for the next allocated region. Alternatively, when a region is deallocated, its list of blocks can be appended to a global freelist from which other regions may later allocate new blocks. With either case of this simple scheme, it is not possible to deallocate individual objects in regions.

The overall cost per allocated byte of this scheme is very low; almost all allocations involve only a comparison and an update to the next-free-position pointer. Deallocating a region is a constant-time operation, and is done rarely. Unlike in typical garbage collection systems, there is no need to tag data with its type.

History and concepts

The basic concept of regions is very old, first appearing as early as 1967 in Douglas T. Ross's AED Free Storage Package, in which memory was partitioned into a hierarchy of zones; each zone had its own allocator, and a zone could be freed all-at-once, making zones usable as regions.[2] In 1976, the PL/I standard included the AREA data type.[3] In 1990, Hanson demonstrated that explicit regions in C (which he called arenas[clarification needed]) could achieve time performance per allocated byte superior to even the fastest-known heap allocation mechanism.[1] Explicit regions were instrumental in the design of some early C-based software projects, including the Apache HTTP Server, which calls them pools, and the PostgreSQL database management system, which calls them memory contexts.[4] Like traditional heap allocation, these schemes do not provide memory safety; it is possible for a programmer to access a region after it is deallocated through a dangling pointer, or to forget to deallocate a region, causing a memory leak.

Region inference

In 1988, researchers began investigating how to use regions for safe memory allocation by introducing the concept of region inference, where the creation and deallocation of regions, as well as the assignment of individual static allocation expressions to particular regions, is inserted by the compiler at compile-time. The compiler is able to do this in such a way that it can guarantee dangling pointers and leaks do not occur.

In an early work by Ruggieri and Murtagh,[5] a region is created at the beginning of each function and deallocated at the end. They then use data flow analysis to determine a lifetime for each static allocation expression, and assign it to the youngest region that contains its entire lifetime.

In 1994, this work was generalized in a seminal work by Tofte and Talpin to support type polymorphism and higher-order functions in Standard ML, a functional programming language, using a different algorithm based on type inference and the theoretical concepts of polymorphic region types and the region calculus.[6][7] Their work introduced an extension of the lambda calculus including regions, adding two constructs:

at : Compute the result of the expression and store it in region ;
letregion in end: Create a region and bind it to ; evaluate ; then deallocate the region.

Due to this syntactic structure, regions are nested, meaning that if is created after , it must also be deallocated before ; the result is a stack of regions. Moreover, regions must be deallocated in the same function in which they are created. These restrictions were relaxed by Aiken et al.[8]

This extended lambda calculus was intended to serve as a provably memory-safe intermediate representation for compiling Standard ML programs into machine code, but building a translator that would produce good results on large programs faced a number of practical limitations which had to be resolved with new analyses, including dealing with recursive calls, tail calls, and eliminating regions which contained only a single value. This work was completed in 1995[9] and integrated into the ML Kit, a version of ML based on region allocation in place of garbage collection. This permitted a direct comparison between the two on medium-sized test programs, yielding widely varying results ("between 10 times faster and four times slower") depending on how "region-friendly" the program was; compile times, however, were on the order of minutes.[10] The ML Kit was eventually scaled to large applications with two additions: a scheme for separate compilation of modules, and a hybrid technique combining region inference with tracing garbage collection.[11][12]

Generalization to new language environments

Following the development of ML Kit, regions began to be generalized to other language environments:

  • Various extensions to the C programming language:
    • The safe C dialect Cyclone, which among many other features adds support for explicit regions, and evaluates the impact of migrating existing C applications to use them.[13][14][15]
    • An extension to C called RC[16] was implemented that uses explicitly-managed regions, but also uses reference counting on regions to guarantee memory safety by ensuring that no region is freed prematurely.[17][18] Regions decrease the overhead of reference counting, since references internal to regions don't require counts to be updated when they're modified. RC includes an explicit static type system for regions that allows some reference count updates to be eliminated.[19]
    • A restriction of C called Control-C limits programs to use regions (and only a single region at a time), as part of its design to statically ensure memory safety.[20]
  • Regions were implemented for a subset of Java,[21] and became a critical component of memory management in Real time Java, which combines them with ownership types to demonstrate object encapsulation and eliminate runtime checks on region deallocation.[22][23][24] More recently, a semi-automatic system was proposed for inferring regions in embedded real-time Java applications, combining a compile-time static analysis, a runtime region allocation policy, and programmer hints.[25][26] Regions are a good fit for real-time computing because their time overhead is statically predictable, without the complexity of incremental garbage collection.
  • They were implemented for the logic programming languages Prolog[27][28] and Mercury[29][30] by extending Tofte and Talpin's region inference model to support backtracking and cuts.
  • Region-based storage management is used throughout the parallel programming language ParaSail. Due to the lack of explicit pointers in ParaSail,[31] there is no need for reference counting.

Standard library support

  • C++ features std::pmr::monotonic_buffer_resource inside the std::pmr library (defined in header <memory_resource>).[32]
  • Java 21 added a Java API to allocate and release Arenas.[33] The stated purpose of these is to improve safe integration with native libraries so as to prevent JVM memory leaks and to reduce the risk of JVM memory corruption by native code.[34] Arenas are a part of the Java Foreign Function and Memory Interface (which was introduced in Java 22 and made stable in Java 25), which is a successor to Java Native Interface (JNI), and includes classes like java.lang.foreign.Arena, java.lang.foreign.MemorySegment, and others.[35]
  • Go features an arena library, with arena.Arena.[36]
  • Zig has the std.heap.ArenaAllocator struct.[37]
  • Odin has the mem.Arena type within the core package.[38]

Disadvantages

Systems using regions may experience issues where regions become very large before they are deallocated and contain a large proportion of dead data; these are commonly called "leaks" (even though they are eventually freed). Eliminating leaks may involve restructuring the program, typically by introducing new, shorter-lifetime regions. Debugging this type of problem is especially difficult in systems using region inference, where the programmer must understand the underlying inference algorithm, or examine the verbose intermediate representation, to diagnose the issue. Tracing garbage collectors are more effective at deallocating this type of data in a timely manner without program changes; this was one justification for hybrid region/GC systems.[11] On the other hand, tracing garbage collectors can also exhibit subtle leaks, if references are retained to data which will never be used again.

Region-based memory management works best when the number of regions is relatively small and each contains many objects; programs that contain many sparse regions will exhibit internal fragmentation, leading to wasted memory and a time overhead for region management. Again, in the presence of region inference this problem can be more difficult to diagnose.

Hybrid methods

As mentioned above, RC uses a hybrid of regions and reference counting, limiting the overhead of reference counting since references internal to regions don't require counts to be updated when they're modified. Similarly, some mark-region hybrid methods combine tracing garbage collection with regions; these function by dividing the heap into regions, performing a mark-sweep pass in which any regions containing live objects are marked, and then freeing any unmarked regions. These require continual defragmentation to remain effective.[39]

Notes

  1. For MFT and OS/VS1
  2. For MVT, OS/VS2 and later MVS systems.
  3. For OS/360 and OS/VS1
  4. For OS/VS2 and later MVS systems.

References

Related Articles

Wikiwand AI