User:Glhsla92/Memory management

Memory management is a form of resource management applied to computer memory. The essential requirement of memory management is to provide ways to dynamically allocate portions of memory to programs at their request, and free it for reuse when no longer needed. This is critical to any advanced computer system where more than a single process might be underway at any time.

Several methods have been devised that increase the effectiveness of memory management. Virtual memory systems separate the memory addresses used by a process from actual physical addresses, allowing separation of processes and increasing the size of the virtual address space beyond the available amount of RAM using paging or swapping to secondary storage. The quality of the virtual memory manager can have an extensive effect on overall system performance. The system allows a computer to appear as if it may have more memory available than physically present, thereby allowing multiple processes to share it.

In some operating systems, e.g. OS/360 and successors, memory is managed by the operating system. In other operating systems, e.g. Unix-like operating systems, memory is managed at the application level.

Memory management within an address space is generally categorized as either manual memory management or automatic memory management.

Manual memory management


The task of fulfilling an allocation request consists of locating a block of unused memory of sufficient size. Memory requests are satisfied by allocating portions from a large pool of memory called the heap or free store. At any given time, some parts of the heap are in use, while some are "free" (unused) and thus available for future allocations. In the C language, the function which allocates memory from the heap is called malloc and the function which takes previously allocated memory and marks it as "free" (to be used by future allocations) is called free.

Several issues complicate the implementation, such as external fragmentation, which arises when there are many small gaps between allocated memory blocks, which invalidates their use for an allocation request. The allocator's metadata can also inflate the size of (individually) small allocations. This is often managed by chunking. The memory management system must track outstanding allocations to ensure that they do not overlap and that no memory is ever "lost" (i.e. that there are no "memory leaks").

Efficiency
The specific dynamic memory allocation algorithm implemented can impact performance significantly. A study conducted in 1994 by Digital Equipment Corporation illustrates the overheads involved for a variety of allocators. The lowest average instruction path length required to allocate a single memory slot was 52 (as measured with an instruction level profiler on a variety of software).

Implementations
Since the precise location of the allocation is not known in advance, the memory is accessed indirectly, usually through a pointer reference. The specific algorithm used to organize the memory area and allocate and deallocate chunks is interlinked with the kernel, and may use any of the following methods:

Fixed-size blocks allocation
Fixed-size blocks allocation, also called memory pool allocation, uses a free list of fixed-size blocks of memory (often all of the same size). This works well for simple embedded systems where no large objects need to be allocated but suffers from fragmentation especially with long memory addresses. However, due to the significantly reduced overhead, this method can substantially improve performance for objects that need frequent allocation and deallocation, and so it is often used in video games.

Buddy blocks
In this system, memory is allocated into several pools of memory instead of just one, where each pool represents blocks of memory of a certain power of two in size, or blocks of some other convenient size progression. All blocks of a particular size are kept in a sorted linked list or tree and all new blocks that are formed during allocation are added to their respective memory pools for later use. If a smaller size is requested than is available, the smallest available size is selected and split. One of the resulting parts is selected, and the process repeats until the request is complete. When a block is allocated, the allocator will start with the smallest sufficiently large block to avoid needlessly breaking blocks. When a block is freed, it is compared to its buddy. If they are both free, they are combined and placed in the correspondingly larger-sized buddy-block list.

Slab allocation
This memory allocation mechanism preallocates memory chunks suitable to fit objects of a certain type or size. These chunks are called caches and the allocator only has to keep track of a list of free cache slots. Constructing an object will use any one of the free cache slots and destructing an object will add a slot back to the free cache slot list. This technique alleviates memory fragmentation and is efficient as there is no need to search for a suitable portion of memory, as any open slot will suffice.

Stack allocation
Many Unix-like systems as well as Microsoft Windows implement a function called alloca for dynamically allocating stack memory in a way similar to the heap-based malloc. A compiler typically translates it to inlined instructions manipulating the stack pointer. Although there is no need of manually freeing memory allocated this way as it is automatically freed when the function that called alloca returns, there exists a risk of overflow. And since alloca is an ad hoc expansion seen in many systems but never in POSIX or the C standard, its behavior in case of a stack overflow is undefined.

