FIX: Printing from Java Results in Depletion of GDI and System Resources


If you use PrintJob (through getToolkit().getPrintJob) for printing multiple times through Java, this results in depletion of Graphics Device

Interface (GDI) resources. If the applet that is doing the printing is hosted in a browser, it will eventually cause the browser to stop responding.


The device context is not being deleted in Graphics.dispose().


To resolve this problem, update your version of the Microsoft virtual machine (Microsoft VM) to build 3188 (or later). See the "References" section of this

article for a link to download the latest Microsoft VM.


Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

This problem was corrected in Microsoft VM build 3188 and later.

This problem was corrected in Windows 2000 Service Pack 1.


Steps to Reproduce Behavior

You can follow this procedure to reproduce the problem behavior:
  1. On a Windows 98-based computer with Microsoft VM version 3186 or earlier, create a file with the following code:
    import java.awt.*;import java.awt.event.*;public class GDILeakTest extends Frame {    PrintCanvas canvas;    public GDILeakTest() {        super("GDI leak Test");        canvas = new PrintCanvas();        add("Center", canvas);        Button b = new Button("Print");        b.setActionCommand("print");b.addActionListener( new ActionListener(){public void actionPerformed(ActionEvent e){buttonActionHandler(e);}});        this.add("South", b);this.addWindowListener( new WindowAdapter(){public void windowClosing(WindowEvent e){close();}});        this.pack();    }    public void close()    {System.exit(0);    }    private void buttonActionHandler(ActionEvent e){String cmd = e.getActionCommand();if (cmd.equals("print")) {    PrintJob pjob = getToolkit().getPrintJob(this,"GDI Leak Test", null);    if (pjob != null) {Graphics pg = pjob.getGraphics();if (pg != null) {   canvas.printAll(pg);   pg.dispose();}pjob.end();    }}    }    public static void main(String args[]) {        GDILeakTest test = new GDILeakTest();;    }}class PrintCanvas extends Canvas {    public Dimension getPreferredSize() {        return new Dimension(400, 200);    }    public void paint(Graphics g) {        Font font = new Font("Courier", Font.BOLD, 11);        g.setFont(font);        g.setColor(;        g.drawString("This is a test page", 10, 50);        g.drawString("Print this page number of times", 10, 70);        g.drawString("Watch the resource meter", 10, 90);    }}
  2. Use the following command to compile
  3. Run the generated GDILeakTest.class using:

    jview GDILeakTest.class
    This will cause a frame dialog box to appear with a Print button.
  4. Open the Resource Meter tool in Windows 98 (from the Start menu, point to Programs, point to Accessories, point to System

    , and click Resource Meter). Click the Print button to print a test page, and each print will show a decrease in the GDI and system

    resources. After a number of repeated prints, the GDI resources will be exhausted and the system will become unresponsive or come to a halt.


The latest Microsoft VM is available for download at the following Web site:

For additional information about the latest service pack for Windows 2000, click the article number below
to view the article in the Microsoft Knowledge Base:

260910 How to Obtain the Latest Windows 2000 Service Pack
For support information about Visual J++ and the SDK for Java, visit the following Microsoft Web site:


ID članka: 244201 - posljednja izmjena: 14. velj 2017. - verzija: 1

Povratne informacije