Exactly-once pin cleanup 2026-11-05
Exactly-once pin cleanup gives one owner responsibility for ending a particular pinning lifetime, despite several possible completion paths. The resource must remain pinned until its consumers finish, and its cleanup must occur once after that condition is met. This combines the safety requirement of asynchronous pin release with a liveness requirement: a pin left permanently owned by nobody can obstruct later memory compaction.
A useful state machine has states “submission in progress”, “completion pending” and “released”. A submission error that guarantees no future consumer can access the address may take the first state directly to released. A pending result transfers cleanup responsibility to a completion path. Immediate completion must follow the API's stated rule about whether a callback will also arrive; it must not independently trigger two owners to dispose of the same handle. Microsoft's native-call example explicitly handles submission exceptions and funnels immediate or deferred completion into cleanup.[ref]
Memory and Span usage guidelines by Microsoft. Rule10, exception branch, non-pending result branch and completion routine
For an original race example, suppose a completion callback and an error-reporting path can both observe an operation as finished. A plain sequence “test flag; if clear, set flag; release pin” can race: both callers read clear before either writes set, and both release. A synchronized ownership claim, using a mutex or an appropriate compare-and-swap transition, can ensure that only one caller changes pending to released. The losing caller performs no cleanup. This is a schematic ownership protocol, not a claim that every pin API tolerates arbitrary racing callbacks.
For a count of releases on one acquired handle, the at-most-once condition is . The requirement that cleanup eventually occurs after all promised access has ended supplies at final completion, so together they give . A one-time flag alone establishes neither the end of access nor eventual execution of the winning path.
The operation's state must also stay alive while any callback can inspect it. Reference counting can protect that state independently of the pin being released. Marking a handle released does not permit another caller to access freed callback state. Keeping resource ownership, callback-state lifetime and data synchronization separate makes the protocol easier to verify. The primary task-based memory rule similarly requires all consumer access to finish by task termination.[ref]
Memory and Span usage guidelines by Microsoft. Rule4, consumer lifetime through terminal task state
External memory fragmentation 2026-11-05
External memory fragmentation occurs when free capacity is divided among separated blocks so that a request for one sufficiently large contiguous block cannot be satisfied. It concerns the layout available to a particular allocator, rather than only the total free computer memory. In a fixed nonmoving pool, occupied blocks can separate free intervals that would have been useful if adjacent.[ref]
Operating Systems: Three Easy Pieces by Remzi H. Arpaci-Dusseau and others. Chapter17, “Free-Space Management”, introduction and §17.1, variable-size allocation and nonrelocatable live blocks
For an original example, a104-byte pool contains free blocks of24,40 and16 bytes, separated by occupied blocks of16 and8 bytes. Total free capacity is80 bytes, but the largest free memory block is only40 bytes. A contiguous48-byte request therefore fails in this closed pool, assuming live blocks cannot move and ignoring metadata/alignment overhead.
Memory block coalescing merges adjacent free intervals. If the occupied16-byte block between the first two holes is released, the neighboring24,16 and40-byte intervals can form one80-byte free block. Coalescing does not merge across a still-occupied interval. Memory compaction instead moves live data to collect free space, which requires a way to preserve or update references. An allocator generally cannot relocate an arbitrary live block behind an unchanged C programming language pointer.[ref]
Operating Systems: Three Easy Pieces by Remzi H. Arpaci-Dusseau and others. §17.1, relocation assumption; §17.2, splitting and coalescing
The illustrative measure equals0.5 for the original layout, but it does not say that half the memory is universally unusable: requests no larger than16 bytes can still fit. Actual allocation success depends on request sizes and allocator policy. Internal fragmentation instead concerns unused space inside allocated units.
The contiguity requirement also needs its address space specified. A virtually contiguous block can use noncontiguous physical page frames through virtual memory. Physical page fragmentation and an application's heap holes are therefore different allocation-layer questions. Changing the physical mapping alone does not merge separated virtual holes around live objects.
Memory block coalescing 2026-11-05
Memory block coalescing merges adjacent free allocation blocks into a larger free block. It improves the allocator's representation and use of free space without moving intervening live objects. The mechanism is useful after deallocation, when a returned block may adjoin free neighbors. A free list records the resulting regions; it can be implemented using a list or another search structure.[ref]
Operating Systems: Three Easy Pieces by Remzi H. Arpaci-Dusseau and others. Chapter17, §17.2, “Splitting and Coalescing”, printed pp.3–4
Using half-open address intervals, two free blocks and can become . Their usable bookkeeping lengths add: . For an original example, suppose free intervals occupy offsets and while the intervening16-byte block is live. Releasing that middle block makes all of free, so merging its two neighbors produces an80-byte block. An occupied8-byte block after offset80 still separates this region from a later free interval.
An address-aware insertion can check the predecessor's end against the new block's start and the successor's start against its end. When both are adjacent, all three join. An implementation may delay merging, but a search that examines only unmerged entries can miss a sufficiently large contiguous region. Coalescing policy and allocation search therefore affect observed success as well as the physical occupancy pattern.
These examples omit block headers and other allocator metadata. A real merge must maintain those structures, preserve memory alignment guarantees and avoid treating an occupied or separately managed region as free. Coalescing does not cure a hole separated from its neighbor by a still-live allocation; that requires deallocation, a different placement strategy or a valid memory compaction mechanism.
Memory compaction 2026-11-05
Memory compaction moves live data so that free computer memory can be collected into larger contiguous regions. It changes data placement, while memory block coalescing combines intervals that are already free and adjacent. Compaction therefore needs a way to preserve the meaning of references to moved data. An ordinary allocator cannot freely move an arbitrary live C programming language allocation behind unchanged raw pointers.[ref]
Operating Systems: Three Easy Pieces by Remzi H. Arpaci-Dusseau and others. Chapter17, §17.1, live-allocation nonrelocation assumption and compaction discussion, printed pp.2–3
Consider the104-byte pool with free blocks of24,40 and16 bytes, separated by live blocks of16 and8 bytes. Its total free capacity is80 bytes, but its largest free memory block is40 bytes. If both live blocks are movable, placing the16-byte object at offset0 and the8-byte object at offset16 creates one free80-byte interval from24 to104. No additional capacity is created; its contiguity improves.
The references must follow each movable memory object. A reference to offset24 in the original first object becomes offset0; a reference five bytes inside that object changes from29 to5. A garbage collection system with accurate reference information or another suitable level of indirection can arrange such updates. Blindly replacing every integer equal to an old address would corrupt unrelated data.
Physical-page compaction is a different allocation-layer application. An operating system can move eligible pages while preserving a process's virtual memory addresses through updated mappings. Linux's documented compaction mechanism seeks contiguous free physical blocks, including for large-page allocation; moving pages also has execution costs and can affect latency.[ref]
Documentation for /proc/sys/vm/ in the Linux kernel documentation. Linux v6.12 documentation, compact_memory and compaction_proactiveness sections
Such physical rearrangement does not automatically combine separated virtual heap holes. The address space, movable objects and reference-preservation mechanism must accompany a compaction claim. Movable memory object 2026-11-05
A movable memory object is a live allocation that its memory-management system may relocate while preserving its identity and valid references. Moving the stored bytes is only part of the operation: references must be updated, or an indirection mechanism must continue to locate the object. This capability permits memory compaction. Microsoft's primary runtime documentation describes separate marking, reference-relocation and object-compaction phases of garbage collection.[ref]
Fundamentals of garbage collection by Microsoft. “What happens during a garbage collection”, marking, relocating and compacting phases
For an illustrative16-byte object initially starting at offset24, compaction may place its bytes at offset0. A reference to its beginning then changes from24 to0; a reference five bytes inside it changes from29 to5. In general, moving the base from to maps a tracked interior reference to . An unrelated integer with value29 must not be rewritten merely because it resembles the old address.
The memory manager therefore needs reliable knowledge of roots and references, including those held in other live objects. A garbage collection root can keep an object reachable while its address changes. If an external consumer requires the address to stay fixed, a pinned memory object imposes a relocation constraint instead. Movability is a property of the reference-management arrangement, not simply of whether the object contains bytes that can be copied.
A raw C programming language pointer returned by an ordinary allocator generally prevents arbitrary hidden movement, because the allocator does not control all copies of that address.[ref]
Operating Systems: Three Easy Pieces by Remzi H. Arpaci-Dusseau and others. Chapter17, §17.1, nonrelocation assumption and tracked-reference discussion, printed pp.2–3
Physical-page movement under virtual memory is another layer: an operating system may preserve virtual addresses while changing the underlying physical mapping. It need not make the application-level object movable in its own address space. Pinned memory object 2026-11-05
A pinned memory object is a normally movable memory object whose address must remain unchanged during a specified operation. Pinning permits a pointer to remain usable when the code consuming it cannot participate in the runtime's relocation of references. The pinning lifetime is part of that guarantee: an address retained beyond the protected operation need not remain valid. Microsoft's language reference specifies that a
fixed statement prevents relocation during its body and restricts the declared pointer to that statement.[ref]Fixed statement in the C reference by Microsoft. Opening definition; array-element and object-field guarantees
For an original layout example, consider96 bytes with a pinned16-byte object at offsets and one other16-byte live object. The remaining64 bytes are free. Even if the other object can move anywhere, the pinned object divides the address range into regions of32 and48 bytes. A56-byte contiguous allocation cannot fit. Moving the other object into gives free intervals and , whose largest size is48 bytes. If both live objects could move, placing them in would instead leave a64-byte interval. Pinning can therefore limit memory compaction despite ample total free capacity.
The numerical layout omits allocation headers and memory alignment; a real allocator must include them. Pinning an object's address also does not by itself specify how its virtual memory pages are backed. The garbage collector's object-relocation guarantee and an operating system's physical-page residency rules concern different layers. Likewise, a pinned buffer in a storage cache may mean protection from eviction rather than this object-relocation guarantee.
Once the protected operation is finished, releasing its pin can restore the runtime's freedom to move the object during a later collection. The program still has to obey the language's reference-lifetime rules: releasing a pin does not convert an old unmanaged address into a relocatable reference. The general garbage collection process must update references when it moves surviving objects.[ref]
Fundamentals of garbage collection by Microsoft. “What happens during a garbage collection”, relocating and compacting phases
Pinning lifetime 2026-11-05
The pinning lifetime is the interval during which a pinned memory object must retain its address for all consumers entitled to use a non-relocatable pointer into it. The interval must cover actual memory access, rather than merely the act of handing out the address. Microsoft's
fixed statement guarantees address stability during its body and restricts its declared pointer to that statement; a pointer retained for later work needs a guarantee covering that later work too.[ref]Fixed statement in the C reference by Microsoft. Opening definition and array-element/object-field lifetime guarantees
For an original timing example, suppose a pin starts at0ms, an external operation receives the address at0.5ms, its submission function returns at1ms, and its final memory access finishes at5ms. Releasing the pin at the function's return leaves4ms of possible access after address stability has ended. Releasing it only after completion at5ms can cover the whole access interval. These times describe an illustrative protocol, not a timing claim about a particular runtime or device.
If the pin is active on , every access time must satisfy . With several users, the release point must follow the last access by all of them. A count of outstanding operations can enforce this rule: after two operations start, one completion changes the count from2 to1 and does not permit release; the last completion changes1 to0. Asynchronous pin release therefore depends on a completion guarantee. Requesting that unfinished work stop does not by itself demonstrate that its final access has ended.
The pinning lifetime and data synchronization solve different problems. Stable addresses do not prevent two users from changing the same bytes concurrently; a mutex or another synchronization mechanism may still be needed. Nor should a pin be held indefinitely just because longer protection seems convenient: pinning can obstruct memory compaction, as the pinned memory object layout example shows. The useful interval is long enough for the access contract and ends when that contract permits release.
