Name: yyT116575 Date: 04/19/2001
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
When attempting to load a URL into the JEditorPane using the setPage() method,
memory leaks at a rate relative to the size of the information contained at the
URL being viewed.
Note that the intended application would need to reload information from the
same URL on a continuous, extended basis, without caching the page. Hence the
use of the "NonCachingEditorPane" subclass to defeat caching of the document.
The following code will leak memory at a rate of about 1mb/sec when provided
with a suitable html document (one with say- 1000 lines of text). Adjusting
the rate of reloading will of course lessen the impact of the loss, although
it does not provide a solution in an extended use scenerio.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.io.*;
import java.net.*;
public class MemoryLeaker extends JPanel {
NonCachingEditorPane jt = new NonCachingEditorPane();
JScrollPane pane = new JScrollPane();
JTextField input = new JTextField("file:C:\\test.html");
URL url;
private javax.swing.Timer refresher = new javax.swing.Timer( 500,
new ActionListener(){
String charSetSpec = "text/html";
public void actionPerformed( ActionEvent a ) {
try {
jt.setPage( url );
} catch( IOException ex ) {
ex.printStackTrace();
JOptionPane.showMessageDialog (MemoryLeaker.this,
"Couldn't read " + input.getText(), "Invalid Input",
JOptionPane.ERROR_MESSAGE);
refresher.stop();
}
}
}
);
public MemoryLeaker() {
setLayout (new BorderLayout (5, 5));
pane.setBorder ( BorderFactory.createLoweredBevelBorder());
pane.getViewport().add(jt);
add(pane, BorderLayout.CENTER);
add (input, BorderLayout.SOUTH);
input.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
try {
url = new URL( input.getText() );
jt.setPage( url );
} catch ( Exception mfurl ){ mfurl.printStackTrace();}
refresher.start();
}
});
}
public static void main( String[] args ) {
JFrame frame = new JFrame( "MemoryLeaker" );
frame.getContentPane().setLayout( new BorderLayout() );
frame.getContentPane().add( new MemoryLeaker(), BorderLayout.CENTER );
frame.setSize( 800, 600 );
frame.setVisible( true );
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit( 0 );
}
});
}
class NonCachingEditorPane extends JEditorPane {
public URL getPage() {
return null;
}
}
}
Procedure to reproduce the memory leak:
1. Take the attached file (test.html), put it on your drive where you can
find it.( or another suitable file if you like).
2. Compile the code in MemoryLeaker.java
3. java MemoryLeaker
4. Enter the name of the file you would like to load as a URL in the
textfield at the bottom of the application.
5. Press enter.
6. Monitor your memory usage via your O.S., or wait for a
java.lang.OutOfMemoryError.
I have produced this problem on a PC. My boss, on a SunBlade,
running Solaris, with a similar application that he wrote without
seeing my code, reproduced the leak as well.
(Review ID: 120936)
======================================================================