Search in sources :

Example 26 with WeakReference

use of java.lang.ref.WeakReference in project Klyph by jonathangerbaud.

the class UploadPhotoService method onCreate.

@Override
public void onCreate() {
    HandlerThread thread = new HandlerThread("ServiceStartArguments", android.os.Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper, new WeakReference<Service>(this));
}
Also used : HandlerThread(android.os.HandlerThread) WeakReference(java.lang.ref.WeakReference)

Example 27 with WeakReference

use of java.lang.ref.WeakReference in project quasar by puniverse.

the class ActorTest method testWatchGC.

@Test
public void testWatchGC() throws Exception {
    Assume.assumeFalse(Debug.isDebug());
    final Actor<Message, Void> actor = spawnActor(new BasicActor<Message, Void>(mailboxConfig) {

        @Override
        protected Void doRun() throws SuspendExecution, InterruptedException {
            Fiber.sleep(120000);
            return null;
        }
    });
    System.out.println("actor1 is " + actor);
    WeakReference wrActor2 = new WeakReference(spawnActor(new BasicActor<Message, Void>(mailboxConfig) {

        @Override
        protected Void doRun() throws SuspendExecution, InterruptedException {
            Fiber.sleep(10);
            final Object watch = watch(actor.ref());
            //                unwatch(actor, watch);
            return null;
        }
    }));
    System.out.println("actor2 is " + wrActor2.get());
    for (int i = 0; i < 10; i++) {
        Thread.sleep(10);
        System.gc();
    }
    Thread.sleep(2000);
    assertEquals(null, wrActor2.get());
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) WeakReference(java.lang.ref.WeakReference) Test(org.junit.Test)

Example 28 with WeakReference

use of java.lang.ref.WeakReference in project roboguice by roboguice.

the class MapBinderTest method testWeakKeySet_integration_mapbinder.

// Tests for com.google.inject.internal.WeakKeySet not leaking memory.
public void testWeakKeySet_integration_mapbinder() {
    Key<Map<String, String>> mapKey = Key.get(new TypeLiteral<Map<String, String>>() {
    });
    Injector parentInjector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).toInstance("hi");
        }
    });
    WeakKeySetUtils.assertNotBlacklisted(parentInjector, mapKey);
    Injector childInjector = parentInjector.createChildInjector(new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<String, String> binder = MapBinder.newMapBinder(binder(), String.class, String.class);
            binder.addBinding("bar").toInstance("foo");
        }
    });
    WeakReference<Injector> weakRef = new WeakReference<Injector>(childInjector);
    WeakKeySetUtils.assertBlacklisted(parentInjector, mapKey);
    // Clear the ref, GC, and ensure that we are no longer blacklisting.
    childInjector = null;
    WeakKeySetUtils.awaitClear(weakRef);
    WeakKeySetUtils.assertNotBlacklisted(parentInjector, mapKey);
}
Also used : Injector(com.google.inject.Injector) WeakReference(java.lang.ref.WeakReference) Map(java.util.Map) HashMap(java.util.HashMap) AbstractModule(com.google.inject.AbstractModule)

Example 29 with WeakReference

use of java.lang.ref.WeakReference in project roboguice by roboguice.

the class OptionalBinderTest method testWeakKeySet_integration.

// Tests for com.google.inject.internal.WeakKeySet not leaking memory.
public void testWeakKeySet_integration() {
    Injector parentInjector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).toInstance("hi");
        }
    });
    WeakKeySetUtils.assertNotBlacklisted(parentInjector, Key.get(Integer.class));
    Injector childInjector = parentInjector.createChildInjector(new AbstractModule() {

        @Override
        protected void configure() {
            OptionalBinder.newOptionalBinder(binder(), Integer.class).setDefault().toInstance(4);
        }
    });
    WeakReference<Injector> weakRef = new WeakReference<Injector>(childInjector);
    WeakKeySetUtils.assertBlacklisted(parentInjector, Key.get(Integer.class));
    // Clear the ref, GC, and ensure that we are no longer blacklisting.
    childInjector = null;
    WeakKeySetUtils.awaitClear(weakRef);
    WeakKeySetUtils.assertNotBlacklisted(parentInjector, Key.get(Integer.class));
}
Also used : Injector(com.google.inject.Injector) WeakReference(java.lang.ref.WeakReference) AbstractModule(com.google.inject.AbstractModule)

Example 30 with WeakReference

use of java.lang.ref.WeakReference in project roboguice by roboguice.

the class WeakKeySetTest method testWeakKeySet_integration.

public void testWeakKeySet_integration() {
    Injector parentInjector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Integer.class).toInstance(4);
        }
    });
    assertNotBlacklisted(parentInjector, Key.get(String.class));
    Injector childInjector = parentInjector.createChildInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).toInstance("bar");
        }
    });
    WeakReference<Injector> weakRef = new WeakReference<Injector>(childInjector);
    assertBlacklisted(parentInjector, Key.get(String.class));
    // Clear the ref, GC, and ensure that we are no longer blacklisting.
    childInjector = null;
    awaitClear(weakRef);
    assertNotBlacklisted(parentInjector, Key.get(String.class));
}
Also used : Injector(com.google.inject.Injector) WeakReference(java.lang.ref.WeakReference) AbstractModule(com.google.inject.AbstractModule)

Aggregations

WeakReference (java.lang.ref.WeakReference)277 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)19 Map (java.util.Map)19 Field (java.lang.reflect.Field)18 Activity (android.app.Activity)17 Method (java.lang.reflect.Method)15 File (java.io.File)14 IOException (java.io.IOException)13 URLClassLoader (java.net.URLClassLoader)13 Test (org.junit.Test)13 Reference (java.lang.ref.Reference)12 ReferenceQueue (java.lang.ref.ReferenceQueue)12 Iterator (java.util.Iterator)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