Search in sources :

Example 81 with WeakReference

use of java.lang.ref.WeakReference in project android_frameworks_base by DirtyUnicorns.

the class Main method gc.

private static void gc() {
    // See RuntimeUtil#gc in jlibs (http://jlibs.in/)
    Object obj = new Object();
    WeakReference ref = new WeakReference<Object>(obj);
    obj = null;
    while (ref.get() != null) {
        System.gc();
    }
}
Also used : WeakReference(java.lang.ref.WeakReference)

Example 82 with WeakReference

use of java.lang.ref.WeakReference in project OneSignal-Android-SDK by OneSignal.

the class OSObservable method notifyChange.

boolean notifyChange(final StateType state) {
    boolean notified = false;
    for (Object observer : observers) {
        final Object strongRefObserver;
        if (observer instanceof WeakReference)
            strongRefObserver = ((WeakReference) observer).get();
        else
            strongRefObserver = observer;
        if (strongRefObserver != null) {
            try {
                Class<?> clazz = strongRefObserver.getClass();
                final Method method = clazz.getDeclaredMethod(methodName, state.getClass());
                method.setAccessible(true);
                if (fireOnMainThread) {
                    OSUtils.runOnMainUIThread(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                method.invoke(strongRefObserver, state);
                            } catch (Throwable t) {
                                t.printStackTrace();
                            }
                        }
                    });
                } else
                    method.invoke(strongRefObserver, state);
                notified = true;
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }
    return notified;
}
Also used : WeakReference(java.lang.ref.WeakReference) Method(java.lang.reflect.Method)

Example 83 with WeakReference

use of java.lang.ref.WeakReference in project jdk8u_jdk by JetBrains.

the class AppTest method main.

public static void main(String[] args) {
    appsThreadGroup = new ThreadGroup("MyAppsThreadGroup");
    doneSignal = new CountDownLatch(1);
    launchSignal = new CountDownLatch(1);
    Runnable launcher = new Runnable() {

        public void run() {
            try {
                ClassLoader cl = Thread.currentThread().getContextClassLoader();
                Class<?> appMain = cl.loadClass("AppTest");
                Method launch = appMain.getDeclaredMethod("launch", doneSignal.getClass());
                Constructor<?> c = appMain.getConstructor();
                Object o = c.newInstance();
                launch.invoke(o, doneSignal);
            } catch (Throwable e) {
                launchFailure = e;
            } finally {
                launchSignal.countDown();
            }
        }
    };
    /* prepare test  class loader */
    URL pwd = null;
    try {
        pwd = new File(System.getProperty("test.classes", ".")).toURI().toURL();
    } catch (MalformedURLException e) {
        throw new RuntimeException("Test failed.", e);
    }
    URL[] urls = new URL[] { pwd };
    MyClassLoader appClassLoader = new MyClassLoader(urls, "test0");
    WeakReference<MyClassLoader> ref = new WeakReference<>(appClassLoader);
    Thread appThread = new Thread(appsThreadGroup, launcher, "AppThread-0");
    appThread.setContextClassLoader(appClassLoader);
    appThread.start();
    appClassLoader = null;
    launcher = null;
    appThread = null;
    /* wait for laucnh completion */
    try {
        launchSignal.await();
    } catch (InterruptedException e) {
    }
    /* check if launch failed */
    if (launchFailure != null) {
        throw new RuntimeException("Test failed.", launchFailure);
    }
    /* wait for test app excution completion */
    try {
        doneSignal.await();
    } catch (InterruptedException e) {
    }
    /* give a chence to GC */
    waitAndGC(5);
    if (ref.get() != null) {
        throw new RuntimeException("Test failed: classloader is still alive");
    }
    System.out.println("Test passed.");
}
Also used : MalformedURLException(java.net.MalformedURLException) Method(java.lang.reflect.Method) CountDownLatch(java.util.concurrent.CountDownLatch) URL(java.net.URL) WeakReference(java.lang.ref.WeakReference) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Example 84 with WeakReference

use of java.lang.ref.WeakReference in project jdk8u_jdk by JetBrains.

the class ClassLeakTest method main.

