United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 6519180 Memory leak in Robot on Windows
6519180 : Memory leak in Robot on Windows

Details
Type:
Bug
Submit Date:
2007-01-30
Status:
Closed
Updated Date:
2011-05-17
Project Name:
JDK
Resolved Date:
2011-05-17
Component:
client-libs
OS:
windows_xp,windows
Sub-Component:
java.awt
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
5.0,7
Fixed Versions:
7

Related Reports
Backport:
Relates:

Sub Tasks

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 :(
                                     
2007-02-06
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) {
                                     
2007-02-06
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 :(
                                     
2007-01-31
EVALUATION

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



Hardware and Software, Engineered to Work Together