JDK-4037927 : Graphics.setFont() causes virtual memory leaks
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.1
  • Priority: P2
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_2.5.1
  • CPU: sparc
  • Submitted: 1997-03-11
  • Updated: 1997-11-11
  • Resolved: 1997-11-11
Related Reports
Duplicate :  
Description

Name: mc57594			Date: 03/10/97


Here is code to reproduce the problem I have discovered.  More
information follows the code, so read on.

----- START CODE -----

import java.lang.*;
import java.awt.*;

class TestFont extends Canvas
{
  public void paint(Graphics g)
  {
    Font font;

    // Set up the new font.
    font = new Font("Helvetica", Font.PLAIN, 12);
    g.setFont(font);

    // Draw something.
    g.drawString("Font Test Program!!!", 50, 50);
  }

  // Return the preferred size for this component.
  public Dimension getPreferredSize()
  {
    return getMinimumSize();
  }

  // Return the minimum size for this component.
  public Dimension getMinimumSize()
  {
    return new Dimension(300, 100);
  }

  public static void main(String args[])
  {
    Frame f;
    TestFont tf;

    f = new Frame("Font Test Program");
    tf = new TestFont();

    f.add(tf);
    f.pack();
    f.show();

    while (true)
    {
      try {
	Thread.sleep(100);
      } catch (InterruptedException ie) {}

      tf.repaint();
    }
  }
}

----- END CODE -----

If this program is compiled under Java 1.1 (final) and run, it
will use up virtual memory resources.  To see this, I also run
the command 'vmstat 1' in another window.  It becomes obvious
that virtual memory is definitely decreasing while the program is
running, and that it decreases at a rate related to the frequency
that the canvas is repainted.  When virtual memory hits 0, the
machine will crash.  How badly the machine crashes depends; I've
had machines become unusable and have to be power-cycled, or
sometimes the windowing system just goes down, or sometimes the
program just seg-faults and core dumps.  It depends on the setup
of the system, it seems.

Note that if the call to Graphics.setFont() is not called, no
leaking occurs.  This indicates to me that Graphics.setFont()
keeps around unneccessary references to the passed-in Font
objects, but I don't have any solid idea of how it all works, of
course.  :)
company - Caltech Seismology Lab , email - ###@###.###
======================================================================

Comments
WORK AROUND Name: mc57594 Date: 03/10/97 The most obvious work-around is to only create the Font object once and call Graphics.setFont() only once. However, I have components in my program that must create fonts according to the importance of the text they are displaying, and I can't have them crashing my program. So this isn't really a viable workaround. ======================================================================
11-06-2004

EVALUATION hugh.meyers@Canada 1997-11-11 Bug 4068618 has a much better description of the bug
11-11-1997