2016-02-26 82 views
0

此代碼導致NetBeans分析器無法看到的內存泄漏。 Windows上的泄漏並沒有那麼糟糕,並且似乎平穩,但是在運行時絕對會殺死Linux機器的內存。如果我註釋掉標籤上的setText方法,則內存不會泄漏。如果我打印到控制檯而不是將文本發送到標籤,則不會發生泄漏。這個Java程序爲什麼導致內存泄漏?

我認爲setText()方法由於某種原因持有舊值。

import javafx.application.Platform; 
import javafx.embed.swing.JFXPanel; 
import javafx.geometry.Orientation; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.layout.FlowPane; 
import javafx.stage.Stage; 

/** 
* 
* @author admin 
*/ 
public class Sandbox { 


    Label theLabel; 
    boolean isUpdating = false; 
    int count = 0; 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     new Sandbox(); 
    } 

    public Sandbox(){ 

     new JFXPanel(); // this will prepare JavaFX toolkit and environment 
     FlowPane root = new FlowPane(Orientation.VERTICAL); 

     Platform.runLater(new Runnable() { 
      @Override 
      public void run() { 
       Scene scene = new Scene(root, 600, 600); 
       Stage stage = new Stage(); 
       stage.setScene(scene); 
       stage.show(); 
      } 
     }); 

     theLabel = new Label(); 
     root.getChildren().add(theLabel); 

     while(true){ 
      try{ 
       count ++; 
       if(isUpdating == false){ 
        isUpdating = true; 
        Platform.runLater(new Runnable(){ 
         public void run(){ 
          theLabel.setText("TEST:" + count); //The culprit 
          isUpdating = false; 
         } 
        }); 
       } 
       Thread.sleep(0); 
      }catch(InterruptedException ex){ 

      } 
     } 
    } 
} 
+1

你確定它實際上是在泄漏內存,而不是僅僅使用一切可用的內存?除非必須,否則JVM通常不會清理垃圾。 – resueman

+0

無限循環不能好... – Reimeus

+0

@Reimeus:在生產中,這段代碼是使用執行程序服務和自行調用的runnable運行的。我試圖削減和簡化程序,使錯誤更容易找到。 –

回答

0

看來JavaFX在默認的Linux驅動程序(Noveau)中存在問題。在爲我的系統(NVidia Quatro K2200)安裝了正確的圖形驅動程序之後,內存泄漏消失了。