lidavidm commented on a change in pull request #12603: URL: https://github.com/apache/arrow/pull/12603#discussion_r827954312
########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. Review comment: Use the actual module name, `arrow-memory-core` etc. ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. Review comment: ```suggestion Arrow offers a high level of abstraction providing several access APIs to read/write data into direct memory. ``` ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). + +Unlike (Direct)ByteBuffer, it has reference counting built in (`see the next section`). + +Reference counting +================== + +Direct memory involve more activities than allocate and deallocate because allocators (thru pool/cache) +allocate buffers (ArrowBuf). + +Arrow uses manual reference counting to track whether a buffer is in use, or can be deallocated or returned +to the allocator's pool. This simply means that each buffer has a counter keeping track of the number of references to +this buffer, and end user is responsible for properly incrementing/decrementing the counter according the buffer is used. + +In Arrow, each ArrowBuf has an associated ReferenceManager that tracks the reference count, which can be retrieved +with ArrowBuf.getReferenceManager(). The reference count can be updated with ``ReferenceManager.release`` and +``ReferenceManager.retain``. + +Of course, this is tedious and error-prone, so usually, instead of directly working with buffers, we should use +higher-level APIs like ValueVector. Such classes generally implement Closeable/AutoCloseable and will automatically +decrement the reference count when closed method. + +.. code-block:: + + |__ A = Allocator + |____ B = IntVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + |____ C = VarcharVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + +Allocators implement AutoCloseable as well. In this case, closing the allocator will check that all buffers +obtained from the allocator are closed. If not, ``close()`` method will raise an exception; this helps track +memory leaks from unclosed buffers. + +As you see reference counting needs to be handled properly by us, if at some point you need to ensuring that an +independent section of code has `fully cleaned up all allocated buffers while still maintaining a global memory limit +through the RootAllocator`, well ``BufferAllocator.newChildAllocator`` is what you should use. + +Reason To Use Direct Memory +=========================== + +* When `writing an ArrowBuf`_ we use the direct buffer (``nioBuffer()`` returns a DirectByteBuffer) and the JVM `will attempt to avoid copying the buffer's content to (or from) an intermediate buffer`_ so it makes I/O (and hence IPC) faster. +* We can `directly wrap a native memory address`_ instead of having to copy data for JNI (where in implementing the C Data Interface we can directly create `Java ArrowBufs that directly correspond to the C pointers`_). +* Conversely in JNI, we can directly use `Java ArrowBufs in C++`_ without having to copy data. + +So basically #1 is more efficient I/O, and #2/#3 is better integration with JNI code. + +Development Guidelines +====================== + +* Use the BufferAllocator interface in APIs instead of RootAllocator. +* Applications should generally create one RootAllocator at the start of the program. +* Remember to close() allocators after use (whether they are child allocators or the RootAllocator), either manually or preferably via a try-with-resources statement. Review comment: ```suggestion Applications should generally: * Use the BufferAllocator interface in APIs instead of RootAllocator. * Create one RootAllocator at the start of the program. * ``close()`` allocators after use (whether they are child allocators or the RootAllocator), either manually or preferably via a try-with-resources statement. ``` ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). + +Unlike (Direct)ByteBuffer, it has reference counting built in (`see the next section`). + +Reference counting +================== + +Direct memory involve more activities than allocate and deallocate because allocators (thru pool/cache) +allocate buffers (ArrowBuf). + +Arrow uses manual reference counting to track whether a buffer is in use, or can be deallocated or returned +to the allocator's pool. This simply means that each buffer has a counter keeping track of the number of references to +this buffer, and end user is responsible for properly incrementing/decrementing the counter according the buffer is used. + +In Arrow, each ArrowBuf has an associated ReferenceManager that tracks the reference count, which can be retrieved +with ArrowBuf.getReferenceManager(). The reference count can be updated with ``ReferenceManager.release`` and +``ReferenceManager.retain``. + +Of course, this is tedious and error-prone, so usually, instead of directly working with buffers, we should use +higher-level APIs like ValueVector. Such classes generally implement Closeable/AutoCloseable and will automatically +decrement the reference count when closed method. + +.. code-block:: + + |__ A = Allocator + |____ B = IntVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + |____ C = VarcharVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + +Allocators implement AutoCloseable as well. In this case, closing the allocator will check that all buffers +obtained from the allocator are closed. If not, ``close()`` method will raise an exception; this helps track +memory leaks from unclosed buffers. + +As you see reference counting needs to be handled properly by us, if at some point you need to ensuring that an +independent section of code has `fully cleaned up all allocated buffers while still maintaining a global memory limit +through the RootAllocator`, well ``BufferAllocator.newChildAllocator`` is what you should use. + +Reason To Use Direct Memory +=========================== Review comment: ```suggestion Why Arrow Uses Direct Memory ============================ ``` ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + Review comment: Add a paragraph/short snippet demonstrating allocating a buffer from an allocator. ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,114 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `ArrowBuf`_ +* `Reference counting`_ Review comment: Let's not link into random source files either. We don't have to link everything here. ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). + +Unlike (Direct)ByteBuffer, it has reference counting built in (`see the next section`). + +Reference counting +================== + +Direct memory involve more activities than allocate and deallocate because allocators (thru pool/cache) +allocate buffers (ArrowBuf). + +Arrow uses manual reference counting to track whether a buffer is in use, or can be deallocated or returned +to the allocator's pool. This simply means that each buffer has a counter keeping track of the number of references to +this buffer, and end user is responsible for properly incrementing/decrementing the counter according the buffer is used. + +In Arrow, each ArrowBuf has an associated ReferenceManager that tracks the reference count, which can be retrieved +with ArrowBuf.getReferenceManager(). The reference count can be updated with ``ReferenceManager.release`` and +``ReferenceManager.retain``. + +Of course, this is tedious and error-prone, so usually, instead of directly working with buffers, we should use +higher-level APIs like ValueVector. Such classes generally implement Closeable/AutoCloseable and will automatically +decrement the reference count when closed method. Review comment: ```suggestion decrement the reference count when closed. ``` ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, Review comment: ```suggestion ArrowBuf represents a single, contiguous region of `direct memory`_. It consists of an address and a length, ``` ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. Review comment: On second thought, maybe just remove this paragraph. ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). + +Unlike (Direct)ByteBuffer, it has reference counting built in (`see the next section`). + +Reference counting +================== + +Direct memory involve more activities than allocate and deallocate because allocators (thru pool/cache) +allocate buffers (ArrowBuf). + +Arrow uses manual reference counting to track whether a buffer is in use, or can be deallocated or returned +to the allocator's pool. This simply means that each buffer has a counter keeping track of the number of references to +this buffer, and end user is responsible for properly incrementing/decrementing the counter according the buffer is used. + +In Arrow, each ArrowBuf has an associated ReferenceManager that tracks the reference count, which can be retrieved +with ArrowBuf.getReferenceManager(). The reference count can be updated with ``ReferenceManager.release`` and +``ReferenceManager.retain``. + +Of course, this is tedious and error-prone, so usually, instead of directly working with buffers, we should use +higher-level APIs like ValueVector. Such classes generally implement Closeable/AutoCloseable and will automatically +decrement the reference count when closed method. + +.. code-block:: + + |__ A = Allocator + |____ B = IntVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + |____ C = VarcharVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + +Allocators implement AutoCloseable as well. In this case, closing the allocator will check that all buffers +obtained from the allocator are closed. If not, ``close()`` method will raise an exception; this helps track +memory leaks from unclosed buffers. + +As you see reference counting needs to be handled properly by us, if at some point you need to ensuring that an +independent section of code has `fully cleaned up all allocated buffers while still maintaining a global memory limit +through the RootAllocator`, well ``BufferAllocator.newChildAllocator`` is what you should use. + +Reason To Use Direct Memory +=========================== + +* When `writing an ArrowBuf`_ we use the direct buffer (``nioBuffer()`` returns a DirectByteBuffer) and the JVM `will attempt to avoid copying the buffer's content to (or from) an intermediate buffer`_ so it makes I/O (and hence IPC) faster. +* We can `directly wrap a native memory address`_ instead of having to copy data for JNI (where in implementing the C Data Interface we can directly create `Java ArrowBufs that directly correspond to the C pointers`_). +* Conversely in JNI, we can directly use `Java ArrowBufs in C++`_ without having to copy data. + +So basically #1 is more efficient I/O, and #2/#3 is better integration with JNI code. + +Development Guidelines +====================== + +* Use the BufferAllocator interface in APIs instead of RootAllocator. +* Applications should generally create one RootAllocator at the start of the program. +* Remember to close() allocators after use (whether they are child allocators or the RootAllocator), either manually or preferably via a try-with-resources statement. + +Debugging Memory Leaks/Allocation +================================= + +Allocators have a debug mode, that makes it easier to figure out where a leak is originated (Consider to add this +parameter to your application: ``-Darrow.memory.debug.allocator=true``). This parameter enable to create an historical log +about the memory allocation. + +Arrow modules use logback to collect logs, configure it properly to see your logs (create ``logback-test.xml`` file on +resources folder and your project could read that by conventions). Review comment: We use slf4j. Applications can configure their favorite backend. ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). + +Unlike (Direct)ByteBuffer, it has reference counting built in (`see the next section`). + +Reference counting +================== + +Direct memory involve more activities than allocate and deallocate because allocators (thru pool/cache) +allocate buffers (ArrowBuf). + +Arrow uses manual reference counting to track whether a buffer is in use, or can be deallocated or returned +to the allocator's pool. This simply means that each buffer has a counter keeping track of the number of references to +this buffer, and end user is responsible for properly incrementing/decrementing the counter according the buffer is used. + +In Arrow, each ArrowBuf has an associated ReferenceManager that tracks the reference count, which can be retrieved +with ArrowBuf.getReferenceManager(). The reference count can be updated with ``ReferenceManager.release`` and +``ReferenceManager.retain``. + +Of course, this is tedious and error-prone, so usually, instead of directly working with buffers, we should use +higher-level APIs like ValueVector. Such classes generally implement Closeable/AutoCloseable and will automatically +decrement the reference count when closed method. + +.. code-block:: + + |__ A = Allocator + |____ B = IntVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + |____ C = VarcharVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + +Allocators implement AutoCloseable as well. In this case, closing the allocator will check that all buffers +obtained from the allocator are closed. If not, ``close()`` method will raise an exception; this helps track +memory leaks from unclosed buffers. + +As you see reference counting needs to be handled properly by us, if at some point you need to ensuring that an +independent section of code has `fully cleaned up all allocated buffers while still maintaining a global memory limit +through the RootAllocator`, well ``BufferAllocator.newChildAllocator`` is what you should use. + +Reason To Use Direct Memory +=========================== + +* When `writing an ArrowBuf`_ we use the direct buffer (``nioBuffer()`` returns a DirectByteBuffer) and the JVM `will attempt to avoid copying the buffer's content to (or from) an intermediate buffer`_ so it makes I/O (and hence IPC) faster. +* We can `directly wrap a native memory address`_ instead of having to copy data for JNI (where in implementing the C Data Interface we can directly create `Java ArrowBufs that directly correspond to the C pointers`_). +* Conversely in JNI, we can directly use `Java ArrowBufs in C++`_ without having to copy data. + +So basically #1 is more efficient I/O, and #2/#3 is better integration with JNI code. + +Development Guidelines +====================== + +* Use the BufferAllocator interface in APIs instead of RootAllocator. +* Applications should generally create one RootAllocator at the start of the program. +* Remember to close() allocators after use (whether they are child allocators or the RootAllocator), either manually or preferably via a try-with-resources statement. + +Debugging Memory Leaks/Allocation +================================= + +Allocators have a debug mode, that makes it easier to figure out where a leak is originated (Consider to add this +parameter to your application: ``-Darrow.memory.debug.allocator=true``). This parameter enable to create an historical log +about the memory allocation. + +Arrow modules use logback to collect logs, configure it properly to see your logs (create ``logback-test.xml`` file on +resources folder and your project could read that by conventions). + +This is an example of historical log enabled: Review comment: I think this is the regular exception you get, this doesn't show any extra debug info. ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). + +Unlike (Direct)ByteBuffer, it has reference counting built in (`see the next section`). Review comment: ```suggestion Unlike (Direct)ByteBuffer, it has reference counting built in, as discussed later. ``` ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). + +Unlike (Direct)ByteBuffer, it has reference counting built in (`see the next section`). + +Reference counting +================== + +Direct memory involve more activities than allocate and deallocate because allocators (thru pool/cache) +allocate buffers (ArrowBuf). + +Arrow uses manual reference counting to track whether a buffer is in use, or can be deallocated or returned +to the allocator's pool. This simply means that each buffer has a counter keeping track of the number of references to +this buffer, and end user is responsible for properly incrementing/decrementing the counter according the buffer is used. + +In Arrow, each ArrowBuf has an associated ReferenceManager that tracks the reference count, which can be retrieved +with ArrowBuf.getReferenceManager(). The reference count can be updated with ``ReferenceManager.release`` and +``ReferenceManager.retain``. + +Of course, this is tedious and error-prone, so usually, instead of directly working with buffers, we should use +higher-level APIs like ValueVector. Such classes generally implement Closeable/AutoCloseable and will automatically +decrement the reference count when closed method. + +.. code-block:: + + |__ A = Allocator + |____ B = IntVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + |____ C = VarcharVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + +Allocators implement AutoCloseable as well. In this case, closing the allocator will check that all buffers +obtained from the allocator are closed. If not, ``close()`` method will raise an exception; this helps track +memory leaks from unclosed buffers. + +As you see reference counting needs to be handled properly by us, if at some point you need to ensuring that an +independent section of code has `fully cleaned up all allocated buffers while still maintaining a global memory limit +through the RootAllocator`, well ``BufferAllocator.newChildAllocator`` is what you should use. + +Reason To Use Direct Memory +=========================== + +* When `writing an ArrowBuf`_ we use the direct buffer (``nioBuffer()`` returns a DirectByteBuffer) and the JVM `will attempt to avoid copying the buffer's content to (or from) an intermediate buffer`_ so it makes I/O (and hence IPC) faster. +* We can `directly wrap a native memory address`_ instead of having to copy data for JNI (where in implementing the C Data Interface we can directly create `Java ArrowBufs that directly correspond to the C pointers`_). +* Conversely in JNI, we can directly use `Java ArrowBufs in C++`_ without having to copy data. + +So basically #1 is more efficient I/O, and #2/#3 is better integration with JNI code. + +Development Guidelines +====================== + +* Use the BufferAllocator interface in APIs instead of RootAllocator. +* Applications should generally create one RootAllocator at the start of the program. +* Remember to close() allocators after use (whether they are child allocators or the RootAllocator), either manually or preferably via a try-with-resources statement. + +Debugging Memory Leaks/Allocation +================================= + +Allocators have a debug mode, that makes it easier to figure out where a leak is originated (Consider to add this +parameter to your application: ``-Darrow.memory.debug.allocator=true``). This parameter enable to create an historical log +about the memory allocation. Review comment: ```suggestion Allocators have a debug mode that makes it easier to figure out where a leak is originated. To enable it, enable assertions with ``-ea`` or set the system property, ``-Darrow.memory.debug.allocator=true``. When enabled, a log will be kept of allocations. ``` ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). Review comment: I think we can remove this, and move the section on direct memory below to be a subsection at the end of this section. ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf Review comment: I think this should go first ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). + +Unlike (Direct)ByteBuffer, it has reference counting built in (`see the next section`). + +Reference counting +================== + +Direct memory involve more activities than allocate and deallocate because allocators (thru pool/cache) +allocate buffers (ArrowBuf). + +Arrow uses manual reference counting to track whether a buffer is in use, or can be deallocated or returned +to the allocator's pool. This simply means that each buffer has a counter keeping track of the number of references to +this buffer, and end user is responsible for properly incrementing/decrementing the counter according the buffer is used. + +In Arrow, each ArrowBuf has an associated ReferenceManager that tracks the reference count, which can be retrieved +with ArrowBuf.getReferenceManager(). The reference count can be updated with ``ReferenceManager.release`` and +``ReferenceManager.retain``. + +Of course, this is tedious and error-prone, so usually, instead of directly working with buffers, we should use +higher-level APIs like ValueVector. Such classes generally implement Closeable/AutoCloseable and will automatically +decrement the reference count when closed method. + +.. code-block:: + + |__ A = Allocator + |____ B = IntVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + |____ C = VarcharVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + +Allocators implement AutoCloseable as well. In this case, closing the allocator will check that all buffers +obtained from the allocator are closed. If not, ``close()`` method will raise an exception; this helps track +memory leaks from unclosed buffers. + +As you see reference counting needs to be handled properly by us, if at some point you need to ensuring that an +independent section of code has `fully cleaned up all allocated buffers while still maintaining a global memory limit +through the RootAllocator`, well ``BufferAllocator.newChildAllocator`` is what you should use. + +Reason To Use Direct Memory +=========================== + +* When `writing an ArrowBuf`_ we use the direct buffer (``nioBuffer()`` returns a DirectByteBuffer) and the JVM `will attempt to avoid copying the buffer's content to (or from) an intermediate buffer`_ so it makes I/O (and hence IPC) faster. +* We can `directly wrap a native memory address`_ instead of having to copy data for JNI (where in implementing the C Data Interface we can directly create `Java ArrowBufs that directly correspond to the C pointers`_). +* Conversely in JNI, we can directly use `Java ArrowBufs in C++`_ without having to copy data. Review comment: "Conversely, on the C++ side of the JNI boundary, we can directly access the memory in ArrowBuf without copying data." ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). + +Unlike (Direct)ByteBuffer, it has reference counting built in (`see the next section`). + +Reference counting +================== + +Direct memory involve more activities than allocate and deallocate because allocators (thru pool/cache) +allocate buffers (ArrowBuf). + +Arrow uses manual reference counting to track whether a buffer is in use, or can be deallocated or returned +to the allocator's pool. This simply means that each buffer has a counter keeping track of the number of references to +this buffer, and end user is responsible for properly incrementing/decrementing the counter according the buffer is used. + +In Arrow, each ArrowBuf has an associated ReferenceManager that tracks the reference count, which can be retrieved +with ArrowBuf.getReferenceManager(). The reference count can be updated with ``ReferenceManager.release`` and +``ReferenceManager.retain``. + +Of course, this is tedious and error-prone, so usually, instead of directly working with buffers, we should use +higher-level APIs like ValueVector. Such classes generally implement Closeable/AutoCloseable and will automatically +decrement the reference count when closed method. + +.. code-block:: + + |__ A = Allocator + |____ B = IntVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + |____ C = VarcharVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + +Allocators implement AutoCloseable as well. In this case, closing the allocator will check that all buffers +obtained from the allocator are closed. If not, ``close()`` method will raise an exception; this helps track +memory leaks from unclosed buffers. + +As you see reference counting needs to be handled properly by us, if at some point you need to ensuring that an +independent section of code has `fully cleaned up all allocated buffers while still maintaining a global memory limit +through the RootAllocator`, well ``BufferAllocator.newChildAllocator`` is what you should use. + +Reason To Use Direct Memory +=========================== + +* When `writing an ArrowBuf`_ we use the direct buffer (``nioBuffer()`` returns a DirectByteBuffer) and the JVM `will attempt to avoid copying the buffer's content to (or from) an intermediate buffer`_ so it makes I/O (and hence IPC) faster. +* We can `directly wrap a native memory address`_ instead of having to copy data for JNI (where in implementing the C Data Interface we can directly create `Java ArrowBufs that directly correspond to the C pointers`_). Review comment: "Since Arrow always uses direct memory, JNI modules can directly wrap native memory addresses instead of copying data. We use this in modules like the C Data Interface." ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. Review comment: Link to BufferAllocator's javadocs. ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). + +Unlike (Direct)ByteBuffer, it has reference counting built in (`see the next section`). + +Reference counting +================== + +Direct memory involve more activities than allocate and deallocate because allocators (thru pool/cache) +allocate buffers (ArrowBuf). + +Arrow uses manual reference counting to track whether a buffer is in use, or can be deallocated or returned +to the allocator's pool. This simply means that each buffer has a counter keeping track of the number of references to +this buffer, and end user is responsible for properly incrementing/decrementing the counter according the buffer is used. + +In Arrow, each ArrowBuf has an associated ReferenceManager that tracks the reference count, which can be retrieved +with ArrowBuf.getReferenceManager(). The reference count can be updated with ``ReferenceManager.release`` and +``ReferenceManager.retain``. + +Of course, this is tedious and error-prone, so usually, instead of directly working with buffers, we should use +higher-level APIs like ValueVector. Such classes generally implement Closeable/AutoCloseable and will automatically +decrement the reference count when closed method. + +.. code-block:: + + |__ A = Allocator + |____ B = IntVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + |____ C = VarcharVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + +Allocators implement AutoCloseable as well. In this case, closing the allocator will check that all buffers +obtained from the allocator are closed. If not, ``close()`` method will raise an exception; this helps track +memory leaks from unclosed buffers. + +As you see reference counting needs to be handled properly by us, if at some point you need to ensuring that an +independent section of code has `fully cleaned up all allocated buffers while still maintaining a global memory limit +through the RootAllocator`, well ``BufferAllocator.newChildAllocator`` is what you should use. + +Reason To Use Direct Memory +=========================== + +* When `writing an ArrowBuf`_ we use the direct buffer (``nioBuffer()`` returns a DirectByteBuffer) and the JVM `will attempt to avoid copying the buffer's content to (or from) an intermediate buffer`_ so it makes I/O (and hence IPC) faster. Review comment: We don't need to link into random bits of code in the user-facing docs. "The JVM can optimize I/O operations when using direct memory/direct buffers; it will attempt to avoid copying buffer contents to/from an intermediate buffer. This can speed up IPC in Arrow." ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. Review comment: Hmm, I feel like we can still introduce this better. "The concrete implementation of the BufferAllocator interface is RootAllocator. (Link to the Javadocs.) Applications should generally create one RootAllocator at the start of the program, and use it through the BufferAllocator interface. Allocators implement AutoCloseable and must be closed after the application is done with them; this will check that all outstanding memory has been freed (see the next section). Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then more allocators are created as children of an existing allocator via newChildAllocator. (Link to Javadocs.) When creating a RootAllocator or a child allocator, a memory limit is provided, and when allocating memory, the limit is checked. Furthermore, when allocating memory from a child allocator, those allocations are also reflected in all parent allocators. Hence, the RootAllocator effectively sets the program-wide memory limit, and serves as the master bookkeeper for all memory allocations. Child allocators are not strictly required, but can help better organize code. For instance, a lower memory limit can be set for a particular section of code. When the allocator is closed, it then checks that that section didn't leak any memory. And child allocators can be named, which makes it easier to tell where an ArrowBuf came from during debugging." ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). + +Unlike (Direct)ByteBuffer, it has reference counting built in (`see the next section`). + +Reference counting +================== + +Direct memory involve more activities than allocate and deallocate because allocators (thru pool/cache) +allocate buffers (ArrowBuf). Review comment: Direct memory is more expensive to allocate and deallocate. That's why allocators pool or cache direct buffers. The caching is not the _reason_ why it's expensive, it's the _consequence_. ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). + +Unlike (Direct)ByteBuffer, it has reference counting built in (`see the next section`). + +Reference counting +================== + +Direct memory involve more activities than allocate and deallocate because allocators (thru pool/cache) +allocate buffers (ArrowBuf). + +Arrow uses manual reference counting to track whether a buffer is in use, or can be deallocated or returned Review comment: Because we want to pool/cache buffers and manage them deterministically, we use manual reference counting instead of the garbage collector. ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). + +Unlike (Direct)ByteBuffer, it has reference counting built in (`see the next section`). + +Reference counting +================== + +Direct memory involve more activities than allocate and deallocate because allocators (thru pool/cache) +allocate buffers (ArrowBuf). + +Arrow uses manual reference counting to track whether a buffer is in use, or can be deallocated or returned +to the allocator's pool. This simply means that each buffer has a counter keeping track of the number of references to +this buffer, and end user is responsible for properly incrementing/decrementing the counter according the buffer is used. Review comment: ```suggestion the buffer, and the user is responsible for properly incrementing/decrementing the counter as the buffer is used. ``` ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). + +Unlike (Direct)ByteBuffer, it has reference counting built in (`see the next section`). + +Reference counting +================== + +Direct memory involve more activities than allocate and deallocate because allocators (thru pool/cache) +allocate buffers (ArrowBuf). + +Arrow uses manual reference counting to track whether a buffer is in use, or can be deallocated or returned +to the allocator's pool. This simply means that each buffer has a counter keeping track of the number of references to +this buffer, and end user is responsible for properly incrementing/decrementing the counter according the buffer is used. + +In Arrow, each ArrowBuf has an associated ReferenceManager that tracks the reference count, which can be retrieved +with ArrowBuf.getReferenceManager(). The reference count can be updated with ``ReferenceManager.release`` and Review comment: Link to Javadocs. ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). + +Unlike (Direct)ByteBuffer, it has reference counting built in (`see the next section`). + +Reference counting +================== + +Direct memory involve more activities than allocate and deallocate because allocators (thru pool/cache) +allocate buffers (ArrowBuf). + +Arrow uses manual reference counting to track whether a buffer is in use, or can be deallocated or returned +to the allocator's pool. This simply means that each buffer has a counter keeping track of the number of references to +this buffer, and end user is responsible for properly incrementing/decrementing the counter according the buffer is used. + +In Arrow, each ArrowBuf has an associated ReferenceManager that tracks the reference count, which can be retrieved +with ArrowBuf.getReferenceManager(). The reference count can be updated with ``ReferenceManager.release`` and +``ReferenceManager.retain``. + +Of course, this is tedious and error-prone, so usually, instead of directly working with buffers, we should use +higher-level APIs like ValueVector. Such classes generally implement Closeable/AutoCloseable and will automatically +decrement the reference count when closed method. + +.. code-block:: + + |__ A = Allocator + |____ B = IntVector (reference count = 2 ) Review comment: This is wrong, no? Buffers have a reference count, not vectors. And ValidityBuffer and ValueBuffer aren't classes. ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). + +Unlike (Direct)ByteBuffer, it has reference counting built in (`see the next section`). + +Reference counting +================== + +Direct memory involve more activities than allocate and deallocate because allocators (thru pool/cache) +allocate buffers (ArrowBuf). + +Arrow uses manual reference counting to track whether a buffer is in use, or can be deallocated or returned +to the allocator's pool. This simply means that each buffer has a counter keeping track of the number of references to +this buffer, and end user is responsible for properly incrementing/decrementing the counter according the buffer is used. + +In Arrow, each ArrowBuf has an associated ReferenceManager that tracks the reference count, which can be retrieved +with ArrowBuf.getReferenceManager(). The reference count can be updated with ``ReferenceManager.release`` and +``ReferenceManager.retain``. + +Of course, this is tedious and error-prone, so usually, instead of directly working with buffers, we should use +higher-level APIs like ValueVector. Such classes generally implement Closeable/AutoCloseable and will automatically +decrement the reference count when closed method. + +.. code-block:: + + |__ A = Allocator + |____ B = IntVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + |____ C = VarcharVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + +Allocators implement AutoCloseable as well. In this case, closing the allocator will check that all buffers +obtained from the allocator are closed. If not, ``close()`` method will raise an exception; this helps track +memory leaks from unclosed buffers. + +As you see reference counting needs to be handled properly by us, if at some point you need to ensuring that an +independent section of code has `fully cleaned up all allocated buffers while still maintaining a global memory limit +through the RootAllocator`, well ``BufferAllocator.newChildAllocator`` is what you should use. Review comment: ```suggestion As you see, reference counting needs to be handled carefully. To ensure that an independent section of code has fully cleaned up all allocated buffers, use a new child allocator. ``` ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). + +Unlike (Direct)ByteBuffer, it has reference counting built in (`see the next section`). + +Reference counting +================== + +Direct memory involve more activities than allocate and deallocate because allocators (thru pool/cache) +allocate buffers (ArrowBuf). + +Arrow uses manual reference counting to track whether a buffer is in use, or can be deallocated or returned +to the allocator's pool. This simply means that each buffer has a counter keeping track of the number of references to +this buffer, and end user is responsible for properly incrementing/decrementing the counter according the buffer is used. + +In Arrow, each ArrowBuf has an associated ReferenceManager that tracks the reference count, which can be retrieved +with ArrowBuf.getReferenceManager(). The reference count can be updated with ``ReferenceManager.release`` and +``ReferenceManager.retain``. + +Of course, this is tedious and error-prone, so usually, instead of directly working with buffers, we should use +higher-level APIs like ValueVector. Such classes generally implement Closeable/AutoCloseable and will automatically +decrement the reference count when closed method. + +.. code-block:: + + |__ A = Allocator + |____ B = IntVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + |____ C = VarcharVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + +Allocators implement AutoCloseable as well. In this case, closing the allocator will check that all buffers +obtained from the allocator are closed. If not, ``close()`` method will raise an exception; this helps track +memory leaks from unclosed buffers. + +As you see reference counting needs to be handled properly by us, if at some point you need to ensuring that an +independent section of code has `fully cleaned up all allocated buffers while still maintaining a global memory limit +through the RootAllocator`, well ``BufferAllocator.newChildAllocator`` is what you should use. + +Reason To Use Direct Memory +=========================== + +* When `writing an ArrowBuf`_ we use the direct buffer (``nioBuffer()`` returns a DirectByteBuffer) and the JVM `will attempt to avoid copying the buffer's content to (or from) an intermediate buffer`_ so it makes I/O (and hence IPC) faster. +* We can `directly wrap a native memory address`_ instead of having to copy data for JNI (where in implementing the C Data Interface we can directly create `Java ArrowBufs that directly correspond to the C pointers`_). +* Conversely in JNI, we can directly use `Java ArrowBufs in C++`_ without having to copy data. + +So basically #1 is more efficient I/O, and #2/#3 is better integration with JNI code. + +Development Guidelines +====================== + +* Use the BufferAllocator interface in APIs instead of RootAllocator. +* Applications should generally create one RootAllocator at the start of the program. +* Remember to close() allocators after use (whether they are child allocators or the RootAllocator), either manually or preferably via a try-with-resources statement. + +Debugging Memory Leaks/Allocation +================================= + +Allocators have a debug mode, that makes it easier to figure out where a leak is originated (Consider to add this +parameter to your application: ``-Darrow.memory.debug.allocator=true``). This parameter enable to create an historical log +about the memory allocation. Review comment: Have you tried this? Can you show exactly 1) how to access this log and 2) what it looks like? ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,114 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `ArrowBuf`_ +* `Reference counting`_ Review comment: Or actually, if you want: we can remove this list and put `.. contents::` here instead. ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, +and provides low-level interfaces for working with the contents, similar to ByteBuffer. + +The objects created using ``Direct Memory`` take advantage of native executions and it is decided natively by the JVM. Arrow +offer efficient memory operations base on this Direct Memory implementation (`see section below for detailed reasons of use`). + +Unlike (Direct)ByteBuffer, it has reference counting built in (`see the next section`). + +Reference counting +================== + +Direct memory involve more activities than allocate and deallocate because allocators (thru pool/cache) +allocate buffers (ArrowBuf). + +Arrow uses manual reference counting to track whether a buffer is in use, or can be deallocated or returned +to the allocator's pool. This simply means that each buffer has a counter keeping track of the number of references to +this buffer, and end user is responsible for properly incrementing/decrementing the counter according the buffer is used. + +In Arrow, each ArrowBuf has an associated ReferenceManager that tracks the reference count, which can be retrieved +with ArrowBuf.getReferenceManager(). The reference count can be updated with ``ReferenceManager.release`` and +``ReferenceManager.retain``. + +Of course, this is tedious and error-prone, so usually, instead of directly working with buffers, we should use +higher-level APIs like ValueVector. Such classes generally implement Closeable/AutoCloseable and will automatically +decrement the reference count when closed method. + +.. code-block:: + + |__ A = Allocator + |____ B = IntVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + |____ C = VarcharVector (reference count = 2 ) + |____________ ValidityBuffer + |____________ ValueBuffer + +Allocators implement AutoCloseable as well. In this case, closing the allocator will check that all buffers +obtained from the allocator are closed. If not, ``close()`` method will raise an exception; this helps track +memory leaks from unclosed buffers. + +As you see reference counting needs to be handled properly by us, if at some point you need to ensuring that an +independent section of code has `fully cleaned up all allocated buffers while still maintaining a global memory limit +through the RootAllocator`, well ``BufferAllocator.newChildAllocator`` is what you should use. + +Reason To Use Direct Memory +=========================== + +* When `writing an ArrowBuf`_ we use the direct buffer (``nioBuffer()`` returns a DirectByteBuffer) and the JVM `will attempt to avoid copying the buffer's content to (or from) an intermediate buffer`_ so it makes I/O (and hence IPC) faster. +* We can `directly wrap a native memory address`_ instead of having to copy data for JNI (where in implementing the C Data Interface we can directly create `Java ArrowBufs that directly correspond to the C pointers`_). +* Conversely in JNI, we can directly use `Java ArrowBufs in C++`_ without having to copy data. + +So basically #1 is more efficient I/O, and #2/#3 is better integration with JNI code. + +Development Guidelines +====================== + +* Use the BufferAllocator interface in APIs instead of RootAllocator. +* Applications should generally create one RootAllocator at the start of the program. +* Remember to close() allocators after use (whether they are child allocators or the RootAllocator), either manually or preferably via a try-with-resources statement. + +Debugging Memory Leaks/Allocation +================================= + +Allocators have a debug mode, that makes it easier to figure out where a leak is originated (Consider to add this +parameter to your application: ``-Darrow.memory.debug.allocator=true``). This parameter enable to create an historical log +about the memory allocation. + +Arrow modules use logback to collect logs, configure it properly to see your logs (create ``logback-test.xml`` file on +resources folder and your project could read that by conventions). + +This is an example of historical log enabled: Review comment: Also we should provide the corresponding code snippet. ########## File path: docs/source/java/memory.rst ########## @@ -0,0 +1,174 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you under the Apache License, Version 2.0 (the +.. "License"); you may not use this file except in compliance +.. with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, +.. software distributed under the License is distributed on an +.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +.. KIND, either express or implied. See the License for the +.. specific language governing permissions and limitations +.. under the License. + +================= +Memory Management +================= + +.. contents:: + +The memory modules contain all the functionality that Arrow uses to manage memory (allocation and deallocation). +This section will introduce you to the major concepts in Java’s memory management: + +* `BufferAllocator`_ +* `ArrowBuf`_ +* `Reference counting`_ + +Getting Started +=============== + +Arrow's memory management is built around the needs of the columnar format and using off-heap memory. +Also, it is its own independent implementation, and does not wrap the C++ implementation. + +Arrow offers a high level of abstraction providing several access APIs to read/write data into a direct memory. + +Arrow provides multiple modules: the core interfaces, and implementations of the interfaces. +Users need the core interfaces, and exactly one of the implementations. + +* ``Memory Core``: Provides the interfaces used by the Arrow libraries and applications. +* ``Memory Netty``: An implementation of the memory interfaces based on the `Netty`_ library. +* ``Memory Unsafe``: An implementation of the memory interfaces based on the `sun.misc.Unsafe`_ library. + +BufferAllocator +=============== + +The BufferAllocator interface deals with allocating ArrowBufs for the application. + +The concrete implementation of the allocator is RootAllocator. Applications should generally create one RootAllocator at the +start of the program, and use it through the BufferAllocator interface. Allocators have a memory limit. The RootAllocator +sets the program-wide memory limit. The RootAllocator is responsible for being the master bookkeeper for memory allocations. + +Arrow provides a tree-based model for memory allocation. The RootAllocator is created first, then all allocators +are created as children ``BufferAllocator.newChildAllocator`` of that allocator. + +One of the uses of child allocators is to set a lower temporary limit for one section of the code. Also, child +allocators can be named; this makes it easier to tell where an ArrowBuf came from during debugging. + +ArrowBuf +======== + +ArrowBuf represents a single, contiguous allocation of `Direct Memory`_. It consists of an address and a length, Review comment: I think upon digging around that multiple ArrowBufs can point to the same allocation (that is why the reference counting is handled by a separate ReferenceManager) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
