本文整理汇总了Java中sun.misc.Cleaner类的典型用法代码示例。如果您正苦于以下问题:Java Cleaner类的具体用法?Java Cleaner怎么用?Java Cleaner使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Cleaner类属于sun.misc包,在下文中一共展示了Cleaner类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: Reference
import sun.misc.Cleaner; //导入依赖的package包/类
Reference(T referent, ReferenceQueue queue)
{
this.queue = queue == null ? ReferenceQueue.NULL : queue;
if (referent instanceof Class && noclassgc())
{
// We don't do Class gc, so no point in using a weak reference for classes.
weakRef = null;
strongRef = referent;
}
else if (referent == null)
{
weakRef = null;
}
else
{
weakRef = new cli.System.WeakReference(referent, this instanceof PhantomReference);
if (queue != null)
{
queue.addToActiveList(this);
}
if (this instanceof Cleaner)
{
new CleanerGuard();
}
}
}
示例2: 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");
}
示例3: disposeDirectBuffer
import sun.misc.Cleaner; //导入依赖的package包/类
static boolean disposeDirectBuffer(final ByteBuffer buffer) {
return AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
@Nullable
public Object run() {
try {
if (buffer instanceof DirectBuffer) {
Cleaner cleaner = ((DirectBuffer)buffer).cleaner();
if (cleaner != null) cleaner.clean(); // Already cleaned otherwise
}
return null;
}
catch (Throwable e) {
return buffer;
}
}
}) == null;
}
示例4: 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();
}
});
}
示例5: 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));
}
示例6: 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;
}
}
示例7: run
import sun.misc.Cleaner; //导入依赖的package包/类
public void run() {
for (;;) {
Reference r;
synchronized (lock) {
if (pending != null) {
r = pending;
pending = r.discovered;
r.discovered = null;
} else {
try {
lock.wait();
} catch (InterruptedException x) { }
continue;
}
}
// Fast path for cleaners
if (r instanceof Cleaner) {
((Cleaner)r).clean();
continue;
}
ReferenceQueue q = r.queue;
if (q != ReferenceQueue.NULL) q.enqueue(r);
}
}
示例8: clean
import sun.misc.Cleaner; //导入依赖的package包/类
/**
* Attempts to pre-emptively invoke the cleaner method on the given object instead
* of waiting for the garbage collector to do it. We do this under Windows because
* errors can result if a mapped byte buffer is cleaned after its associated file
* is closed, truncated, deleted, etc.
* @param buffer The buffer to release.
*/
private static void clean(final Object buffer) {
if (buffer != null) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
try {
Method getCleanerMethod = buffer.getClass().getMethod("cleaner", new Class[0]);
getCleanerMethod.setAccessible(true);
Cleaner cleaner = (Cleaner)getCleanerMethod.invoke(buffer, new Object[0]);
cleaner.clean();
} catch (Exception e) {
logger.warn("Error cleaning buffer", e);
}
return null;
}
});
}
}
示例9: 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));
}
示例10: shutdown
import sun.misc.Cleaner; //导入依赖的package包/类
public void shutdown() {
removeMonitoring();
writeQ.clear();
readQ.clear();
inputQueue.clear();
try {
// Cleanup the ByteBuffers only if they are sun.nio.ch.DirectBuffer
// If we don't cleanup then we will leak 16K of memory
if (getRbuf() instanceof DirectBuffer) {
Cleaner cleaner = ((DirectBuffer) getRbuf()).cleaner();
if (cleaner != null) cleaner.clean();
cleaner = ((DirectBuffer) getWbuf()).cleaner();
if (cleaner != null) cleaner.clean();
}
} catch (Throwable t) {
getLogger().error("Exception cleaning ByteBuffer.", t);
}
}
示例11: 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];
}
}
示例12: 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];
}
}
示例13: 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];
}
}
示例14: 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];
}
}
示例15: 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];
}
}