-
Bug
-
Resolution: Fixed
-
P4
-
1.3.0
-
beta
-
generic
-
generic
ingrid.yao@Eng 2000-03-29
HP reported another AWT memory leak problem which is similar
to bug 4317277.
Attached is a test case for the leak in Choice.
When went through the code , we notice a leak in awt_Choice.c file in
function Java_sun_awt_motif_MChoicePeer_addItem:
180 if (IsMultiFont) {
181 mfstr = awtJNI_MakeMultiFontString(env, item, font);
182 >>>>> fontlist = awtJNI_GetFontList(env, font);
183 } else {
184 citem = (char *) JNU_GetStringPlatformChars(env, item, NULL);
185 }
186
In the above code fontlist is never getting freed. This causes a huge
memory leak. This fontlist should be freed before returning from the
fuction. A suggested fix is to free fontlist before returning from the function
addItem.
if ( fontlist != NULL )
{
XmFontListFree(fontlist);
}
The test case showed a lot of improvement when the above mentioned
fix is added to the code.
As this problem is there in jdk1.3 also , the fix needs to be added
to the release of both 1.2 and 1.3.
Similar leaks might exist in other widgets with Text.
Test case
============
import java.awt.*;
import java.awt.Event.*;
public class Buta_Choice extends Frame
{
protected Panel centerPanel = new Panel ();
protected GridLayout gridLayout = new GridLayout ( 6, 6, 5, 5 );
protected Choice btn[] = new Choice[36];
private static final int TOTAL_FRAMES = 2;
private Buta_Choice ()
{
super ();
centerPanel.setLayout ( gridLayout );
for ( int i = 0 ; i < btn.length ; i++ )
{
btn[i] = new Choice ();
btn[i].add("ABCD");
centerPanel.add ( btn[i] );
}
add ( centerPanel, BorderLayout.CENTER );
pack ();
setVisible ( true );
}
public void closeWindow ()
{
setVisible(false);
dispose ();
for ( int i = btn.length - 1 ; i >= 0 ; i-- )
{
gridLayout.removeLayoutComponent(btn[i]); // add by k
btn[i].removeAll();
btn[i] = null;
}
centerPanel.removeAll(); // add by k
centerPanel.removeNotify(); // add by k
centerPanel = null;
gridLayout = null;
btn = null;
try {
// remove by ki.
// reason: it isn't ensure to run this method at just this time.
// super.finalize ();
}
catch ( java.lang.Throwable ex ) {
System.out.println ( "2222222222222" );
}
try {
// remove by k.
// reason: it isn't ensure to run this method at just this time.
//finalize ();
}
catch ( java.lang.Throwable ex ) {
System.out.println ( "1111111111111" );
}
finally {
System.gc ();
}
}
/******* test 1 ************/
public static void main(String[] args)
{
System.out.println ( "Key In Start" );
// try {
// System.in.read ();
// }
// catch (Exception e) {
// }
int i, j;
Buta_Choice buta[];
buta = new Buta_Choice[TOTAL_FRAMES];
// for ( j = 0 ; j < 10 ; j++ )
j = 0;
while(true)
{
System.out.println ( "Key In NEW" );
for ( i = 0 ; i < buta.length ; i++ )
{
System.out.print ( "j = " + j + " " );
System.out.println ( "i = " + i );
buta[i] = new Buta_Choice();
}
System.out.println ( "Key In dispose" );
for ( i = 0 ; i < buta.length ; i++ )
{
System.out.print ( "j = " + j + " " );
System.out.println ( "i = " + i );
buta[i].closeWindow ();
buta[i] = null;
try
{
Thread.sleep(30);
}
catch(InterruptedException e)
{}
System.gc(); // add by k
}
System.gc ();
Runtime.getRuntime().runFinalization();
j++;
}
}
}