當前位置: 首頁>>代碼示例>>Java>>正文


Java Cleaner類代碼示例

本文整理匯總了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();
        }
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:27,代碼來源:Reference.java

示例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");
}
 
開發者ID:actiontech,項目名稱:dble,代碼行數:29,代碼來源:Platform.java

示例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;
}
 
開發者ID:jskierbi,項目名稱:intellij-ce-playground,代碼行數:19,代碼來源:DirectBufferWrapper.java

示例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();
        }
    });
}
 
開發者ID:anhnv-3991,項目名稱:VoltDB,代碼行數:32,代碼來源:ElasticHashinator.java

示例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));



    }
 
開發者ID:jgaltidor,項目名稱:VarJ,代碼行數:25,代碼來源:DirectByteBuffer.java

示例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;
    }
}
 
開發者ID:helyho,項目名稱:Voovan,代碼行數:24,代碼來源:TByteBuffer.java

示例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);
    }
}
 
開發者ID:reprogrammer,項目名稱:checker-framework,代碼行數:27,代碼來源:Reference.java

示例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;
      }
    });
  }
}
 
開發者ID:quoll,項目名稱:mulgara,代碼行數:25,代碼來源:MappingUtil.java

示例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));
}
 
開發者ID:RuedigerMoeller,項目名稱:fast-serialization,代碼行數:21,代碼來源:MMFBytez.java

示例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);
    }
}
 
開發者ID:Netflix,項目名稱:EVCache,代碼行數:19,代碼來源:EVCacheNodeImpl.java

示例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];
    }
}
 
開發者ID:IcmVis,項目名稱:JLargeArrays,代碼行數:27,代碼來源:FloatLargeArray.java

示例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];
    }
}
 
開發者ID:IcmVis,項目名稱:JLargeArrays,代碼行數:27,代碼來源:LongLargeArray.java

示例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];
    }
}
 
開發者ID:IcmVis,項目名稱:JLargeArrays,代碼行數:27,代碼來源:IntLargeArray.java

示例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];
    }
}
 
開發者ID:IcmVis,項目名稱:JLargeArrays,代碼行數:27,代碼來源:ByteLargeArray.java

示例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];
    }
}
 
開發者ID:IcmVis,項目名稱:JLargeArrays,代碼行數:27,代碼來源:ShortLargeArray.java


注:本文中的sun.misc.Cleaner類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。