本文整理匯總了Java中sun.misc.Cleaner.create方法的典型用法代碼示例。如果您正苦於以下問題:Java Cleaner.create方法的具體用法?Java Cleaner.create怎麽用?Java Cleaner.create使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sun.misc.Cleaner
的用法示例。
在下文中一共展示了Cleaner.create方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: allocateDirectBuffer
import sun.misc.Cleaner; //導入方法依賴的package包/類
/**
* Uses internal JDK APIs to allocate a DirectByteBuffer while ignoring the JVM's
* MaxDirectMemorySize limit (the default limit is too low and we do not want to require users
* to increase it).
*/
@SuppressWarnings("unchecked")
public static ByteBuffer allocateDirectBuffer(int size) {
try {
Class cls = Class.forName("java.nio.DirectByteBuffer");
Constructor constructor = cls.getDeclaredConstructor(Long.TYPE, Integer.TYPE);
constructor.setAccessible(true);
Field cleanerField = cls.getDeclaredField("cleaner");
cleanerField.setAccessible(true);
final long memory = allocateMemory(size);
ByteBuffer buffer = (ByteBuffer) constructor.newInstance(memory, size);
Cleaner cleaner = Cleaner.create(buffer, new Runnable() {
@Override
public void run() {
freeMemory(memory);
}
});
cleanerField.set(buffer, cleaner);
return buffer;
} catch (Exception e) {
throwException(e);
}
throw new IllegalStateException("unreachable");
}
示例2: ElasticHashinator
import sun.misc.Cleaner; //導入方法依賴的package包/類
/**
* The serialization format is big-endian and the first value is the number of tokens
* Construct the hashinator from a binary description of the ring.
* followed by the token values where each token value consists of the 8-byte position on the ring
* and and the 4-byte partition id. All values are signed.
* @param configBytes config data
* @param cooked compressible wire serialization format if true
*/
public ElasticHashinator(byte configBytes[], boolean cooked) {
Pair<Long, Integer> p = (cooked ? updateCooked(configBytes)
: updateRaw(configBytes));
m_tokens = p.getFirst();
m_tokenCount = p.getSecond();
m_cleaner = Cleaner.create(this, new Deallocator(m_tokens, m_tokenCount * 8));
m_configBytes = !cooked ? Suppliers.ofInstance(configBytes) : m_configBytesSupplier;
m_cookedBytes = cooked ? Suppliers.ofInstance(configBytes) : m_cookedBytesSupplier;
m_tokensMap = Suppliers.memoize(new Supplier<ImmutableSortedMap<Integer, Integer>>() {
@Override
public ImmutableSortedMap<Integer, Integer> get() {
ImmutableSortedMap.Builder<Integer, Integer> builder = ImmutableSortedMap.naturalOrder();
for (int ii = 0; ii < m_tokenCount; ii++) {
final long ptr = m_tokens + (ii * 8);
final int token = Bits.unsafe.getInt(ptr);
final int partition = Bits.unsafe.getInt(ptr + 4);
builder.put(token, partition);
}
return builder.build();
}
});
}
示例3: DirectByteBuffer
import sun.misc.Cleaner; //導入方法依賴的package包/類
DirectByteBuffer(int cap) { // package-private
super(-1, 0, cap, cap, false);
Bits.reserveMemory(cap);
int ps = Bits.pageSize();
long base = 0;
try {
base = unsafe.allocateMemory(cap + ps);
} catch (OutOfMemoryError x) {
Bits.unreserveMemory(cap);
throw x;
}
unsafe.setMemory(base, cap + ps, (byte) 0);
if (base % ps != 0) {
// Round up to page boundary
address = base + ps - (base & (ps - 1));
} else {
address = base;
}
cleaner = Cleaner.create(this, new Deallocator(base, cap));
}
示例4: allocateManualReleaseBuffer
import sun.misc.Cleaner; //導入方法依賴的package包/類
/**
* 分配可能手工進行釋放的 ByteBuffer
* @param capacity 容量
* @return ByteBuffer 對象
*/
protected static ByteBuffer allocateManualReleaseBuffer(int capacity){
try {
long address = (TUnsafe.getUnsafe().allocateMemory(capacity));
Deallocator deallocator = new Deallocator(address);
ByteBuffer byteBuffer = (ByteBuffer) DIRECT_BYTE_BUFFER_CONSTURCTOR.newInstance(address, capacity, deallocator);
Cleaner cleaner = Cleaner.create(byteBuffer, deallocator);
cleanerField.set(byteBuffer, cleaner);
return byteBuffer;
} catch (Exception e) {
Logger.error("Create ByteBufferChannel error. ", e);
return null;
}
}
示例5: init
import sun.misc.Cleaner; //導入方法依賴的package包/類
protected void init(String file, long length, boolean clearFile) throws Exception {
File f = new File(file);
if ( f.exists() && clearFile ) {
f.delete();
}
this.file = f;
if ( f.exists() ) {
length = f.length();
}
RandomAccessFile raf = new RandomAccessFile(f, "rw");
raf.setLength(length); // FIXME: see stackoverflow. does not work always
FileChannel fileChannel = raf.getChannel();
this.fileChannel = raf.getChannel();
this.baseAdress = map0(fileChannel, imodeFor(FileChannel.MapMode.READ_WRITE), 0L, length);
this.length = length;
this.cleaner = Cleaner.create(this, new Unmapper(baseAdress, length, fileChannel));
}
示例6: FloatLargeArray
import sun.misc.Cleaner; //導入方法依賴的package包/類
/**
* Creates new instance of this class.
*
* @param length number of elements
* @param zeroNativeMemory if true, then the native memory is zeroed.
*/
public FloatLargeArray(long length, boolean zeroNativeMemory)
{
this.type = LargeArrayType.FLOAT;
this.sizeof = 4;
if (length <= 0) {
throw new IllegalArgumentException(length + " is not a positive long value");
}
this.length = length;
if (length > LARGEST_32BIT_INDEX) {
System.gc();
this.ptr = Utilities.UNSAFE.allocateMemory(this.length * this.sizeof);
if (zeroNativeMemory) {
zeroNativeMemory(length);
}
Cleaner.create(this, new Deallocator(this.ptr, this.length, this.sizeof));
MemoryCounter.increaseCounter(this.length * this.sizeof);
} else {
data = new float[(int) length];
}
}
示例7: LongLargeArray
import sun.misc.Cleaner; //導入方法依賴的package包/類
/**
* Creates new instance of this class.
*
* @param length number of elements
* @param zeroNativeMemory if true, then the native memory is zeroed.
*/
public LongLargeArray(long length, boolean zeroNativeMemory)
{
this.type = LargeArrayType.LONG;
this.sizeof = 8;
if (length <= 0) {
throw new IllegalArgumentException(length + " is not a positive long value");
}
this.length = length;
if (length > LARGEST_32BIT_INDEX) {
System.gc();
this.ptr = Utilities.UNSAFE.allocateMemory(this.length * this.sizeof);
if (zeroNativeMemory) {
zeroNativeMemory(length);
}
Cleaner.create(this, new Deallocator(this.ptr, this.length, this.sizeof));
MemoryCounter.increaseCounter(this.length * this.sizeof);
} else {
data = new long[(int) length];
}
}
示例8: IntLargeArray
import sun.misc.Cleaner; //導入方法依賴的package包/類
/**
* Creates new instance of this class.
*
* @param length number of elements
* @param zeroNativeMemory if true, then the native memory is zeroed.
*/
public IntLargeArray(long length, boolean zeroNativeMemory)
{
this.type = LargeArrayType.INT;
this.sizeof = 4;
if (length <= 0) {
throw new IllegalArgumentException(length + " is not a positive long value");
}
this.length = length;
if (length > LARGEST_32BIT_INDEX) {
System.gc();
this.ptr = Utilities.UNSAFE.allocateMemory(this.length * this.sizeof);
if (zeroNativeMemory) {
zeroNativeMemory(length);
}
Cleaner.create(this, new Deallocator(this.ptr, this.length, this.sizeof));
MemoryCounter.increaseCounter(this.length * this.sizeof);
} else {
data = new int[(int) length];
}
}
示例9: ByteLargeArray
import sun.misc.Cleaner; //導入方法依賴的package包/類
/**
* Creates new instance of this class.
*
* @param length number of elements
* @param zeroNativeMemory if true, then the native memory is zeroed.
*/
public ByteLargeArray(long length, boolean zeroNativeMemory)
{
this.type = LargeArrayType.BYTE;
this.sizeof = 1;
if (length <= 0) {
throw new IllegalArgumentException(length + " is not a positive long value");
}
this.length = length;
if (length > LARGEST_32BIT_INDEX) {
System.gc();
this.ptr = Utilities.UNSAFE.allocateMemory(this.length * this.sizeof);
if (zeroNativeMemory) {
zeroNativeMemory(length);
}
Cleaner.create(this, new Deallocator(this.ptr, this.length, this.sizeof));
MemoryCounter.increaseCounter(this.length * this.sizeof);
} else {
data = new byte[(int) length];
}
}
示例10: ShortLargeArray
import sun.misc.Cleaner; //導入方法依賴的package包/類
/**
* Creates new instance of this class.
*
* @param length number of elements
* @param zeroNativeMemory if true, then the native memory is zeroed.
*/
public ShortLargeArray(long length, boolean zeroNativeMemory)
{
this.type = LargeArrayType.SHORT;
this.sizeof = 2;
if (length <= 0) {
throw new IllegalArgumentException(length + " is not a positive long value");
}
this.length = length;
if (length > LARGEST_32BIT_INDEX) {
System.gc();
this.ptr = Utilities.UNSAFE.allocateMemory(this.length * this.sizeof);
if (zeroNativeMemory) {
zeroNativeMemory(length);
}
Cleaner.create(this, new Deallocator(this.ptr, this.length, this.sizeof));
MemoryCounter.increaseCounter(this.length * this.sizeof);
} else {
data = new short[(int) length];
}
}
示例11: DoubleLargeArray
import sun.misc.Cleaner; //導入方法依賴的package包/類
/**
* Creates new instance of this class.
*
* @param length number of elements
* @param zeroNativeMemory if true, then the native memory is zeroed.
*/
public DoubleLargeArray(long length, boolean zeroNativeMemory)
{
this.type = LargeArrayType.DOUBLE;
this.sizeof = 8;
if (length <= 0) {
throw new IllegalArgumentException(length + " is not a positive long value");
}
this.length = length;
if (length > LARGEST_32BIT_INDEX) {
System.gc();
this.ptr = Utilities.UNSAFE.allocateMemory(this.length * this.sizeof);
if (zeroNativeMemory) {
zeroNativeMemory(length);
}
Cleaner.create(this, new Deallocator(this.ptr, this.length, this.sizeof));
MemoryCounter.increaseCounter(this.length * this.sizeof);
} else {
data = new double[(int) length];
}
}
示例12: LogicLargeArray
import sun.misc.Cleaner; //導入方法依賴的package包/類
/**
* Creates new instance of this class.
*
* @param length number of elements
* @param zeroNativeMemory if true, then the native memory is zeroed.
*/
public LogicLargeArray(long length, boolean zeroNativeMemory)
{
this.type = LargeArrayType.LOGIC;
this.sizeof = 1;
if (length <= 0) {
throw new IllegalArgumentException(length + " is not a positive long value");
}
this.length = length;
if (length > LARGEST_32BIT_INDEX) {
System.gc();
this.ptr = Utilities.UNSAFE.allocateMemory(this.length * this.sizeof);
if (zeroNativeMemory) {
zeroNativeMemory(length);
}
Cleaner.create(this, new LargeArray.Deallocator(this.ptr, this.length, this.sizeof));
MemoryCounter.increaseCounter(this.length * this.sizeof);
} else {
data = new byte[(int) length];
}
}
示例13: DirectByteBuffer
import sun.misc.Cleaner; //導入方法依賴的package包/類
DirectByteBuffer(int cap) { // package-private
super(-1, 0, cap, cap);
boolean pa = VM.isDirectMemoryPageAligned();
int ps = Bits.pageSize();
long size = Math.max(1L, (long)cap + (pa ? ps : 0));
Bits.reserveMemory(size, cap);
long base = 0;
try {
base = unsafe.allocateMemory(size);
} catch (OutOfMemoryError x) {
Bits.unreserveMemory(size, cap);
throw x;
}
unsafe.setMemory(base, size, (byte) 0);
if (pa && (base % ps != 0)) {
// Round up to page boundary
address = base + ps - (base & (ps - 1));
} else {
address = base;
}
cleaner = Cleaner.create(this, new Deallocator(base, size, cap));
att = null;
}
示例14: allocateMemory
import sun.misc.Cleaner; //導入方法依賴的package包/類
public static long allocateMemory(long size, Object holder) {
final long address = _UNSAFE.allocateMemory(size);
Cleaner.create(holder, new Runnable() {
@Override
public void run() {
_UNSAFE.freeMemory(address);
}
});
return address;
}
示例15: AbstractOffheapArray
import sun.misc.Cleaner; //導入方法依賴的package包/類
public AbstractOffheapArray(long capacity) {
if (capacity < 0) {
throw new IllegalArgumentException("Cannot allocate with capacity=" + capacity);
}
address = UNSAFE.allocateMemory(capacity);
boxedAddress = new AtomicLong(address);
cleaner = Cleaner.create(this, new Deallocator(boxedAddress));
UNSAFE.setMemory(address, capacity, (byte) 0);
this.capacity = capacity;
}