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
This article was previously published under Q244201
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.
CAUSE
The device context is not being deleted in Graphics.dispose().
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.
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.
MORE INFORMATION
Steps to Reproduce Behavior
You can follow this procedure to reproduce the problem behavior:
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); } }
- Use the following command to compile GDILeakTest.java:
jvc GDILeakTest.java
- Run the generated GDILeakTest.class using:
jview GDILeakTest.class
- 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.
REFERENCES
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:
Keywords: kbbug kbfix kbjavavm33xxfix kbqfe kbjavavm31xxfix kbfaq KB244201