Sun Java BUG: GDI object leak

Discussion in 'Java' started by Howard Rubin - change nospam to nyx, May 19, 2004.

  1. Hi,
    I'm working on an applet that has a GDI object leaking issue; in this
    applet, a second thread draws something (using java.awt.Graphics's
    methods ) on the main Panel dynamically. But when running under Sun JVM,
    every time the drawing is called, one more GDI object is created and
    memory is allocated for the GDI object, they are not released when the
    thread exits and all its objects are dereferenced. So after a while,
    the Windows GUI will be very slow and erratic, for there are so many
    GDI objects.

    The problem does not happen on Microsoft JVM.

    To see the problem, you can use the following simplified applet code,
    start the applet in a browser or appletviewer that uses Sun JVM on Windows.
    Open Windows Task Manager, turn on the "GDI objects" column in the
    "processes" tab. You can see the browser's process's memory and GDI
    object count keep increasing. And after some minutes, when there are
    too many GDI objects in the OS, the WIndows GUI becomes very slow and
    erratic.

    I tried cleaning up the resource using System.gc(),
    System.runFinalization(), nullifying the Graphics object, forcing
    Graphics.dispose() method(); and I tried different ways to implement
    the thread ( using Runnable ), start the thread at different places,
    and different ways to stop the thread; also I tried letting the main
    thread create the Graphics object then pass it down to the second
    thread, and using Toolkit.sync() method.
    None of these worked.

    Any one have any idea how to fix or workaround this problem?

    Source code is below, I will post the compiled classes later.

    thanks

    ---------- BEGIN SOURCE ----------
    //Run this applet using SUN JVM under Windows, and open //the Task manager with
    "GDI objects" column on.
    //You will see the GDI object leak.
    //And after some minutes the GUI of Windows will become slow and erratic.
    //It's the Graphics method in the secondary thread causing the problem.
    //Microsoft JVM does not have this problem.

    import java.applet.*;
    import java.awt.event.*;
    import java.awt.*;

    public class Table extends Applet implements Runnable{
    public void init() {}

    public void start() {
    Thread t = new Thread( this );
    t.start();
    }

    public void run() {
    while(true) {
    Graphics gr = null;
    try {
    gr = getGraphics();
    // This one does NOT cause the leak.
    // And removing this one does not help.
    gr.drawLine(0, 10, 10, 0 );
    } finally { gr.dispose(); }
    try { Thread.sleep(20); } catch (InterruptedException e) { }

    Flash f = new Flash();
    f.t = this;
    f.start();
    try { Thread.sleep(30); } catch (InterruptedException e) { }
    }
    }

    private class Flash extends Thread{
    public Table t;
    public synchronized void run(){
    Graphics gr = null;
    try {
    gr = t.getGraphics();
    gr.drawLine(0,0,10,10); // This one DOES cause the leak.
    } finally { gr.dispose(); }
    gr = null;
    }
    };
    }

    ---------- END SOURCE ----------
    Howard Rubin - change nospam to nyx, May 19, 2004
    #1
    1. Advertising

  2. Howard Rubin - change nospam to nyx

    Filip Larsen Guest

    Howard Rubin wrote

    > I'm working on an applet that has a GDI object leaking issue; in this
    > applet, a second thread draws something (using java.awt.Graphics's
    > methods ) on the main Panel dynamically. But when running under Sun

    JVM,
    > every time the drawing is called, one more GDI object is created and
    > memory is allocated for the GDI object, they are not released when the
    > thread exits and all its objects are dereferenced.


    I don't know what GDI is, but I do know that in general you can get
    trouble if another thread than the event dispatcher thread is used to
    invoke the AWT API.


    Regards,
    --
    Filip Larsen
    Filip Larsen, May 20, 2004
    #2
    1. Advertising

  3. Howard Rubin - change nospam to nyx

    Skippy Guest

    > Graphics gr = null;
    > try {
    > gr = getGraphics();
    > // This one does NOT cause the leak.


    > Graphics gr = null;
    > try {
    > gr = t.getGraphics();
    > gr.drawLine(0,0,10,10); // This one DOES cause the leak.


    Why the use of getGraphics() ? This is often poor design. Override
    paint(Graphics g) to get your instance of the Graphics-object and use
    repaint() to let the GUI properly repaint.
    Skippy, Jun 6, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cGJ1bQ==?=

    GDI+ - limits for pixel dimensions in a bitmap object

    =?Utf-8?B?cGJ1bQ==?=, Jun 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    346
    =?Utf-8?B?cGJ1bQ==?=
    Jun 18, 2004
  2. tgarmanzo

    Sun Java Plug-in memory leak

    tgarmanzo, Mar 1, 2004, in forum: Java
    Replies:
    4
    Views:
    3,021
    Vladimir Kondratyev
    Mar 6, 2004
  3. Ulf Meinhardt
    Replies:
    0
    Views:
    401
    Ulf Meinhardt
    Nov 11, 2004
  4. Replies:
    0
    Views:
    2,847
  5. Robin van de Water [synthesiSFactory]

    GDI+ is not properly initialized (internal GDI+ error).

    Robin van de Water [synthesiSFactory], Jan 10, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    1,325
    Robin van de Water [synthesiSFactory]
    Jan 10, 2007
Loading...

Share This Page