use of sun.misc.Cleaner in project hive by apache.
the class SimpleAllocator method deallocate.
@Override
public void deallocate(MemoryBuffer buffer) {
LlapDataBuffer buf = (LlapDataBuffer) buffer;
ByteBuffer bb = buf.byteBuffer;
buf.byteBuffer = null;
if (!bb.isDirect())
return;
Field field = cleanerField;
if (field == null)
return;
try {
((Cleaner) field.get(bb)).clean();
} catch (Throwable t) {
LlapIoImpl.LOG.warn("Error using DirectByteBuffer cleaner; stopping its use", t);
cleanerField = null;
}
}
use of sun.misc.Cleaner in project Mycat-Server by MyCATApache.
the class Platform method allocateDirectBuffer.
/**
* 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");
}
use of sun.misc.Cleaner in project mapdb by jankotek.
the class ByteBufferVol method unmap.
/**
* Hack to unmap MappedByteBuffer.
* Unmap is necessary on Windows, otherwise file is locked until JVM exits or BB is GCed.
* There is no public JVM API to unmap buffer, so this tries to use SUN proprietary API for unmap.
* Any error is silently ignored (for example SUN API does not exist on Android).
*/
protected static boolean unmap(MappedByteBuffer b) {
if (!unmapHackSupported) {
return false;
}
if (!(b instanceof DirectBuffer))
return false;
// need to dispose old direct buffer, see bug
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038
DirectBuffer bb = (DirectBuffer) b;
Cleaner c = bb.cleaner();
if (c != null) {
c.clean();
return true;
}
Object attachment = bb.attachment();
return attachment != null && attachment instanceof DirectBuffer && attachment != b && unmap((MappedByteBuffer) attachment);
}
use of sun.misc.Cleaner in project jdk8u_jdk by JetBrains.
the class Reference method tryHandlePending.
/**
* Try handle pending {@link Reference} if there is one.<p>
* Return {@code true} as a hint that there might be another
* {@link Reference} pending or {@code false} when there are no more pending
* {@link Reference}s at the moment and the program can do some other
* useful work instead of looping.
*
* @param waitForNotify if {@code true} and there was no pending
* {@link Reference}, wait until notified from VM
* or interrupted; if {@code false}, return immediately
* when there is no pending {@link Reference}.
* @return {@code true} if there was a {@link Reference} pending and it
* was processed, or we waited for notification and either got it
* or thread was interrupted before being notified;
* {@code false} otherwise.
*/
static boolean tryHandlePending(boolean waitForNotify) {
Reference<Object> r;
Cleaner c;
try {
synchronized (lock) {
if (pending != null) {
r = pending;
// 'instanceof' might throw OutOfMemoryError sometimes
// so do this before un-linking 'r' from the 'pending' chain...
c = r instanceof Cleaner ? (Cleaner) r : null;
// unlink 'r' from 'pending' chain
pending = r.discovered;
r.discovered = null;
} else {
// because it may try to allocate exception objects.
if (waitForNotify) {
lock.wait();
}
// retry if waited
return waitForNotify;
}
}
} catch (OutOfMemoryError x) {
// Give other threads CPU time so they hopefully drop some live references
// and GC reclaims some space.
// Also prevent CPU intensive spinning in case 'r instanceof Cleaner' above
// persistently throws OOME for some time...
Thread.yield();
// retry
return true;
} catch (InterruptedException x) {
// retry
return true;
}
// Fast path for cleaners
if (c != null) {
c.clean();
return true;
}
ReferenceQueue<? super Object> q = r.queue;
if (q != ReferenceQueue.NULL)
q.enqueue(r);
return true;
}
use of sun.misc.Cleaner in project druid by druid-io.
the class OffHeapNamespaceExtractionCacheManager method createCache.
@Override
public CacheHandler createCache() {
ConcurrentMap<String, String> cache;
String mapDbKey;
// This loop will succeed because 2^64 cache maps couldn't exist in memory simultaneously
while (true) {
mapDbKey = Long.toString(mapDbKeyCounter.getAndIncrement());
try {
HTreeMap<String, String> hTreeMap = mmapDB.createHashMap(mapDbKey).make();
// Access MapDB's HTreeMap and create a cleaner via proxy, because there is no 100% confidence that there are
// no memory leaks in MapDB and in OffHeapCacheManager. Otherwise JVM will never be able to clean the cleaner
// and dispose leaked cache.
cache = new CacheProxy(hTreeMap);
cacheCount.incrementAndGet();
break;
} catch (IllegalArgumentException e) {
// failed to create a map, the key exists, go to the next iteration
}
}
MapDbCacheDisposer cacheDisposer = new MapDbCacheDisposer(mapDbKey);
// Cleaner is "the second level of defence". Normally all users of createCache() must call disposeCache() with
// the returned CacheHandler instance manually. But if they don't do this for whatever reason, JVM will cleanup
// the cache itself.
Cleaner cleaner = Cleaner.create(cache, cacheDisposer);
MapDbCacheDisposerAndCleaner disposerAndCleaner = new MapDbCacheDisposerAndCleaner(cacheDisposer, cleaner);
return new CacheHandler(this, cache, disposerAndCleaner);
}
Aggregations