lidavidm commented on code in PR #37498:
URL: https://github.com/apache/arrow/pull/37498#discussion_r1321595468


##########
docs/source/java/memory.rst:
##########
@@ -288,6 +288,54 @@ Finally, enabling the ``TRACE`` logging level will 
automatically provide this st
    |        at RootAllocator.close (RootAllocator.java:29)
    |        at (#8:1)
 
+Sometimes, explicitly passing allocators around is difficult. For example, it
+can be hard to pass around extra state, like an allocator, through layers of 
+existing application or framework code. A global or singleton allocator 
instance
+can be useful here, though it should not be your first choice.
+
+How this works:
+
+1. Set up a global allocator in a singleton class.
+2. Provide methods to create child allocators from the global allocator.
+3. Give child allocators proper names to make it easier to figure out where
+   allocations occurred in case of errors.
+4. Ensure that resources are properly closed.
+5. Check that the global allocator is empty at some suitable point, such as
+   right before program shutdown.
+6. If it is not empty, review the above allocation bugs.
+
+.. code-block:: java
+
+    //1
+    private static final BufferAllocator allocator = new RootAllocator();
+    private static final AtomicInteger childNumber = new AtomicInteger(0);
+    ...
+    //2
+    public static BufferAllocator getChildAllocator() {
+        return allocator.newChildAllocator(nextChildName(), 0, Long.MAX_VALUE);
+    }
+    ...
+    //3
+    private static String nextChildName() {
+        return "Allocator-Child-" + childNumber.incrementAndGet();
+    }
+    ...
+    //4: Business code
+    try (BufferAllocator allocator = GlobalAllocator.getChildAllocator()) {
+        ...
+    }
+    ...
+    //5
+    public static void checkGlobalCleanUpResources() {
+        ...
+        if (!allocator.getChildAllocators().isEmpty()) {
+          throw new IllegalStateException(...)
+          ));
+        } else if (allocator.getAllocatedMemory() != 0) {
+          throw new IllegalStateException(...);
+        }
+    }

Review Comment:
   ```suggestion
       public static void checkGlobalCleanUpResources() {
           ...
           if (!allocator.getChildAllocators().isEmpty()) {
             throw new IllegalStateException(...);
           } else if (allocator.getAllocatedMemory() != 0) {
             throw new IllegalStateException(...);
           }
       }
   ```



-- 
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]

Reply via email to