A safer version of alloca called _malloca, which reports errors, exists on Microsoft Windows. It requires the use of _freea. gnulib provides an equivalent interface, albeit instead of throwing an SEH exception on overflow, it delegates to malloc when an overlarge size is detected. A similar feature can be emulated using manual accounting and size-checking, such as in the uses of alloca_account in glibc.

Automated memory management
The proper management of memory in an application is a difficult problem, and several different strategies for handling memory management have been devised.

Automatic management of call stack variables
In many programming language implementations, the runtime environment for the program automatically allocates memory in the call stack for non-static local variables of a subroutine, called automatic variables, when the subroutine is called, and automatically releases that memory when the subroutine is exited. Special declarations may allow local variables to retain values between invocations of the procedure, or may allow local variables to be accessed by other subroutines. The automatic allocation of local variables makes recursion possible, to a depth limited by available memory.

Garbage collection
Garbage collection is a strategy for automatically detecting memory allocated to objects that are no longer usable in a program, and returning that allocated memory to a pool of free memory locations. This method is in contrast to "manual" memory management where a programmer explicitly codes memory requests and memory releases in the program. While automatic garbage collection has the advantages of reducing programmer workload and preventing certain kinds of memory allocation bugs, garbage collection does require memory resources of its own, and can compete with the application program for processor time.

Reference counting
Reference counting is a strategy for detecting that memory is no longer usable by a program by maintaining a counter for how many independent pointers point to the memory. Whenever a new pointer points to a piece of memory, the programmer is supposed to increase the counter. When the pointer changes where it points, or when the pointer is no longer pointing to anything or has itself been freed, the counter should decrease. When the counter drops to zero, the memory should be considered unused and freed. Some reference counting systems require programmer involvement and some are implemented automatically by the compiler. A disadvantage of reference counting is that circular references can develop which cause a memory leak to occur. This can be mitigated by either adding the concept of a "weak reference" (a reference that does not participate in reference counting, but is notified when the thing it is pointing to is no longer valid) or by combining reference counting and garbage collection together.

Memory pools
A memory pool is a technique of automatically deallocating memory based on the state of the application, such as the lifecycle of a request or transaction. The idea is that many applications execute large chunks of code which may generate memory allocations, but that there is a point in execution where all of those chunks are known to be no longer valid. For example, in a web service, after each request the web service no longer needs any of the memory allocated during the execution of the request. Therefore, rather than keeping track of whether or not memory is currently being referenced, the memory is allocated according to the request or lifecycle stage with which it is associated. When that request or stage has passed, all associated memory is deallocated simultaneously.

Systems with virtual memory
Virtual memory is a method of decoupling the memory organization from the physical hardware. The applications operate on memory via virtual addresses. Each attempt by the application to access a particular virtual memory address results in the virtual memory address being translated to an actual physical address. In this way the addition of virtual memory enables granular control over memory systems and methods of access.

In virtual memory systems the operating system limits how a process can access the memory. This feature, called memory protection, can be used to disallow a process to read or write to memory that is not allocated to it, preventing malicious or malfunctioning code in one program from interfering with the operation of another.

Even though the memory allocated for specific processes is normally isolated, processes sometimes need to be able to share information. Shared memory is one of the fastest techniques for inter-process communication.

Memory is usually classified by access rate into primary storage and secondary storage. Memory management systems, among other operations, also handle the moving of information between these two levels of memory.

Memory management in OS/360 and successors
IBM System/360 does not support virtual memory. Memory isolation of jobs is optionally accomplished using protection keys, assigning storage for each job a different key, 0 for the supervisor or 1–15. Memory management in OS/360 is a supervisor function. Storage is requested using the  macro and freed using the   macro, which result in a call to the supervisor (SVC) to perform the operation.

In OS/360 the details vary depending on how the system is generated, e.g., for PCP, MFT, MVT.

