Search code examples
javaswingjframeout-of-memoryjbutton

Out of memory error while opening a JFrame from button in Same Package?


My program was working perfectly until I used a switch statement in JFrame of 2nd class to be opened from 1st class in same package. I got out of memory error.. related to heap space. I removed that switch statement still same error occured. when there is no linkin between jframes they are running perfectly. but when I link them, 1st frame works and on clicking submit button, it dissappears, probably because in its action I've used this.dispose but it isn't opening 2nd frame and after 5-10 minutes I get out of memory error

Class1(JFrame1)

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            qualification = (String)degree.getSelectedItem( );
    contact_num= Integer.parseInt(num.getText());
    String xyz= (String)d.getSelectedItem();
    date=Integer.parseInt(xyz);
    xyz= (String)m.getSelectedItem();
    month=Integer.parseInt(xyz);
    year=Integer.parseInt(y.getText());
    street= (String)(strt.getText());
    name=(String)nme.getText();
    email= (String)e_mail.getText();
    state= (String)stt.getText();
    city= (String)cty.getText();
    gender= (String)sex.getSelectedItem();
    Sample a=new Sample();
    a.setVisible(true);
       this.dispose();
}

Class2(JFrame2)

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    System.out.print("ab");

}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    System.out.print("b");

}                                        

Code for Class2 with switch statement due to which I got that error

public Qualification abc=new Qualification();
    Sample aa= new Sample();
    public String s;
    public void open_sample()
    {
        this.setVisible(true);
        switch(abc.qualification)
        {
            case "BE":
            BE1 a=new BE1(); 
            break;
            case "10th/12th":
            C101 b=new C101();
            b.setVisible(true);
            break;
            case "MBA":
            MBA1 c=new MBA1();
            c.setVisible(true);
            break;
            case "CA":
            CA1 x=new CA1();
            x.setVisible(true);
            break;
            default:

        }
    }

private void sa1ActionPerformed(java.awt.event.ActionEvent evt) {                                   s= evt.getActionCommand();
         open_sample();
 }                                   

    private void sa4ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        s= evt.getActionCommand();
   open_sample();}                                   

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
                      System.out.print("hello");
  }                                        

    private void sa3ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        s= evt.getActionCommand();
       open_sample();
    }                                   

    private void sa2ActionPerformed(java.awt.event.ActionEvent evt) {                                    
           s = evt.getActionCommand();
      open_sample();
    } 

Solution

  • My first thought was:

    final Sample a = new Sample();
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            a.setVisible(true);
        }
    });
    this.dispose();
    

    This is in general a good practise, fast handling an event on the event queue handling thread, and doing the real work a bit later.

    However one error seems that the code is using fields instead of local variables, and seem to be used inside the Sample constructor. So Sample is an embedded class? Then a this.dispose() will not free the class itself, as long as Sample is not freed it maintains a JFrame1.this.

    Not sure whether this helps, but a bit of code rewriting seems appropiate.

    Also try

    setDefaultCloseOperation(DISPOSE_ON_CLOSE); // Init
    ...
    setVisible(false); // Instead of dispose()