Class UnmanagedMemory

java.lang.Object
org.graalvm.nativeimage.UnmanagedMemory

public final class UnmanagedMemory extends Object
Contains static methods that allow allocate/free of unmanaged memory, i.e., memory that is not under the control of the garbage collector. In a typical C environment, these are the malloc/free functions of the standard C library, however this class makes no assumptions or guarantees about how the memory is managed. In particular, it is not allowed to free memory returned by these allocation function directly using the standard C library (or vice versa).
Since:
19.0
  • Method Details

    • malloc

      public static <T extends PointerBase> T malloc(UnsignedWord size)
      Allocates size bytes of unmanaged memory. The content of the memory is undefined.

      This method never returns a null pointer, but instead throws an OutOfMemoryError when allocation fails.

      Since:
      19.0
    • malloc

      public static <T extends PointerBase> T malloc(int size)
      Allocates size bytes of unmanaged memory. The content of the memory is undefined.

      This method never returns a null pointer, but instead throws an OutOfMemoryError when allocation fails.

      Since:
      19.0
    • calloc

      public static <T extends PointerBase> T calloc(UnsignedWord size)
      Allocates size bytes of unmanaged memory. The content of the memory is set to 0.

      This method never returns a null pointer, but instead throws an OutOfMemoryError when allocation fails.

      Since:
      19.0
    • calloc

      public static <T extends PointerBase> T calloc(int size)
      Allocates size bytes of unmanaged memory. The content of the memory is set to 0.

      This method never returns a null pointer, but instead throws an OutOfMemoryError when allocation fails.

      Since:
      19.0
    • realloc

      public static <T extends PointerBase> T realloc(T ptr, UnsignedWord size)
      Changes the size of the provided unmanaged memory to size bytes of unmanaged memory. If the new size is larger than the old size, the content of the additional memory is undefined.

      This method never returns a null pointer, but instead throws an OutOfMemoryError when allocation fails. In that case, the old data is not deallocated and remains unchanged.

      Since:
      19.0
    • free

      public static void free(PointerBase ptr)
      Frees unmanaged memory that was previously allocated using methods of this class. This method is a no-op if the given pointer is null.
      Since:
      19.0