JDK-6519180 : Memory leak in Robot on Windows
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 5.0,7
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows,windows_xp
  • CPU: x86
  • Submitted: 2007-01-30
  • Updated: 2011-05-17
  • Resolved: 2011-05-17
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6 JDK 7
6u10Fixed 7 b09Fixed
Related Reports
Relates :  
Description
the problem was reported on forum:

http://forum.java.sun.com/thread.jspa?threadID=5120823&tstart=0

Here is the test to reproduce the problem:

import java.awt.*;
import javax.swing.*;

public class JFrameLeak {

    static final long FREQUENCY = 20 * 1000;

    public static void main(String[] arg) {
        
        JFrame f = new JFrame();
        long counter = 0;
        f.setSize(new Dimension(100,100));
        f.show();
        while(true) {
            try {
                new Robot().mouseMove(100,100);
                counter += 1;
                if (counter % FREQUENCY == 0) {
                    System.out.println("Iteration: " + Long.toString(counter));
                }
            } catch (java.awt.AWTException awte) {
                System.err.println("Caught: " + awte.toString());
            }
        }
    }
}

Comments
EVALUATION So, I have verified that after the fix we do release all memory we allocated for Robot. So, I'm commiting this. Will investigate in background why we still see some problems :(
06-02-2007

SUGGESTED FIX +++ Robot.java 2007-01-30 21:54:41.000000000 +0300 @@ -114,10 +114,12 @@ checkRobotAllowed(); gdLoc = screen.getDefaultConfiguration().getBounds().getLocation(); Toolkit toolkit = Toolkit.getDefaultToolkit(); if (toolkit instanceof ComponentFactory) { peer = ((ComponentFactory)toolkit).createRobot(this, screen); + disposer = new RobotDisposer(peer); + sun.java2d.Disposer.addRecord(anchor, disposer); } } /* determine if the security policy allows Robot's to be created */ private void checkRobotAllowed() { @@ -132,10 +134,26 @@ if (device == null || device.getType() != GraphicsDevice.TYPE_RASTER_SCREEN) { throw new IllegalArgumentException("not a valid screen device"); } } + private transient Object anchor = new Object(); + + static class RobotDisposer implements sun.java2d.DisposerRecord { + private final RobotPeer peer; + public RobotDisposer(RobotPeer peer) { + this.peer = peer; + } + public void dispose() { + if (peer != null) { + peer.dispose(); + } + } + } + + private transient RobotDisposer disposer; + /** * Moves mouse pointer to given screen coordinates. * @param x X position * @param y Y position */ +++ RobotPeer.java 2007-01-30 21:51:35.000000000 +0300 @@ -31,6 +31,8 @@ public void keyPress(int keycode); public void keyRelease(int keycode); public int getRGBPixel(int x, int y); public int [] getRGBPixels(Rectangle bounds); + + public void dispose(); } +++ XRobotPeer.java 2007-01-30 21:57:08.000000000 +0300 @@ -22,10 +22,14 @@ XRobotPeer(GraphicsConfiguration gc) { this.xgc = (X11GraphicsConfig)gc; setup(); } + public void dispose() { + // does nothing + } + public void mouseMove(int x, int y) { mouseMoveImpl(xgc, x, y); } public void mousePress(int buttons) {
06-02-2007

EVALUATION I've implemented disposing of native object (the same way as we do for other components) But I still see the same problem (memory leak :( We, basically, are not receiving notification that Robot is going to be collected for very long time :(
31-01-2007

EVALUATION it looks like we do not delete native class associated with WRobotPeer :( Need to implement this.
30-01-2007