import java.applet.*; import java.awt.*; import java.awt.event.*; public class leak2 extends Applet { public void init() { setBackground(new Color(225,225, 255)); try { FrameForApplet frameForApplet = new FrameForApplet("A frame"); } catch (Throwable e) { System.out.println("Can't use Frame: " + e); return; } } public void paint(Graphics g) { g.drawString("A Frame appears using Java version " + System.getProperty("java.version"), 10 , 25); } } // END OF Class leak2 final class FrameForApplet extends Frame { ButtonPanel buttonPanel; int panelNow; static final int RED = 0; static final int GREEN = 1; FrameForApplet(String s) { super(s); setLayout(new GridBagLayout()); addNotify(); setBounds(400, 100, 400, 400); changeToPage(GREEN); show(); } final void changeToPage(int newPanel) { if (buttonPanel != null) { buttonPanel = null; removeAll(); } Runtime.getRuntime().gc(); System.out.println("total = " + Runtime.getRuntime().totalMemory() + "\t free + " + Runtime.getRuntime().freeMemory()); if (newPanel == GREEN) { buttonPanel = new ButtonPanel(GREEN, this); add(buttonPanel); } else if (newPanel == RED) { buttonPanel = new ButtonPanel(RED, this); add(buttonPanel); } panelNow = newPanel; validate(); } } // END OF Class FrameForApplet class ButtonPanel extends Panel implements ActionListener { static int buttons = 100; int now; Button[] button; FrameForApplet container; ButtonPanel(int now, FrameForApplet container) { this.container = container; this.now = now; Color buttonColor = (now == 1)? Color.green : Color.red; setLayout(new GridLayout(buttons/10, 10, 4, 4)); button = new Button[buttons]; for (int i=0; i