It's malloc, except when it's not.
For instance, allocating memory by calling malloc via JNI, and then freeing
it with Unsafe.freeMemory, will crash your process if your code is running
on OpenJ9.

On Mon, 18 Mar 2019 at 01:41, Gil Tene <[email protected]> wrote:

> Read the source, Luke:
>
>
> http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/111b95ad4584/src/share/vm/prims/unsafe.cpp#l606
>
> UNSAFE_ENTRY(jlong, Unsafe_AllocateMemory(JNIEnv *env, jobject unsafe, jlong 
> size))
>  
> <http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/111b95ad4584/src/share/vm/prims/unsafe.cpp#l606>
>  UnsafeWrapper("Unsafe_AllocateMemory");
>  
> <http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/111b95ad4584/src/share/vm/prims/unsafe.cpp#l607>
>  size_t sz = (size_t)size;
>  
> <http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/111b95ad4584/src/share/vm/prims/unsafe.cpp#l608>
>  if (sz != (julong)size || size < 0) {
>  
> <http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/111b95ad4584/src/share/vm/prims/unsafe.cpp#l609>
>    THROW_0(vmSymbols::java_lang_IllegalArgumentException());
>  
> <http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/111b95ad4584/src/share/vm/prims/unsafe.cpp#l610>
>  }
>  
> <http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/111b95ad4584/src/share/vm/prims/unsafe.cpp#l611>
>  if (sz == 0) {
>  
> <http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/111b95ad4584/src/share/vm/prims/unsafe.cpp#l612>
>    return 0;
>  
> <http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/111b95ad4584/src/share/vm/prims/unsafe.cpp#l613>
>  }
>  
> <http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/111b95ad4584/src/share/vm/prims/unsafe.cpp#l614>
>  sz = round_to(sz, HeapWordSize);
>  
> <http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/111b95ad4584/src/share/vm/prims/unsafe.cpp#l615>
>  void* x = os::malloc(sz, mtInternal);
>  
> <http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/111b95ad4584/src/share/vm/prims/unsafe.cpp#l616>
>  if (x == NULL) {
>  
> <http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/111b95ad4584/src/share/vm/prims/unsafe.cpp#l617>
>    THROW_0(vmSymbols::java_lang_OutOfMemoryError());
>  
> <http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/111b95ad4584/src/share/vm/prims/unsafe.cpp#l618>
>  } 
> <http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/111b95ad4584/src/share/vm/prims/unsafe.cpp#l619>
>  //Copy::fill_to_words((HeapWord*)x, sz / HeapWordSize);
>  
> <http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/111b95ad4584/src/share/vm/prims/unsafe.cpp#l620>
>  return addr_to_java(x);
> UNSAFE_END
>
> So it is a malloc, which (at least on Linux and unix variants) will
> eventually call mmap  [not on a per-malloc basis, malloc manages blocks and
> free lists and such, but mmap is where the actual memory comes from]
>
> -- Gil
>
> On Saturday, March 16, 2019 at 12:53:03 AM UTC-7, joel.wang wrote:
>>
>> What operating system function would be called when I invoke
>> Unsafe.allocateMemory(size) in java ?
>> and a further thing I wanna understand is, when invoking
>> Unsafe.allocateMemory(size), where the memory will be allocated from the
>> operating system point of view? I mean, whether it be allocated in user
>> space or kernel space ?
>>
> --
> You received this message because you are subscribed to the Google Groups
> "mechanical-sympathy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to