Finalize方法导致内存泄漏?

时间:2012-11-16 20:51:40

标签: memory-leaks javafx-2

我无法解决问题,需要你的帮助。当我点击菜单时,我打电话给客户帐户,然后关闭它。每次我打电话给客户账户,内存都会增加。当我关闭帐户时它应该会减少,但它不会发生。

课程菜单

mnItemCL_Cust.setOnAction(new EventHandler<ActionEvent>() {
  @Override
  public void handle(ActionEvent t) {
    try {
      panCenterPrev = (Pane) root.getCenter();
      panCenterAct = Customer.listCustomer();
      root.setCenter(null);
      root.setCenter(panCenterAct);
      Customer.btCanc.setOnAction(new EventHandler<ActionEvent>() {
        @Override public void handle(ActionEvent e) {
          try {
            Customer.Fim();
            panCenterAct.getChildren().clear();
            panCenterAct = null;
            root.setCenter(null);
            root.setCenter(panCenterPrev);
          } catch (Throwable ex) {
            Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
          }
        }             
});

班级客户

public class Customer
{

  public static Pane listCustomer() throws SQLException, ClassNotFoundException
  {
    ...
    final ObservableList<MyCustomer> data = FXCollections.observableArrayList();
    ...
  }

  public static class MyCustomer {
    private final SimpleIntegerProperty idcl;
    private MyCustomer(Integer pIdcl ) {
      this.idcl = new SimpleIntegerProperty(pIdcl);  
    }

    public Integer getIdcl() {
         return idcl.get();
    }

    public void setIdcl(Integer pIdcl) {
        idcl.set(pIdcl);
    }
  }  

  public static void Fim() throws Throwable {
    ...
    rs = null;

    tbViewCL.getItems().clear();
    tbViewCL = null;
    colIDCL.getColumns().clear();
    colIDCL = null;
  }

  ...

  protected void finalize() throws Throwable {
    try{
      ...
      rs.close();
      ...// Never happens... why??  
    } catch(Throwable t) {
        throw t;
    } finally {
        JOptionPane.showMessageDialog(null,"End?");
        super.finalize();
    }
  }

此致

1 个答案:

答案 0 :(得分:0)

Java通常会回收您在看到它适合时使用的内存,因此即使您最终确定了该对象,内存仍可能存在。但是,如果rs.Close()永远不会执行,可能是因为之前发生的事情是抛出异常,我建议你先检查代码,以确保没有任何事情这样做,同样,如果你发现异常是好的练习记录它,这样你就可以知道发生了什么。