]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/java/memory/memory-core/src/main/java/org/apache/arrow/memory/README.md
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / java / memory / memory-core / src / main / java / org / apache / arrow / memory / README.md
1 <!--
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 -->
18 # Memory: Allocation, Accounting and Management
19
20 The memory management package contains all the memory allocation related items that Arrow uses to manage memory.
21
22
23 ## Key Components
24 Memory management can be broken into the following main components:
25
26 - Memory chunk allocation and fragmentation management
27 - `PooledByteBufAllocatorL` - A LittleEndian clone of Netty's jemalloc implementation
28 - `UnsafeDirectLittleEndian` - A base level memory access interface
29 - `LargeBuffer` - A buffer backing implementation used when working with data larger than one Netty chunk (default to 16mb)
30 - Memory limits & Accounting
31 - `Accountant` - A nestable class of lockfree memory accountors.
32 - Application-level memory allocation
33 - `BufferAllocator` - The public interface application users should be leveraging
34 - `BaseAllocator` - The base implementation of memory allocation, contains the meat of our the Arrow allocator implementation
35 - `RootAllocator` - The root allocator. Typically only one created for a JVM
36 - `ChildAllocator` - A child allocator that derives from the root allocator
37 - Buffer ownership and transfer capabilities
38 - `AllocationManager` - Responsible for managing the relationship between multiple allocators and a single chunk of memory
39 - `BufferLedger` - Responsible for allowing maintaining the relationship between an `AllocationManager`, a `BufferAllocator` and one or more individual `ArrowBuf`s
40 - Memory access
41 - `ArrowBuf` - The facade for interacting directly with a chunk of memory.
42
43
44 ## Memory Management Overview
45 Arrow's memory model is based on the following basic concepts:
46
47 - Memory can be allocated up to some limit. That limit could be a real limit (OS/JVM) or a locally imposed limit.
48 - Allocation operates in two phases: accounting then actual allocation. Allocation could fail at either point.
49 - Allocation failure should be recoverable. In all cases, the Allocator infrastructure should expose memory allocation failures (OS or internal limit-based) as `OutOfMemoryException`s.
50 - Any allocator can reserve memory when created. This memory shall be held such that this allocator will always be able to allocate that amount of memory.
51 - A particular application component should work to use a local allocator to understand local memory usage and better debug memory leaks.
52 - The same physical memory can be shared by multiple allocators and the allocator must provide an accounting paradigm for this purpose.
53
54 ## Allocator Trees
55
56 Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators are created as children of that allocator. The RootAllocator is responsible for being the master bookkeeper for memory allocations. All other allocators are created as children of this tree. Each allocator can first determine whether it has enough local memory to satisfy a particular request. If not, the allocator can ask its parent for an additional memory allocation.
57
58 ## Reserving Memory
59
60 Arrow provides two different ways to reserve memory:
61
62 - BufferAllocator accounting reservations:
63 When a new allocator (other than the `RootAllocator`) is initialized, it can set aside memory that it will keep locally for its lifetime. This is memory that will never be released back to its parent allocator until the allocator is closed.
64 - `AllocationReservation` via BufferAllocator.newReservation(): Allows a short-term preallocation strategy so that a particular subsystem can ensure future memory is available to support a particular request.
65
66 ## Memory Ownership, Reference Counts and Sharing
67 Many BufferAllocators can reference the same piece of memory at the same time. The most common situation for this is in the case of a Broadcast Join: in this situation many downstream operators in the same Arrowbit will receive the same physical memory. Each of these operators will be operating within its own Allocator context. We therefore have multiple allocators all pointing at the same physical memory. It is the AllocationManager's responsibility to ensure that in this situation, that all memory is accurately accounted for from the Root's perspective and also to ensure that the memory is correctly released once all BufferAllocators have stopped using that memory.
68
69 For simplicity of accounting, we treat that memory as being used by one of the BufferAllocators associated with the memory. When that allocator releases its claim on that memory, the memory ownership is then moved to another BufferLedger belonging to the same AllocationManager. Note that because a ArrowBuf.release() is what actually causes memory ownership transfer to occur, we always precede with ownership transfer (even if that violates an allocator limit). It is the responsibility of the application owning a particular allocator to frequently confirm whether the allocator is over its memory limit (BufferAllocator.isOverLimit()) and if so, attempt to aggressively release memory to ameliorate the situation.
70
71 All ArrowBufs (direct or sliced) related to a single BufferLedger/BufferAllocator combination share the same reference count and either all will be valid or all will be invalid.
72
73 ## Object Hierarchy
74
75 There are two main ways that someone can look at the object hierarchy for Arrow's memory management scheme. The first is a memory based perspective as below:
76
77 ### Memory Perspective
78 <pre>
79 + AllocationManager
80 |
81 |-- UnsignedDirectLittleEndian (One per AllocationManager)
82 |
83 |-+ BufferLedger 1 ==> Allocator A (owning)
84 | ` - ArrowBuf 1
85 |-+ BufferLedger 2 ==> Allocator B (non-owning)
86 | ` - ArrowBuf 2
87 |-+ BufferLedger 3 ==> Allocator C (non-owning)
88 | - ArrowBuf 3
89 | - ArrowBuf 4
90 ` - ArrowBuf 5
91 </pre>
92
93 In this picture, a piece of memory is owned by an allocator manager. An allocator manager is responsible for that piece of memory no matter which allocator(s) it is working with. An allocator manager will have relationships with a piece of raw memory (via its reference to UnsignedDirectLittleEndian) as well as references to each BufferAllocator it has a relationship to.
94
95 ### Allocator Perspective
96 <pre>
97 + RootAllocator
98 |-+ ChildAllocator 1
99 | | - ChildAllocator 1.1
100 | ` ...
101 |
102 |-+ ChildAllocator 2
103 |-+ ChildAllocator 3
104 | |
105 | |-+ BufferLedger 1 ==> AllocationManager 1 (owning) ==> UDLE
106 | | `- ArrowBuf 1
107 | `-+ BufferLedger 2 ==> AllocationManager 2 (non-owning)==> UDLE
108 | `- ArrowBuf 2
109 |
110 |-+ BufferLedger 3 ==> AllocationManager 1 (non-owning)==> UDLE
111 | ` - ArrowBuf 3
112 |-+ BufferLedger 4 ==> AllocationManager 2 (owning) ==> UDLE
113 | - ArrowBuf 4
114 | - ArrowBuf 5
115 ` - ArrowBuf 6
116 </pre>
117
118 In this picture, a RootAllocator owns three ChildAllocators. The first ChildAllocator (ChildAllocator 1) owns a subsequent ChildAllocator. ChildAllocator has two BufferLedgers/AllocationManager references. Coincidentally, each of these AllocationManager's is also associated with the RootAllocator. In this case, one of the these AllocationManagers is owned by ChildAllocator 3 (AllocationManager 1) while the other AllocationManager (AllocationManager 2) is owned/accounted for by the RootAllocator. Note that in this scenario, ArrowBuf 1 is sharing the underlying memory as ArrowBuf 3. However the subset of that memory (e.g. through slicing) might be different. Also note that ArrowBuf 2 and ArrowBuf 4, 5 and 6 are also sharing the same underlying memory. Also note that ArrowBuf 4, 5 and 6 all share the same reference count and fate.
119
120 ## Debugging Issues
121 The Allocator object provides a useful set of tools to better understand the status of the allocator. If in `DEBUG` mode, the allocator and supporting classes will record additional debug tracking information to better track down memory leaks and issues. To enable DEBUG mode, either enable Java assertions with `-ea` or pass the following system property to the VM when starting `-Darrow.memory.debug.allocator=true`. The BufferAllocator also provides a `BufferAllocator.toVerboseString()` which can be used in DEBUG mode to get extensive stacktrace information and events associated with various Allocator behaviors.