srowen closed pull request #23424: [SPARK-24421][CORE][FOLLOWUP] Use normal
direct ByteBuffer allocation if Cleaner can't be set
URL: https://github.com/apache/spark/pull/23424
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/common/unsafe/src/main/java/org/apache/spark/unsafe/Platform.java
b/common/unsafe/src/main/java/org/apache/spark/unsafe/Platform.java
index 076b693f81c88..1adf7abfc8a68 100644
--- a/common/unsafe/src/main/java/org/apache/spark/unsafe/Platform.java
+++ b/common/unsafe/src/main/java/org/apache/spark/unsafe/Platform.java
@@ -209,22 +209,33 @@ public static long reallocateMemory(long address, long
oldSize, long newSize) {
}
/**
- * Uses internal JDK APIs to allocate a DirectByteBuffer while ignoring the
JVM's
- * MaxDirectMemorySize limit (the default limit is too low and we do not
want to require users
- * to increase it).
+ * Allocate a DirectByteBuffer, potentially bypassing the JVM's
MaxDirectMemorySize limit.
*/
public static ByteBuffer allocateDirectBuffer(int size) {
try {
- long memory = allocateMemory(size);
- ByteBuffer buffer = (ByteBuffer) DBB_CONSTRUCTOR.newInstance(memory,
size);
- if (CLEANER_CREATE_METHOD != null) {
+ if (CLEANER_CREATE_METHOD == null) {
+ // Can't set a Cleaner (see comments on field), so need to allocate
via normal Java APIs
try {
- DBB_CLEANER_FIELD.set(buffer,
- CLEANER_CREATE_METHOD.invoke(null, buffer, (Runnable) () ->
freeMemory(memory)));
- } catch (IllegalAccessException | InvocationTargetException e) {
- throw new IllegalStateException(e);
+ return ByteBuffer.allocateDirect(size);
+ } catch (OutOfMemoryError oome) {
+ // checkstyle.off: RegexpSinglelineJava
+ throw new OutOfMemoryError("Failed to allocate direct buffer (" +
oome.getMessage() +
+ "); try increasing -XX:MaxDirectMemorySize=... to, for example,
your heap size");
+ // checkstyle.on: RegexpSinglelineJava
}
}
+ // Otherwise, use internal JDK APIs to allocate a DirectByteBuffer while
ignoring the JVM's
+ // MaxDirectMemorySize limit (the default limit is too low and we do not
want to
+ // require users to increase it).
+ long memory = allocateMemory(size);
+ ByteBuffer buffer = (ByteBuffer) DBB_CONSTRUCTOR.newInstance(memory,
size);
+ try {
+ DBB_CLEANER_FIELD.set(buffer,
+ CLEANER_CREATE_METHOD.invoke(null, buffer, (Runnable) () ->
freeMemory(memory)));
+ } catch (IllegalAccessException | InvocationTargetException e) {
+ freeMemory(memory);
+ throw new IllegalStateException(e);
+ }
return buffer;
} catch (Exception e) {
throwException(e);
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]