Java Code Examples for sun.misc.Unsafe#allocateMemory()

The following examples show how to use sun.misc.Unsafe#allocateMemory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnsafeGetAddressTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Class c = UnsafeGetAddressTest.class.getClassLoader().loadClass("sun.misc.Unsafe");
    Field f = c.getDeclaredField("theUnsafe");
    f.setAccessible(true);
    unsafe = (Unsafe)f.get(c);

    long address = unsafe.allocateMemory(unsafe.addressSize());
    unsafe.putAddress(address, 0x0000000080000000L);
    // from sun.misc.Unsafe.getAddress' documentation:
    // "If the native pointer is less than 64 bits wide, it is
    // extended as an unsigned number to a Java long."
    result = unsafe.getAddress(address);
    System.out.printf("1: was 0x%x, expected 0x%x\n", result,
            0x0000000080000000L);
    for (int i = 0; i < 1000000; i++) {
        result = unsafe.getAddress(address);
    }

    // The code has got compiled, check the result now
    System.out.printf("2: was 0x%x, expected 0x%x\n", result,
            0x0000000080000000L);
    if (result != 0x0000000080000000L) {
        System.out.println("Test Failed");
        System.exit(97);
    } else {
        System.out.println("Test Passed");
    }
}
 
Example 2
Source File: UnsafeGetAddressTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Class c = UnsafeGetAddressTest.class.getClassLoader().loadClass("sun.misc.Unsafe");
    Field f = c.getDeclaredField("theUnsafe");
    f.setAccessible(true);
    unsafe = (Unsafe)f.get(c);

    long address = unsafe.allocateMemory(unsafe.addressSize());
    unsafe.putAddress(address, 0x0000000080000000L);
    // from sun.misc.Unsafe.getAddress' documentation:
    // "If the native pointer is less than 64 bits wide, it is
    // extended as an unsigned number to a Java long."
    result = unsafe.getAddress(address);
    System.out.printf("1: was 0x%x, expected 0x%x\n", result,
            0x0000000080000000L);
    for (int i = 0; i < 1000000; i++) {
        result = unsafe.getAddress(address);
    }

    // The code has got compiled, check the result now
    System.out.printf("2: was 0x%x, expected 0x%x\n", result,
            0x0000000080000000L);
    if (result != 0x0000000080000000L) {
        System.out.println("Test Failed");
        System.exit(97);
    } else {
        System.out.println("Test Passed");
    }
}
 
Example 3
Source File: UnsafeUtils.java    From message-queue-java with MIT License 5 votes vote down vote up
public static void main(String[] args) {
    Unsafe unsafe = getUnsafe();
    long address = unsafe.allocateMemory(2);
    short number = 1;
    unsafe.putShort(address, number);
    if (unsafe.getByte(address) == 0)
        System.out.println("Big Endian");
    else
        System.out.println("Little Endian");
    unsafe.freeMemory(address);
}
 
Example 4
Source File: UnsafeGetAddressTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Class c = UnsafeGetAddressTest.class.getClassLoader().loadClass("sun.misc.Unsafe");
    Field f = c.getDeclaredField("theUnsafe");
    f.setAccessible(true);
    unsafe = (Unsafe)f.get(c);

    long address = unsafe.allocateMemory(unsafe.addressSize());
    unsafe.putAddress(address, 0x0000000080000000L);
    // from sun.misc.Unsafe.getAddress' documentation:
    // "If the native pointer is less than 64 bits wide, it is
    // extended as an unsigned number to a Java long."
    result = unsafe.getAddress(address);
    System.out.printf("1: was 0x%x, expected 0x%x\n", result,
            0x0000000080000000L);
    for (int i = 0; i < 1000000; i++) {
        result = unsafe.getAddress(address);
    }

    // The code has got compiled, check the result now
    System.out.printf("2: was 0x%x, expected 0x%x\n", result,
            0x0000000080000000L);
    if (result != 0x0000000080000000L) {
        System.out.println("Test Failed");
        System.exit(97);
    } else {
        System.out.println("Test Passed");
    }
}
 
Example 5
Source File: DirectOutOfMemoryDemo.java    From javacore with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
public static void main(String[] args) throws IllegalAccessException {
    Field unsafeField = Unsafe.class.getDeclaredFields()[0];
    unsafeField.setAccessible(true);
    Unsafe unsafe = (Unsafe) unsafeField.get(null);
    while (true) {
        unsafe.allocateMemory(_1MB);
    }
}
 