In OS/360 MVT, suballocation within a job's region or the shared System Queue Area (SQA) is based on subpools, areas a multiple of 2 KB in size—the size of an area protected by a protection key. Subpools are numbered 0–255. Within a region subpools are assigned either the job's storage protection or the supervisor's key, key 0. Subpools 0–127 receive the job's key. Initially only subpool zero is created, and all user storage requests are satisfied from subpool 0, unless another is specified in the memory request. Subpools 250–255 are created by memory requests by the supervisor on behalf of the job. Most of these are assigned key 0, although a few get the key of the job. Subpool numbers are also relevant in MFT, although the details are much simpler. MFT uses fixed partitions redefinable by the operator instead of dynamic regions and PCP has only a single partition.

Each subpool is mapped by a list of control blocks identifying allocated and free memory blocks within the subpool. Memory is allocated by finding a free area of sufficient size, or by allocating additional blocks in the subpool, up to the region size of the job. It is possible to free all or part of an allocated memory area.

The details for OS/VS1 are similar to those for MFT and for MVT; the details for OS/VS2 are similar to those for MVT, except that the page size is 4 KiB. For both OS/VS1 and OS/VS2 the shared System Queue Area (SQA) is nonpageable.

In MVS the address space includes an additional pageable shared area, the Common Storage Area (CSA), and an additional private area, the System Work area (SWA). Also, the storage keys 0-7 are all reserved for use by privileged code.

Memory Management in Linux
Linux's memory management is primarily organized through the use of virtual memory that supports both paging and (limited) segmentation. Allocation is handled via multiple mechanisms, such as the Buddy System for physical memory and slab allocation for kernel objects. The kernel assigns virtual address spaces to processes, and the management is facilitated by the Multi-Level Page Tables which help efficiently handle address translation from linear to physical addresses. Each process is given a separate virtual address space, ensuring isolation and security across processes.

Linux incorporates features like Huge Pages and Transparent HugePages (THP) to optimize TLB efficiency and performance, automatically managing large page mappings to reduce manual configuration. It organizes memory into zones tailored to hardware capabilities, supporting DMA and high memory operations. NUMA support enhances performance by managing memory local to CPU nodes. This reclaim mechanism ensures stable operation under high loads through asynchronous and synchronous reclaims, while memory compaction addresses fragmentation issues.

In extreme cases, the OOM Killer maintains system stability by terminating the least critical, memory-heavy processes. These capabilities make Linux highly adaptable and robust, ideal for a range of computing environments from embedded systems to supercomputers.

Note that Linux’s use of segmentation is limited. According to Understanding The Linux Kernel: "Segmentation has been included in 80x86 microprocessors to encourage programmers to split their applications into logically related entities, such as subroutines or global and local data areas…In fact, segmentation and paging are somewhat redundant, because both can be used to separate the physical address spaces of processes: segmentation can assign a different linear address space to each process, while paging can map the same linear address space into different physical address spaces. Linux prefers paging to segmentation for the following reasons:

• Memory management is simpler when all processes use the same segment register values—that is, when they share the same set of linear addresses.

• One of the design objectives of Linux is portability to a wide range of architectures; RISC architectures, in particular, have limited support for segmentation.

The 2.6 version of Linux uses segmentation only when required by the 80x86 architecture."

Memory Management in Windows
Windows operates its memory management through the Windows NT Virtual Memory Manager (VMM), which uses a combination of paging and segmentation to manage virtual memory - in particular, a form of demand paging with a sophisticated algorithm for page replacement to optimize the use of physical memory. System memory is categorized into two primary memory pools - the paged pool (which can be swapped to disk) and nonpaged pool, which remains in memory.

The system ensures process isolation through unique virtual address spaces, and leverages a feature called the Working Set to keep track of the pages that each process is currently using. The Working set is sized dynamically to optimize the amount of physical memory available.

In server and enterprise environments, Windows employs memory deduplication technology to optimize the use of physical memory by identifying and consolidating duplicate memory pages. For critical operations, Windows supports memory mirroring, where identical copies of memory are maintained to prevent data loss in the event of hardware error.

