Search in sources :

Example 41 with WeakReference

use of java.lang.ref.WeakReference in project jna by java-native-access.

the class NativeLibraryTest method testAliasSimpleLibraryName.

public void testAliasSimpleLibraryName() throws Exception {
    NativeLibrary nl = NativeLibrary.getInstance("testlib");
    File file = nl.getFile();
    Reference<NativeLibrary> ref = new WeakReference<NativeLibrary>(nl);
    nl = null;
    System.gc();
    long start = System.currentTimeMillis();
    while (ref.get() != null) {
        Thread.sleep(10);
        if ((System.currentTimeMillis() - start) > 5000L) {
            fail("Timed out waiting for library to be GC'd");
        }
    }
    TestLibrary lib = Native.loadLibrary(file.getAbsolutePath(), TestLibrary.class);
    int count = lib.callCount();
    TestLibrary lib2 = Native.loadLibrary("testlib", TestLibrary.class);
    int count2 = lib2.callCount();
    assertEquals("Simple library name not aliased", count + 1, count2);
}
Also used : WeakReference(java.lang.ref.WeakReference) File(java.io.File)

Example 42 with WeakReference

use of java.lang.ref.WeakReference in project jna by java-native-access.

the class MemoryTest method testAvoidGCWithExtantBuffer.

public void testAvoidGCWithExtantBuffer() throws Exception {
    if (!Platform.HAS_BUFFERS)
        return;
    Memory m = new Memory(1024);
    m.clear();
    ByteBuffer b = m.getByteBuffer(0, m.size());
    Reference<Memory> ref = new WeakReference<Memory>(m);
    Reference<ByteBuffer> bref = new WeakReference<ByteBuffer>(b);
    // Create a second byte buffer "equal" to the first
    m = new Memory(1024);
    m.clear();
    m.getByteBuffer(0, m.size());
    m = null;
    System.gc();
    Memory.purge();
    for (int i = 0; i < GC_WAITS && ref.get() != null; i++) {
        Thread.sleep(GC_WAIT_INTERVAL);
        System.gc();
        Memory.purge();
    }
    assertNotNull("Memory GC'd while NIO Buffer still exists", ref.get());
    // Avoid IBM J9 optimization resulting in premature GC of buffer
    b.put((byte) 0);
    b = null;
    System.gc();
    Memory.purge();
    for (int i = 0; i < GC_WAITS && (bref.get() != null || ref.get() != null); i++) {
        Thread.sleep(GC_WAIT_INTERVAL);
        System.gc();
        Memory.purge();
    }
    assertNull("Buffer not GC'd\n", bref.get());
    assertNull("Memory not GC'd after buffer GC'd\n", ref.get());
}
Also used : WeakReference(java.lang.ref.WeakReference) ByteBuffer(java.nio.ByteBuffer)

Example 43 with WeakReference

use of java.lang.ref.WeakReference in project jna by java-native-access.

the class CallbacksTest method testStringCallbackMemoryReclamation.

public void testStringCallbackMemoryReclamation() throws InterruptedException {
    TestLibrary.StringCallback cb = new TestLibrary.StringCallback() {

        @Override
        public String callback(String arg, String arg2) {
            return arg + arg2;
        }
    };
    // A little internal groping
    Map<?, ?> m = CallbackReference.allocations;
    m.clear();
    Charset charset = Charset.forName(Native.getDefaultStringEncoding());
    String arg = getName() + "1" + charset.decode(charset.encode(UNICODE));
    String arg2 = getName() + "2" + charset.decode(charset.encode(UNICODE));
    String value = lib.callStringCallback(cb, arg, arg2);
    WeakReference<Object> ref = new WeakReference<Object>(value);
    arg = null;
    value = null;
    System.gc();
    for (int i = 0; i < 100 && (ref.get() != null || m.size() > 0); ++i) {
        try {
            // Give the GC a chance to run
            Thread.sleep(10);
            System.gc();
        } finally {
        }
    }
    assertNull("NativeString reference not GC'd", ref.get());
    assertEquals("NativeString reference still held: " + m.values(), 0, m.size());
}
Also used : WeakReference(java.lang.ref.WeakReference) Charset(java.nio.charset.Charset)

Example 44 with WeakReference

use of java.lang.ref.WeakReference in project groovy by apache.

the class ClassInfoLeakStressTest method testLeak.