public static void main(String[] args) throws Exception {
    System.out.println("Testing that registering and unregistering a " + "Standard MBean does not retain a reference to " + "the MBean's class");
    ClassLoader myClassLoader = ClassLeakTest.class.getClassLoader();
    if (!(myClassLoader instanceof URLClassLoader)) {
        System.out.println("TEST INVALID: test's class loader is not " + "a URLClassLoader");
        System.exit(1);
    }
    URLClassLoader myURLClassLoader = (URLClassLoader) myClassLoader;
    URL[] urls = myURLClassLoader.getURLs();
    PrivateMLet mlet = new PrivateMLet(urls, null, false);
    Class shadowClass = mlet.loadClass(TestMBean.class.getName());
    if (shadowClass == TestMBean.class) {
        System.out.println("TEST INVALID: MLet got original " + "TestMBean not shadow");
        System.exit(1);
    }
    shadowClass = null;
    MBeanServer mbs = MBeanServerFactory.createMBeanServer();
    ObjectName mletName = new ObjectName("x:type=mlet");
    mbs.registerMBean(mlet, mletName);
    ObjectName testName = new ObjectName("x:type=test");
    mbs.createMBean(Test.class.getName(), testName, mletName);
    ClassLoader testLoader = mbs.getClassLoaderFor(testName);
    if (testLoader != mlet) {
        System.out.println("TEST INVALID: MBean's class loader is not " + "MLet: " + testLoader);
        System.exit(1);
    }
    testLoader = null;
    MBeanInfo info = mbs.getMBeanInfo(testName);
    MBeanAttributeInfo[] attrs = info.getAttributes();
    if (attrs.length != 1 || !attrs[0].getName().equals("A") || !attrs[0].isReadable() || !attrs[0].isWritable() || attrs[0].isIs() || !attrs[0].getType().equals("int")) {
        System.out.println("TEST FAILED: unexpected MBeanInfo attrs");
        System.exit(1);
    }
    MBeanOperationInfo[] ops = info.getOperations();
    if (ops.length != 1 || !ops[0].getName().equals("bogus") || ops[0].getSignature().length > 0 || ops[0].getImpact() != MBeanOperationInfo.UNKNOWN || !ops[0].getReturnType().equals("void")) {
        System.out.println("TEST FAILED: unexpected MBeanInfo ops");
        System.exit(1);
    }
    if (info.getConstructors().length != 2) {
        System.out.println("TEST FAILED: wrong number of constructors " + "in introspected bean: " + Arrays.asList(info.getConstructors()));
        System.exit(1);
    }
    if (!info.getClassName().endsWith("Test")) {
        System.out.println("TEST FAILED: wrong info class name: " + info.getClassName());
        System.exit(1);
    }
    mbs.unregisterMBean(testName);
    mbs.unregisterMBean(mletName);
    WeakReference mletRef = new WeakReference(mlet);
    mlet = null;
    System.out.println("MBean registered and unregistered, waiting for " + "garbage collector to collect class loader");
    for (int i = 0; i < 10000 && mletRef.get() != null; i++) {
        System.gc();
        Thread.sleep(1);
    }
    if (mletRef.get() == null)
        System.out.println("Test passed: class loader was GC'd");
    else {
        System.out.println("TEST FAILED: class loader was not GC'd");
        System.exit(1);
    }
}
Also used : WeakReference(java.lang.ref.WeakReference)

Example 85 with WeakReference

use of java.lang.ref.WeakReference in project jdk8u_jdk by JetBrains.

the class LeakTest method testShadow.

private static WeakReference<ClassLoader> testShadow(Class<?> originalTestClass) throws Exception {
    URLClassLoader originalLoader = (URLClassLoader) originalTestClass.getClassLoader();
    URL[] urls = originalLoader.getURLs();
    URLClassLoader shadowLoader = new ShadowClassLoader(urls, originalLoader.getParent());
    System.out.println("Shadow loader is " + shadowLoader);
    String className = originalTestClass.getName();
    Class<?> testClass = Class.forName(className, false, shadowLoader);
    if (testClass.getClassLoader() != shadowLoader) {
        throw new IllegalArgumentException("Loader didn't work: " + testClass.getClassLoader() + " != " + shadowLoader);
    }
    Method main = testClass.getMethod("main", String[].class);
    main.invoke(null, (Object) new String[0]);
    return new WeakReference<ClassLoader>(shadowLoader);
}
Also used : URLClassLoader(java.net.URLClassLoader) WeakReference(java.lang.ref.WeakReference) Method(java.lang.reflect.Method) URL(java.net.URL)

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