Example 6
Source File: UnsafeGetAddressTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Class c = UnsafeGetAddressTest.class.getClassLoader().loadClass("sun.misc.Unsafe");
    Field f = c.getDeclaredField("theUnsafe");
    f.setAccessible(true);
    unsafe = (Unsafe)f.get(c);

    long address = unsafe.allocateMemory(unsafe.addressSize());
    unsafe.putAddress(address, 0x0000000080000000L);
    // from sun.misc.Unsafe.getAddress' documentation:
    // "If the native pointer is less than 64 bits wide, it is
    // extended as an unsigned number to a Java long."
    result = unsafe.getAddress(address);
    System.out.printf("1: was 0x%x, expected 0x%x\n", result,
            0x0000000080000000L);
    for (int i = 0; i < 1000000; i++) {
        result = unsafe.getAddress(address);
    }

    // The code has got compiled, check the result now
    System.out.printf("2: was 0x%x, expected 0x%x\n", result,
            0x0000000080000000L);
    if (result != 0x0000000080000000L) {
        System.out.println("Test Failed");
        System.exit(97);
    } else {
        System.out.println("Test Passed");
    }
}
 
Example 7
Source File: DirectMemoryOOM.java    From java-skills with MIT License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Field unsafeField = Unsafe.class.getDeclaredFields()[0];
    unsafeField.setAccessible(true);
    Unsafe unsafe = (Unsafe) unsafeField.get(null);
    while (true) {
        unsafe.allocateMemory(_1MB);
    }
}
 
Example 8
Source File: UnsafeGetAddressTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Class c = UnsafeGetAddressTest.class.getClassLoader().loadClass("sun.misc.Unsafe");
    Field f = c.getDeclaredField("theUnsafe");
    f.setAccessible(true);
    unsafe = (Unsafe)f.get(c);

    long address = unsafe.allocateMemory(unsafe.addressSize());
    unsafe.putAddress(address, 0x0000000080000000L);
    // from sun.misc.Unsafe.getAddress' documentation:
    // "If the native pointer is less than 64 bits wide, it is
    // extended as an unsigned number to a Java long."
    result = unsafe.getAddress(address);
    System.out.printf("1: was 0x%x, expected 0x%x\n", result,
            0x0000000080000000L);
    for (int i = 0; i < 1000000; i++) {
        result = unsafe.getAddress(address);
    }

    // The code has got compiled, check the result now
    System.out.printf("2: was 0x%x, expected 0x%x\n", result,
            0x0000000080000000L);
    if (result != 0x0000000080000000L) {
        System.out.println("Test Failed");
        System.exit(97);
    } else {
        System.out.println("Test Passed");
    }
}
 
Example 9
Source File: TestOffHeapDataStorage.java    From reladomo with Apache License 2.0 5 votes vote down vote up
public void testUnsafeZeroMemory()
{
    FastUnsafeOffHeapMemoryInitialiser initialiser = new FastUnsafeOffHeapMemoryInitialiser();
    Unsafe unsafe = MithraUnsafe.getUnsafe();

    long limit = 1024;
    long ref = unsafe.allocateMemory(limit);

    try
    {
        for (long size = 1; size < limit; size++)
        {
            // Pre-populate the buffer with non-zero data
            for (long i = 0; i < limit; i++)
            {
                unsafe.putByte(ref + i, (byte) 0xff);
            }

            // Execute code under test
            initialiser.unsafeZeroMemory(ref, size, ref, limit);

            for (long i = 0; i < limit; i++)
            {
                long byteRef = ref + i;
                if (i < size)
                {
                    assertEquals("Buffer data content at location " + i + " (buffer size = " + size + ") should have been zeroed but was NOT", (byte) 0x00, unsafe.getByte(byteRef));
                } else
                {
                    assertEquals("Buffer data content at location " + i + " (buffer size = " + size + ") was modified but should NOT have been", (byte) 0xff, unsafe.getByte(byteRef));
                }
            }
        }
    }
    finally
    {
        unsafe.freeMemory(ref);
    }
}
 
Example 10
Source File: UnsafeWrapperMultiCacheValueTest.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnsafeWrapperMultiCacheValue() throws EvictionException {

  final Unsafe unsafe = UnsafeCacheValue._unsafe;
  final long[] addresses = new long[10];
  int chunkSize = 1000;
  int chunks = 10;
  for (int i = 0; i < chunks; i++) {
    addresses[i] = unsafe.allocateMemory(chunkSize);
  }

  int length = addresses.length * chunkSize;

  UnsafeWrapperMultiCacheValue unsafeWrapperMultiCacheValue = new UnsafeWrapperMultiCacheValue(length, addresses,
      chunkSize) {
    @Override
    protected void releaseInternal() {
      for (int i = 0; i < 10; i++) {
        unsafe.freeMemory(addresses[i]);
      }
    }
  };

  Random random = new Random();
  byte[] buf1 = new byte[length];
  random.nextBytes(buf1);

  unsafeWrapperMultiCacheValue.write(0, buf1, 0, length);

  byte[] buf2 = new byte[length];

  unsafeWrapperMultiCacheValue.read(0, buf2, 0, length);

  assertTrue(equals(buf1, buf2));
  assertTrue(Arrays.equals(buf1, buf2));

  unsafeWrapperMultiCacheValue.release();

}