sun.misc.Cleaner Java Examples
The following examples show how to use
sun.misc.Cleaner.
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: LocalStoreFile.java From journalkeeper with Apache License 2.0 | 6 votes |
private void unloadMappedBuffer() { try { final Buffer mapped = pageBuffer; pageBuffer = null; this.bufferType = NO_BUFFER; if (null != mapped) { Method getCleanerMethod; getCleanerMethod = mapped.getClass().getMethod("cleaner"); getCleanerMethod.setAccessible(true); Cleaner cleaner = (Cleaner) getCleanerMethod.invoke(mapped, new Object[0]); cleaner.clean(); } bufferPool.releaseMMap(this); } catch (Exception e) { logger.warn("Release direct buffer exception: ", e); } }
Example #2
Source File: AllocateDirectMap.java From incubator-datasketches-memory with Apache License 2.0 | 6 votes |
@SuppressWarnings("resource") AllocateDirectMap(final File file, final long fileOffsetBytes, final long capacityBytes, final boolean localReadOnly) { this.capacityBytes = capacityBytes; resourceReadOnly = isFileReadOnly(file); final long fileLength = file.length(); if ((localReadOnly || resourceReadOnly) && fileOffsetBytes + capacityBytes > fileLength) { throw new IllegalArgumentException( "Read-only mode and requested map length is greater than current file length: " + "Requested Length = " + (fileOffsetBytes + capacityBytes) + ", Current File Length = " + fileLength); } raf = mapper(file, fileOffsetBytes, capacityBytes, resourceReadOnly); nativeBaseOffset = map(raf.getChannel(), resourceReadOnly, fileOffsetBytes, capacityBytes); deallocator = new Deallocator(nativeBaseOffset, capacityBytes, raf); cleaner = Cleaner.create(this, deallocator); }
Example #3
Source File: Platform.java From dble with GNU General Public License v2.0 | 6 votes |
/** * 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"); }
Example #4
Source File: TextClassifierImpl.java From android_9.0.0_r45 with Apache License 2.0 | 6 votes |
private TextClassifierImplNative getNative(LocaleList localeList) throws FileNotFoundException { synchronized (mLock) { localeList = localeList == null ? LocaleList.getEmptyLocaleList() : localeList; final ModelFile bestModel = findBestModelLocked(localeList); if (bestModel == null) { throw new FileNotFoundException("No model for " + localeList.toLanguageTags()); } if (mNative == null || mNative.isClosed() || !Objects.equals(mModel, bestModel)) { Log.d(DEFAULT_LOG_TAG, "Loading " + bestModel); final ParcelFileDescriptor fd = ParcelFileDescriptor.open( new File(bestModel.getPath()), ParcelFileDescriptor.MODE_READ_ONLY); mNative = new TextClassifierImplNative(fd.getFd()); mNativeCleaner = Cleaner.create(this, new NativeCloser(mNative)); closeAndLogError(fd); mModel = bestModel; } return mNative; } }
Example #5
Source File: UnsafeHelper.java From gadtry with Apache License 2.0 | 6 votes |
/** * 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). * * @param size allocate mem size * @return ByteBuffer */ @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); long memory = _UNSAFE.allocateMemory(size); ByteBuffer buffer = (ByteBuffer) constructor.newInstance(memory, size); Cleaner cleaner = Cleaner.create(buffer, () -> _UNSAFE.freeMemory(memory)); cleanerField.set(buffer, cleaner); return buffer; } catch (Exception e) { throwException(e); } throw new IllegalStateException("unreachable"); }
Example #6
Source File: StoreFileImpl.java From joyqueue with Apache License 2.0 | 6 votes |
private void unloadMappedBuffer() { try { final Buffer mapped = pageBuffer; pageBuffer = null; this.bufferType = NO_BUFFER; if (null != mapped) { Method getCleanerMethod; getCleanerMethod = mapped.getClass().getMethod("cleaner"); getCleanerMethod.setAccessible(true); Cleaner cleaner = (Cleaner) getCleanerMethod.invoke(mapped, new Object[0]); cleaner.clean(); } bufferPool.releaseMMap(this); } catch (Exception e) { logger.warn("Release direct buffer exception: ", e); } }
Example #7
Source File: Platform.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
/** * 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"); }
Example #8
Source File: DirectByteBuffer.java From JDKSourceCode1.8 with MIT License | 5 votes |
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; }
Example #9
Source File: DirectByteBuffer.java From jdk1.8-source-analysis with Apache License 2.0 | 5 votes |
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; }
Example #10
Source File: DirectByteBuffer.java From Java8CN with Apache License 2.0 | 5 votes |
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; }
Example #11
Source File: MemoryUtil.java From indexr with Apache License 2.0 | 5 votes |
public static ByteBuffer getByteBuffer(long address, int length, boolean autoFree) { ByteBuffer instance = getHollowDirectByteBuffer(); if (autoFree) { Cleaner cleaner = Cleaner.create(instance, new Deallocator(address)); setByteBuffer(instance, address, length, cleaner); } else { setByteBuffer(instance, address, length, null); } instance.order(ByteOrder.nativeOrder()); return instance; }
Example #12
Source File: PreloadBufferPool.java From joyqueue with Apache License 2.0 | 5 votes |
private void releaseIfDirect(ByteBuffer byteBuffer) { if (byteBuffer instanceof DirectBuffer) { try { Method getCleanerMethod; getCleanerMethod = byteBuffer.getClass().getMethod("cleaner"); getCleanerMethod.setAccessible(true); Cleaner cleaner = (Cleaner) getCleanerMethod.invoke(byteBuffer, new Object[0]); cleaner.clean(); } catch (Exception e) { logger.warn("Exception: ", e); } } }
Example #13
Source File: DirectBufCloser.java From qmq with Apache License 2.0 | 5 votes |
public static void close(ByteBuffer buffer) { if (buffer == null || !buffer.isDirect() || buffer.capacity() == 0) { return; } try { final Cleaner cleaner = ((DirectBuffer) buffer).cleaner(); cleaner.clean(); } catch (Exception ignore) { } }
Example #14
Source File: LogSegment.java From qmq with Apache License 2.0 | 5 votes |
private void clean(final ByteBuffer buffer) { if (buffer == null || !buffer.isDirect() || buffer.capacity() == 0) { return; } final Cleaner cleaner = ((DirectBuffer) buffer).cleaner(); cleaner.clean(); }
Example #15
Source File: V6PullConsumer.java From coding-snippets with MIT License | 5 votes |
public void cleanMbb(MappedByteBuffer mapBuffer) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ // long s = System.currentTimeMillis(); Method getCleanerMethod = mapBuffer.getClass().getMethod("cleaner", new Class[0]); getCleanerMethod.setAccessible(true); sun.misc.Cleaner cleaner = (Cleaner) getCleanerMethod.invoke(mapBuffer, new Object[0]); cleaner.clean(); // System.out.println("清理mbb耗时(ms):"+(System.currentTimeMillis() - s)); }
Example #16
Source File: DefaultPullConsumer.java From coding-snippets with MIT License | 5 votes |
public void cleanMbb(MappedByteBuffer mapBuffer) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ // long s = System.currentTimeMillis(); Method getCleanerMethod = mapBuffer.getClass().getMethod("cleaner", new Class[0]); getCleanerMethod.setAccessible(true); sun.misc.Cleaner cleaner = (Cleaner) getCleanerMethod.invoke(mapBuffer, new Object[0]); cleaner.clean(); // System.out.println("清理mbb耗时(ms):"+(System.currentTimeMillis() - s)); }
Example #17
Source File: ByteBufferUtil.java From indexr with Apache License 2.0 | 5 votes |
public static void free(ByteBuffer buffer) { if (buffer.isDirect()) { Cleaner cleaner = ((DirectBuffer) buffer).cleaner(); if (cleaner != null) { cleaner.clean(); } } }
Example #18
Source File: V7PullConsumer.java From coding-snippets with MIT License | 5 votes |
public void cleanMbb(MappedByteBuffer mapBuffer) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ // long s = System.currentTimeMillis(); Method getCleanerMethod = mapBuffer.getClass().getMethod("cleaner", new Class[0]); getCleanerMethod.setAccessible(true); sun.misc.Cleaner cleaner = (Cleaner) getCleanerMethod.invoke(mapBuffer, new Object[0]); cleaner.clean(); // System.out.println("清理mbb耗时(ms):"+(System.currentTimeMillis() - s)); }
Example #19
Source File: PreloadBufferPool.java From journalkeeper with Apache License 2.0 | 5 votes |
private void releaseIfDirect(ByteBuffer byteBuffer) { if (byteBuffer instanceof DirectBuffer) { try { Method getCleanerMethod; getCleanerMethod = byteBuffer.getClass().getMethod("cleaner"); getCleanerMethod.setAccessible(true); Cleaner cleaner = (Cleaner) getCleanerMethod.invoke(byteBuffer, new Object[0]); cleaner.clean(); } catch (Exception e) { logger.warn("Exception: ", e); } } }
Example #20
Source File: NativeBuffer.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
Cleaner cleaner() { return cleaner; }
Example #21
Source File: NativeBuffer.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
Cleaner cleaner() { return cleaner; }
Example #22
Source File: FileChannelImpl.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
private static void unmap(MappedByteBuffer bb) { Cleaner cl = ((DirectBuffer)bb).cleaner(); if (cl != null) cl.clean(); }
Example #23
Source File: NativeBuffer.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
Cleaner cleaner() { return cleaner; }
Example #24
Source File: NativeBuffer.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
NativeBuffer(int size) { this.address = unsafe.allocateMemory(size); this.size = size; this.cleaner = Cleaner.create(this, new Deallocator(address)); }
Example #25
Source File: NativeBuffer.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
Cleaner cleaner() { return cleaner; }
Example #26
Source File: SysMemAllocator.java From Mnemonic with Apache License 2.0 | 4 votes |
/** * Constructor, it initialize and allocate a memory pool from Java off-heap * with specified capacity. * * @param capacity * specify the capacity of a system memory pool * * @param isnew * a place holder, always specify it as true * * @throws Exception * fail to retrieve Unsafe object * */ public SysMemAllocator(long capacity, boolean isnew) throws Exception { m_unsafe = Utils.getUnsafe(); maxStoreCapacity = capacity; /** * create a resource collector to release specified bytebuffer that backed * by Java off-heap. */ m_bufcollector = new ResCollector<MemBufferHolder<SysMemAllocator>, ByteBuffer>( new ResReclaim<ByteBuffer>() { @Override public synchronized void reclaim(ByteBuffer mres) { Long sz = Long.valueOf(mres.capacity()); boolean cb_reclaimed = false; if (null != m_bufferreclaimer) { cb_reclaimed = m_bufferreclaimer.reclaim(mres, sz); } if (!cb_reclaimed) { try { Field cleanerField; cleanerField = mres.getClass().getDeclaredField( "cleaner"); cleanerField.setAccessible(true); Cleaner cleaner = (Cleaner) cleanerField.get(mres); cleaner.clean(); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); } mres = null; } currentMemory.addAndGet(-sz); } }); /** * create a resource collector to release specified chunk that backed by * Java off-heap. */ m_chunkcollector = new ResCollector<MemChunkHolder<SysMemAllocator>, Long>( new ResReclaim<Long>() { @Override public synchronized void reclaim(Long mres) { // System.out.println(String.format("Reclaim: %X ...", mres)); Long sz = m_chunksize.remove(mres); boolean cb_reclaimed = false; if (null != m_chunkreclaimer) { cb_reclaimed = m_chunkreclaimer.reclaim(mres, sz); } if (!cb_reclaimed) { m_unsafe.freeMemory(mres); mres = null; } if (null != sz) { currentMemory.addAndGet(-sz); } } }); }
Example #27
Source File: NativeBuffer.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
NativeBuffer(int size) { this.address = unsafe.allocateMemory(size); this.size = size; this.cleaner = Cleaner.create(this, new Deallocator(address)); }
Example #28
Source File: NativeBuffer.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
Cleaner cleaner() { return cleaner; }
Example #29
Source File: NativeBuffer.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
NativeBuffer(int size) { this.address = unsafe.allocateMemory(size); this.size = size; this.cleaner = Cleaner.create(this, new Deallocator(address)); }
Example #30
Source File: NativeBuffer.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
Cleaner cleaner() { return cleaner; }