davisusanibar commented on code in PR #37178:
URL: https://github.com/apache/arrow/pull/37178#discussion_r1296248401


##########
java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/SingletonAllocator.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.
+ */
+
+package org.apache.arrow.memory.util;
+
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
+
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+import org.apache.arrow.util.VisibleForTesting;
+
+/**
+ * Factory class for managing allocators in simple applications that make
+ * use of an application-wide singleton root allocator with no listeners,
+ * automatic allocator defaults, and no total allocator limit.
+ * <p>
+ * Child allocators are created with a default name with the format
+ * <code>SingletonAllocator#(AtomicCounter)</code>.
+ */
+public final class SingletonAllocator {
+  private SingletonAllocator() {}
+
+  private static final RootAllocator ROOT = new RootAllocator();
+  private static final AtomicInteger COUNTER = new AtomicInteger(0);
+
+  /**
+   * Get a new child allocator with a default name, no initial reservation and 
no maximum allocation.
+   *
+   * @return a new child {@link BufferAllocator}.
+   */
+  public static BufferAllocator getAllocator() {
+    return getAllocator(0, Long.MAX_VALUE);
+  }
+
+  /**
+   * Get a new child allocator with a default name and a custom initial 
reservation and maximum allocation.
+   *
+   * @return a new child {@link BufferAllocator}.
+   */
+  public static BufferAllocator getAllocator(final long initReservation, final 
long maxAllocation) {
+    return ROOT.newChildAllocator(nextChildName(), initReservation, 
maxAllocation);
+  }
+
+  private static String nextChildName() {
+    return "SingletonAllocator#" + COUNTER.incrementAndGet();
+  }
+
+  /**
+   * Get the total allocated memory across all child allocators.
+   *
+   * @return the number of allocated bytes.
+   */
+  public static long getAllocatedMemory() {
+    return ROOT.getAllocatedMemory();
+  }
+
+  /**
+   * Check if there are any outstanding allocators that have not been closed.
+   *
+   * @return <code>true</code> if child allocators exist or <code>false</code> 
if not.
+   */
+  public static boolean hasActiveAllocators() {
+    return !ROOT.getChildAllocators().isEmpty();
+  }
+
+  @VisibleForTesting
+  static void checkGlobalCleanUpResources() {

Review Comment:
   > Probably this should be public? An application should check that all 
memory is freed at shutdown
   
   I thought the same, let me expose that also



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