Windows also has optimizations for Non-Uniform Memory Access (NUMA) architectures in localizing memory accesses to memory nodes nearest to the processor executing a given thread.

Memory Management in MacOS
MacOS utilizes a modern technique involving compressed memory and an efficient paging system. Like Linux, macOS supports a multi-level page table structure. The OS incorporates features such as Automatic Reference Counting (ARC) to manage memory in Objective-C and Swift applications. MacOS also uses a unified buffer cache that integrates the management of file and network data.

The Mach-based kernel architecture characterizing macOS system operations features a “fully-integrated virtual memory system ” that consists of physical RAM and drive storage. Paging schemes are managed by Universal Page Lists (UPLs) which allow fine-grained control over page caching, accessing, and management, thereby optimizing system responses to varying workload demands.

Other unique features include the extensive use of CoW to optimize memory usage when duplicating memory objects by deferring memory copying until absolutely necessary. Recent versions of macOS have introduced memory compression to keep more data in RAM rather than paging it out to disk rather than swapping out pages to disk. The VM is also designed with predictive paging needs based on historical access patterns.

Memory Management in Android
Android, based on the Linux kernel, adapts its memory management strategies to suit mobile devices. In particular, it uses Linux's paging capabilities and memory mapping but also includes additional features such as the Low Memory Killer daemon that monitors the system and kills less important processes when the memory is low in order to minimize power consumption and efficient background processing.

Pages are either cached (backed by file on storage) or anonymous (not backed). If cached, pages can be private (owned by a single process) or shared (used by multiple processes). All cached pages are either clean (unmodified) or dirty (modified) and are dealt with according to private/shared status.

Devices contain three different types of memory: RAM, zRAM, and storage. Unlike other Linux implementations, storage is not used for swap space in Android device management as frequent writing causes wear and shortens the storage medium. Instead, swap space is implemented using zRAM (a dynamic partition of RAM) where data is compressed when placed into and decompressed when copied out of zRAM.

Android automates memory management via a managed environment through ART (Android runtime) (previously Dalvik) which introduces ahead-of-time (AOT) compilation. Memory is also sorted into generational heaps (Young, Older, and Permanent) based on object lifespan and size, optimizing garbage collection efficiency by pre-segregating objects.

Each app process has a defined virtual memory range, with system-enforced max heap sizes. Exceeding limits trigger an OutOfMemoryError, and Android reclaims memory by scanning heap post-garbage collection and freeing unused pages. Non-foreground apps are kept in a cached state for quick switching, with system performance depending on minimizing cached memory use to avoid termination.

Memory Management in iOS
iOS memory management is primarily handled through Automatic Reference Counting (ARC), a compiler feature which automates the process of memory allocation and deallocation for Objective-C objects. For system-level memory management, iOS uses an active memory compression technique, which reduces the physical memory usage by compressing inactive pages rather than paging them out to disk as iOS devices do not have traditional swap space.

ARC automatically manages objects’ lifecycles by keeping track of references to class instances and deallocating instances that are not referenced. Two key challenges addressed by ARC include the resolution of strong reference cycles between two class instances by using weak (reference that does not keep a strong hold on the instance it refers to) and unowned (similar to weak but used when the other instance has the same or longer lifetime) references and preventing instances from premature deallocation.

iOS utilizes a virtual memory system requiring apps to release memory when the system encounters memory pressure (available memory is low and the system cannot meet app demands). Apps may be terminated if they do not sufficiently release memory after receiving low-memory warnings, also known as a “jetsam event”, named after the system decision to “jettison” an app. Jetsam event reports allow the jettisoned process a “reason key” that allows it to identify the conditions that led to the jetsam event. Conditions include the per-process-limit, vm-pageshortage, vnode-limit, highwater, fc-thrashing, and are supplemented with additional information such as the uuid (version of app), states (memory use state), lifetimeMax (highest number of memory pages allocated during the process’ lifetime), coalition (identifies related processes and their memory uses) in the report to inform developers about memory management inadequacies in their applications.