import java.awt.*;
import java.awt.event.*;
public class testegc extends Frame implements ActionListener
{
	Button alocar = new Button ("Alocar objetos");
	Button view = new Button("Atualizar dados da memoria");
	Button clean = new Button ("Executar o Garbage Collector");
	Button fechar = new Button("Fechar");
	Button lista[] = new Button[3000];
	TextField minitial = new TextField(6);
	TextField mcurrent = new TextField(6);
	TextField mlast	=  new TextField(6);
	TextField diferenca = new TextField(6);
	TextArea t = new TextArea();


	public testegc()
	{
		super("Testando o Garbage Collector");
		minitial.setText(""+Runtime.getRuntime().freeMemory());
		mlast.setText(""+Runtime.getRuntime().freeMemory());
		this.setLayout(new GridLayout(6,2));
		this.add(new Label("Memoria livre inicial:"));
		this.add(minitial);
		this.add(new Label("Memoria livre atual:"));
		this.add(mcurrent);
		this.add(new Label("Ultimo dado da memoria livre:"));
		this.add(mlast);
		this.add(new Label("Diferenca entre inicial e atual"));
		this.add(diferenca);
		this.add(alocar);
		this.add(view);
		this.add(clean);
		this.add(fechar);

		this.setSize(400,190);
		minitial.setEditable(false);
		mcurrent.setEditable(false);
		mlast.setEditable(false);
		diferenca.setEditable(false);

		alocar.addActionListener(this);
		view.addActionListener(this);
		clean.addActionListener(this);
		fechar.addActionListener(this);
		t = null;
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == alocar)
			alocar();
		else if(e.getSource() == view)
			view();
		else if(e.getSource() == clean)
			clean();
		else if(e.getSource() == fechar)
			System.exit(0);
	}
	public void view()
	{
		mlast.setText(mcurrent.getText());
		mcurrent.setText(""+ Runtime.getRuntime().freeMemory());
		long dif = Runtime.getRuntime().freeMemory() - Long.valueOf(minitial.getText()).longValue();
		diferenca.setText(""+ dif);
	}

	public void alocar()
	{
		for(int i=0; i< 3000; i++)
		{
			lista[i] = new Button("ok");
		}
		for(int i=0; i< 3000; i++)
		{
			lista[i] = null;
		}

		view();
	}

	public void clean()
	{

		System.gc();
		view();
	}

	public static void main(String a[])
	{
		testegc teste = new testegc();
		teste.show();
	}

}