@Test
public void testLeak() {
    assertFalse(Boolean.getBoolean("groovy.use.classvalue"));
    for (int i = 0; i < NUM_OBJECTS; i++) {
        GroovyClassLoader gcl = new GroovyClassLoader();
        Class scriptClass = gcl.parseClass("int myvar = " + i);
        ClassInfo ci = ClassInfo.getClassInfo(scriptClass);
        Reference<ClassLoader> classLoaderRef = new WeakReference<ClassLoader>(gcl, classLoaderQueue);
        Reference<Class<?>> classRef = new WeakReference<Class<?>>(scriptClass, classQueue);
        Reference<ClassInfo> classInfoRef = new WeakReference<ClassInfo>(ci, classInfoQueue);
        refList.add(classLoaderRef);
        refList.add(classRef);
        refList.add(classInfoRef);
        gcl = null;
        scriptClass = null;
        ci = null;
        GCUtils.gc();
    }
    // Add new class to help evict the last collected entry
    GroovyClassLoader gcl = new GroovyClassLoader();
    Class scriptClass = gcl.parseClass("int myvar = 7777");
    ClassInfo ci = ClassInfo.getClassInfo(scriptClass);
    GCUtils.gc();
    // All objects should have been collected
    assertEquals("GroovyClassLoaders not collected by GC", NUM_OBJECTS, queueSize(classLoaderQueue));
    assertEquals("Script Classes not collected by GC", NUM_OBJECTS, queueSize(classQueue));
    int ciSize = queueSize(classInfoQueue);
    assertEquals("ClassInfo objects [" + ciSize + "] collected by GC, expected [" + NUM_OBJECTS + "]", NUM_OBJECTS, ciSize);
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) WeakReference(java.lang.ref.WeakReference) GroovyClassLoader(groovy.lang.GroovyClassLoader) Test(org.junit.Test)

Example 45 with WeakReference

use of java.lang.ref.WeakReference in project PneumaticCraft by MineMaarten.

the class VisNetHandler method calculateNearbyNodes.

private static void calculateNearbyNodes(World world, int x, int y, int z) {
    HashMap<WorldCoordinates, WeakReference<TileVisNode>> sourcelist = sources.get(world.provider.dimensionId);
    if (sourcelist == null) {
        sourcelist = new HashMap<WorldCoordinates, WeakReference<TileVisNode>>();
        return;
    }
    ArrayList<WeakReference<TileVisNode>> cn = new ArrayList<WeakReference<TileVisNode>>();
    WorldCoordinates drainer = new WorldCoordinates(x, y, z, world.provider.dimensionId);
    ArrayList<Object[]> nearby = new ArrayList<Object[]>();
    for (WeakReference<TileVisNode> root : sourcelist.values()) {
        if (!isNodeValid(root))
            continue;
        TileVisNode source = root.get();
        TileVisNode closest = null;
        float range = Float.MAX_VALUE;
        float r = inRange(world, drainer, source.getLocation(), source.getRange());
        if (r > 0) {
            range = r;
            closest = source;
        }
        ArrayList<WeakReference<TileVisNode>> children = new ArrayList<WeakReference<TileVisNode>>();
        children = getAllChildren(source, children);
        for (WeakReference<TileVisNode> child : children) {
            TileVisNode n = child.get();
            if (n != null && !n.equals(root)) {
                float r2 = inRange(n.getWorldObj(), n.getLocation(), drainer, n.getRange());
                if (r2 > 0 && r2 < range) {
                    range = r2;
                    closest = n;
                }
            }
        }
        if (closest != null) {
            cn.add(new WeakReference(closest));
        }
    }
    nearbyNodes.put(drainer, cn);
}
Also used : WeakReference(java.lang.ref.WeakReference) WorldCoordinates(thaumcraft.api.WorldCoordinates) ArrayList(java.util.ArrayList)

Aggregations

WeakReference (java.lang.ref.WeakReference)291 ArrayList (java.util.ArrayList)32 HashMap (java.util.HashMap)24 Map (java.util.Map)24 Field (java.lang.reflect.Field)19 Activity (android.app.Activity)17 Iterator (java.util.Iterator)17 IOException (java.io.IOException)16 Method (java.lang.reflect.Method)15 File (java.io.File)14 URLClassLoader (java.net.URLClassLoader)14 Test (org.junit.Test)13 Reference (java.lang.ref.Reference)12 ReferenceQueue (java.lang.ref.ReferenceQueue)12 Resources (android.content.res.Resources)10 Handler (android.os.Handler)10 AbstractModule (com.google.inject.AbstractModule)10 Injector (com.google.inject.Injector)10 ArrayMap (android.util.ArrayMap)9 URL (java.net.URL)8