3

Does Java sun Unsafe.allocateMemory has any limitation? I know that it's off heap so it's not subject to GC. But can we limit allocation size by JVM command like :

-XX:MaxDirectMemorySize

1 Answer 1

4

The Unsafe#allocateMemory method is native, and the native implementation of Unsafe#allocateMemory directly calls os::malloc, delegating the work for the allocation to the underlying system. There is some bookkeeping and some debugging options, but it seems that there is no built-in mechanism for limiting the allocation size.

Depending on the application case, you might consider wrapping the Unsafe#allocateMemory call into an own method, that does a check for a size limit based on a command line parameter, as you suggested.

If your intention was to prevent a third party library (that uses Unsafe#allocateMemory internally) from allocating too much memory, I think there will be no reasonable way to achieve this.

Sign up to request clarification or add additional context in comments.

1 Comment

Is there any official doc about the mechanism how the allocation size is controlled for Usnafe#allocateMemory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.