Search in sources :

Example 86 with WeakReference

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

the class LWDispatcherMemoryLeakTest method init.

public static void init() throws Throwable {
    SwingUtilities.invokeAndWait(new Runnable() {

        @Override
        public void run() {
            frame = new JFrame();
            frame.setLayout(new FlowLayout());
            button = new WeakReference<JButton>(new JButton("Text"));
            p = new WeakReference<JPanel>(new JPanel(new FlowLayout()));
            p.get().add(button.get());
            frame.add(p.get());
            frame.setBounds(500, 400, 200, 200);
            frame.setVisible(true);
        }
    });
    Util.waitTillShown(button.get());
    Util.clickOnComp(button.get(), new Robot());
    SwingUtilities.invokeAndWait(new Runnable() {

        @Override
        public void run() {
            frame.remove(p.get());
        }
    });
    Util.waitForIdle(null);
    assertGC();
}
Also used : JPanel(javax.swing.JPanel) FlowLayout(java.awt.FlowLayout) JFrame(javax.swing.JFrame) WeakReference(java.lang.ref.WeakReference) JButton(javax.swing.JButton) Robot(java.awt.Robot)

Example 87 with WeakReference

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

the class InputContextMemoryLeakTest method init.

public static void init() throws Throwable {
    SwingUtilities.invokeAndWait(new Runnable() {

        @Override
        public void run() {
            frame = new JFrame();
            frame.setLayout(new FlowLayout());
            JPanel p1 = new JPanel();
            button = new JButton("Test");
            p1.add(button);
            frame.add(p1);
            text = new WeakReference<JTextField>(new JTextField("Text"));
            p = new WeakReference<JPanel>(new JPanel(new FlowLayout()));
            p.get().add(text.get());
            frame.add(p.get());
            frame.setBounds(500, 400, 200, 200);
            frame.setVisible(true);
        }
    });
    Util.focusComponent(text.get(), 500);
    Util.clickOnComp(button, new Robot());
    //References to objects testes for memory leak are stored in Util.
    //Need to clean them
    Util.cleanUp();
    SwingUtilities.invokeAndWait(new Runnable() {

        @Override
        public void run() {
            frame.remove(p.get());
        }
    });
    Util.waitForIdle(null);
    //After the next caret blink it automatically TextField references
    Thread.sleep(text.get().getCaret().getBlinkRate() * 2);
    Util.waitForIdle(null);
    assertGC();
}
Also used : JPanel(javax.swing.JPanel) FlowLayout(java.awt.FlowLayout) JFrame(javax.swing.JFrame) WeakReference(java.lang.ref.WeakReference) JButton(javax.swing.JButton) JTextField(javax.swing.JTextField) Robot(java.awt.Robot)

Example 88 with WeakReference

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

the class RuntimeThreadInheritanceLeak method main.

public static void main(String[] args) {
    System.err.println("\nRegression test for bug 4404702\n");
    /*
         * HACK: Work around the fact that java.util.logging.LogManager's
         * (singleton) construction also has this bug-- it will register a
         * "shutdown hook", i.e. a thread, which will inherit and pin the
         * current thread's context class loader for the lifetime of the VM--
         * by causing the LogManager to be initialized now, instead of by
         * RMI when our special context class loader is set.
         */
    java.util.logging.LogManager.getLogManager();
    /*
         * HACK: Work around the fact that the non-native, thread-based
         * SecureRandom seed generator (ThreadedSeedGenerator) seems to
         * have this bug too (which had been causing this test to fail
         * when run with jtreg on Windows XP-- see 4910382).
         */
    (new java.security.SecureRandom()).nextInt();
    RuntimeThreadInheritanceLeak obj = new RuntimeThreadInheritanceLeak();
    try {
        ClassLoader loader = URLClassLoader.newInstance(new URL[0]);
        ReferenceQueue refQueue = new ReferenceQueue();
        Reference loaderRef = new WeakReference(loader, refQueue);
        System.err.println("created loader: " + loader);
        Thread.currentThread().setContextClassLoader(loader);
        UnicastRemoteObject.exportObject(obj);
        Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
        System.err.println("exported remote object with loader as context class loader");
        loader = null;
        System.err.println("nulled strong reference to loader");
        UnicastRemoteObject.unexportObject(obj, true);
        System.err.println("unexported remote object");
        /*
             * HACK: Work around the fact that the sun.misc.GC daemon thread
             * also has this bug-- it will have inherited our loader as its
             * context class loader-- by giving it a chance to pass away.
             */
        Thread.sleep(2000);
        System.gc();
        System.err.println("waiting to be notified of loader being weakly reachable...");
        Reference dequeued = refQueue.remove(TIMEOUT);
        if (dequeued == null) {
            System.err.println("TEST FAILED: loader not deteced weakly reachable");
            dumpThreads();
            throw new RuntimeException("TEST FAILED: loader not detected weakly reachable");
        }
        System.err.println("TEST PASSED: loader detected weakly reachable");
        dumpThreads();
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException("TEST FAILED: unexpected exception", e);
    } finally {
        try {
            UnicastRemoteObject.unexportObject(obj, true);
        } catch (RemoteException e) {
        }
    }
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) Reference(java.lang.ref.Reference) WeakReference(java.lang.ref.WeakReference) WeakReference(java.lang.ref.WeakReference) URLClassLoader(java.net.URLClassLoader) RemoteException(java.rmi.RemoteException) RemoteException(java.rmi.RemoteException)

