Microsoft KB Archive/244201

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

Article ID: 244201

Article Last Modified on 6/14/2006

-

APPLIES TO

 Microsoft Java Virtual Machine, when used with:  Microsoft Windows NT 4.0

 Microsoft Windows 95

 Microsoft Windows 98 Standard Edition 

-

<div class="notice_section">

This article was previously published under Q244201

<div class="symptoms_section">

SYMPTOMS
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.

<div class="cause_section">

CAUSE
The device context is not being deleted in Graphics.dispose.

<div class="resolution_section">

RESOLUTION
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.

<div class="status_section">

STATUS
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.

<div class="moreinformation_section">

Steps to Reproduce Behavior
You can follow this procedure to reproduce the problem behavior: <ol>  On a Windows 98-based computer with Microsoft VM version 3186 or earlier, create a file GDILeakTest.java 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; test.show; } }

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(Color.black); 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); } }

</li> Use the following command to compile GDILeakTest.java:

jvc GDILeakTest.java

</li> Run the generated GDILeakTest.class using:

jview GDILeakTest.class

This will cause a frame dialog box to appear with a Print button.</li> Open the Resource Meter tool in Windows 98 (from the Start menu, point to Programs, point to Accessories, point to System Tools, 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.</li></ol>

<div class="references_section">