本文整理了Java中sun.misc.Unsafe.allocateMemory()
方法的一些代码示例,展示了Unsafe.allocateMemory()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Unsafe.allocateMemory()
方法的具体详情如下:
包路径:sun.misc.Unsafe
类名称:Unsafe
方法名:allocateMemory
[英]Allocates a new block of native memory, of the given size in bytes. The contents of the memory are uninitialized; they will generally be garbage. The resulting native pointer will never be zero, and will be aligned for all value types. Dispose of this memory by calling #freeMemory, or resize it with #reallocateMemory.
[中]分配一个新的本机内存块,大小以字节为单位。内存的内容未初始化;它们通常是垃圾。生成的本机指针永远不会为零,并且将针对所有值类型对齐。通过调用#freeMemory来处理此内存,或使用#reallocateMemory调整其大小。
代码示例来源:origin: netty/netty
static long allocateMemory(long size) {
return UNSAFE.allocateMemory(size);
}
代码示例来源:origin: redisson/redisson
static long allocateMemory(long size) {
return UNSAFE.allocateMemory(size);
}
代码示例来源:origin: apache/ignite
/**
* Allocates memory.
*
* @param size Size.
* @return address.
*/
public static long allocateMemory(long size) {
return UNSAFE.allocateMemory(size);
}
代码示例来源:origin: wildfly/wildfly
static long allocateMemory(long size) {
return UNSAFE.allocateMemory(size);
}
代码示例来源:origin: apache/geode
public long allocateMemory(long size) {
return this.unsafe.allocateMemory(size);
}
代码示例来源:origin: netty/netty
static ByteBuffer allocateDirectNoCleaner(int capacity) {
// Calling malloc with capacity of 0 may return a null ptr or a memory address that can be used.
// Just use 1 to make it safe to use in all cases:
// See: http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html
return newDirectBuffer(UNSAFE.allocateMemory(Math.max(1, capacity)), capacity);
}
代码示例来源:origin: redisson/redisson
static ByteBuffer allocateDirectNoCleaner(int capacity) {
// Calling malloc with capacity of 0 may return a null ptr or a memory address that can be used.
// Just use 1 to make it safe to use in all cases:
// See: http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html
return newDirectBuffer(UNSAFE.allocateMemory(Math.max(1, capacity)), capacity);
}
代码示例来源:origin: redisson/redisson
@Override
public BasicBytez newInstance(long size) {
return new MallocBytez(unsafe.allocateMemory(size),size);
}
代码示例来源:origin: redisson/redisson
@Override
public Bytez alloc(long len) {
MallocBytez mallocBytez = new MallocBytez(MallocBytez.unsafe.allocateMemory(len), len);
mallocBytez.clear();
allocated.add(mallocBytez);
alloced.getAndAdd(len);
return mallocBytez;
}
代码示例来源:origin: wildfly/wildfly
static ByteBuffer allocateDirectNoCleaner(int capacity) {
// Calling malloc with capacity of 0 may return a null ptr or a memory address that can be used.
// Just use 1 to make it safe to use in all cases:
// See: http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html
return newDirectBuffer(UNSAFE.allocateMemory(Math.max(1, capacity)), capacity);
}
代码示例来源:origin: RuedigerMoeller/fast-serialization
@Override
public BasicBytez newInstance(long size) {
return new MallocBytez(unsafe.allocateMemory(size),size);
}
代码示例来源:origin: stackoverflow.com
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import sun.misc.Unsafe;
public class TestUnsafe {
public static void main(String[] args) throws Exception{
Class unsafeClass = Class.forName("sun.misc.Unsafe");
Field f = unsafeClass.getDeclaredField("theUnsafe");
f.setAccessible(true);
Unsafe unsafe = (Unsafe) f.get(null);
System.out.print("4..3..2..1...");
try
{
for(;;)
unsafe.allocateMemory(1024*1024);
} catch(Error e) {
System.out.println("Boom :)");
e.printStackTrace();
}
}
}
代码示例来源:origin: RuedigerMoeller/fast-serialization
@Override
public Bytez alloc(long len) {
MallocBytez mallocBytez = new MallocBytez(MallocBytez.unsafe.allocateMemory(len), len);
mallocBytez.clear();
allocated.add(mallocBytez);
alloced.getAndAdd(len);
return mallocBytez;
}
代码示例来源:origin: neo4j/neo4j
/**
* Allocate a block of memory of the given size in bytes, and return a pointer to that memory.
* <p>
* The memory is aligned such that it can be used for any data type.
* The memory is uninitialised, so it may contain random garbage, or it may not.
*
* @return a pointer to the allocated memory
*/
public static long allocateMemory( long bytes ) throws NativeMemoryAllocationRefusedError
{
final long pointer;
try
{
pointer = unsafe.allocateMemory( bytes );
}
catch ( Throwable e )
{
throw new NativeMemoryAllocationRefusedError( bytes, GlobalMemoryTracker.INSTANCE.usedDirectMemory(), e );
}
if ( DIRTY_MEMORY )
{
setMemory( pointer, bytes, (byte) 0xA5 );
}
addAllocatedPointer( pointer, bytes );
GlobalMemoryTracker.INSTANCE.allocated( bytes );
return pointer;
}
代码示例来源:origin: graphhopper/graphhopper
@Test
public void testNativeOrder() {
BitUtil bitUtil = BitUtil.get(ByteOrder.nativeOrder());
long address = UnsafeDataAccess.UNSAFE.allocateMemory(8);
long val = 123123123123L * 123L;
byte[] bytes = new byte[8];
bitUtil.fromLong(bytes, val);
if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
for (int i = 7; i >= 0; i--) {
UnsafeDataAccess.UNSAFE.putByte(address + i, bytes[i]);
}
} else {
// not tested:
for (int i = 0; i < 8; i++) {
UnsafeDataAccess.UNSAFE.putByte(address + i, bytes[i]);
}
}
long tmp = UnsafeDataAccess.UNSAFE.getLong(address);
assertEquals(val, tmp);
UnsafeDataAccess.UNSAFE.freeMemory(address);
}
}
代码示例来源:origin: io.netty/netty-common
static long allocateMemory(long size) {
return UNSAFE.allocateMemory(size);
}
代码示例来源:origin: io.netty/netty-common
static ByteBuffer allocateDirectNoCleaner(int capacity) {
// Calling malloc with capacity of 0 may return a null ptr or a memory address that can be used.
// Just use 1 to make it safe to use in all cases:
// See: http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html
return newDirectBuffer(UNSAFE.allocateMemory(Math.max(1, capacity)), capacity);
}
代码示例来源:origin: stackoverflow.com
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class Main {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, InstantiationException {
/* too lazy to run with VM args, use Reflection */
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
/* get array address */
Unsafe unsafe = (Unsafe)f.get(null);
byte four_bytes[] = {25, 25, 25, 25};
Object trash[] = new Object[] { four_bytes };
long base_offset_bytes = unsafe.arrayBaseOffset(Object[].class);
long four_bytes_address = unsafe.getLong(trash, base_offset_bytes); // <- this is it
long ints_addr = unsafe.allocateMemory(16); // allocate 4 * 4 bytes, i.e. 4 ints
unsafe.copyMemory(four_bytes_address + base_offset_bytes, ints_addr, 4); // copy all four bytes
for(int i = 0; i < 4; i++) {
System.out.println(unsafe.getInt(ints_addr + i)); //run through entire allocated int[],
// get some intestines
}
System.out.println("*****************************");
for(int i = 0; i < 16; i++) {
System.out.println(unsafe.getByte(ints_addr + i)); //run through entire allocated int[],
// get some intestines
}
}
}
代码示例来源:origin: facebook/jcommon
long ptr = unsafe.allocateMemory(size);
long writePtr = ptr;
代码示例来源:origin: apache/activemq-artemis
static ByteBuffer allocateDirectNoCleaner(int capacity) {
// Calling malloc with capacity of 0 may return a null ptr or a memory address that can be used.
// Just use 1 to make it safe to use in all cases:
// See: http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html
return newDirectBuffer(UNSAFE.allocateMemory(Math.max(1, capacity)), capacity);
}
内容来源于网络,如有侵权,请联系作者删除!