Example 89 with WeakReference

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

the class PinClientSocketFactory method main.

public static void main(String[] args) throws Exception {
    System.err.println("\nRegression test for bug 4486732\n");
    Factory factoryImpl = new FactoryImpl();
    Factory factoryStub = (Factory) UnicastRemoteObject.exportObject(factoryImpl, 0);
    for (int i = 0; i < SESSIONS; i++) {
        Session session = factoryStub.getSession();
        session.ping();
    }
    UnicastRemoteObject.unexportObject(factoryImpl, true);
    Registry registryImpl = TestLibrary.createRegistryOnEphemeralPort();
    int port = TestLibrary.getRegistryPort(registryImpl);
    System.out.println("Registry listening on port " + port);
    CSF csf = new CSF();
    Reference<CSF> registryRef = new WeakReference<CSF>(csf);
    Registry registryStub = LocateRegistry.getRegistry("", port, csf);
    csf = null;
    registryStub.list();
    registryStub = null;
    UnicastRemoteObject.unexportObject(registryImpl, true);
    System.gc();
    // allow connections to time out
    Thread.sleep(3 * Long.getLong("sun.rmi.transport.connectionTimeout", 15000));
    System.gc();
    if (CSF.deserializedInstances.size() != SESSIONS) {
        throw new Error("unexpected number of deserialized instances: " + CSF.deserializedInstances.size());
    }
    int nonNullCount = 0;
    for (Reference<CSF> ref : CSF.deserializedInstances) {
        csf = ref.get();
        if (csf != null) {
            System.err.println("non-null deserialized instance: " + csf);
            nonNullCount++;
        }
    }
    if (nonNullCount > 0) {
        throw new Error("TEST FAILED: " + nonNullCount + " non-null deserialized instances");
    }
    csf = registryRef.get();
    if (csf != null) {
        System.err.println("non-null registry instance: " + csf);
        throw new Error("TEST FAILED: non-null registry instance");
    }
    System.err.println("TEST PASSED");
}
Also used : WeakReference(java.lang.ref.WeakReference) RMIServerSocketFactory(java.rmi.server.RMIServerSocketFactory) RMIClientSocketFactory(java.rmi.server.RMIClientSocketFactory) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry)

Example 90 with WeakReference

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

the class PinLastArguments method main.

public static void main(String[] args) throws Exception {
    System.err.println("\nRegression test for bug 6332349\n");
    Ping impl = new PingImpl();
    Reference<?> ref = new WeakReference<Ping>(impl);
    try {
        Ping stub = (Ping) UnicastRemoteObject.exportObject(impl, 0);
        Object notSerializable = new Object();
        stub.ping(impl, null);
        try {
            stub.ping(impl, notSerializable);
        } catch (MarshalException e) {
            if (e.getCause() instanceof NotSerializableException) {
                System.err.println("ping invocation failed as expected");
            } else {
                throw e;
            }
        }
    } finally {
        UnicastRemoteObject.unexportObject(impl, true);
    }
    impl = null;
    // expected we will hang here until timed out by the test harness.
    while (true) {
        System.gc();
        Thread.sleep(20);
        if (ref.get() == null) {
            break;
        }
    }
    System.err.println("TEST PASSED");
}
Also used : MarshalException(java.rmi.MarshalException) NotSerializableException(java.io.NotSerializableException) WeakReference(java.lang.ref.WeakReference) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject)

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