我有一个大问题,我使用了对象的arraylist,这个对象里面有一个计时器。 所以我使用说10作为计时器,所以arraylist的长度是10。
整个计时器几乎从内存中占用80 MB ..如果我使用我的程序一段时间,该程序将给我内存异常。
因为所有计时器从内存中占用太多,如果我从一帧移动到
另一帧,该程序占用相同的内存量。
我预计如果我尝试取消所有计时器或将对象等于空
记忆会减少,但不起作用。
如果问题不明确,请在评论中提问。
抱歉我的英语不好。注意:
这个程序很简单,但真正的程序很大
我的对象(边缘):
package operationresearch.pert.input;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.effect.Bloom;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
public class edge extends HBox {
private ArrayList<TextField> predecessor;
private Timer t2 ;
private TextField duration, activity;
public edge(boolean haveTime) {
predecessor = new ArrayList<>();
activity = new TextField();
activity.setPrefSize(80, 25);
activity.setPromptText("Activity");
if (haveTime) {
duration = new TextField();
duration.setPrefSize(85, 25);
duration.setPromptText("Duration");
}
Label depend = new Label("Depend on ");
depend.setEffect(new Bloom());
TextField init = new TextField();
init.setPrefSize(40, 20);
predecessor.add(init);
this.setSpacing(5);
if (haveTime) {
this.getChildren().addAll(activity, duration, depend, init);
} else {
this.getChildren().addAll(activity, depend, init);
}
setPadding(new Insets(0, 30, 0, 0));
predecessorControl();
}
private void predecessorControl() {
t2= new Timer();
TimerTask task2 = new TimerTask() {
@Override
public void run() {
TextField last = predecessor.get(predecessor.size() - 1);
last.setOnMouseClicked((MouseEvent e) -> {
if (last == predecessor.get(predecessor.size() - 1)) {
TextField newText = new TextField();
newText.setPrefSize(50, 20);
predecessor.add(newText);
getChildren().add(newText);
}
for (int i = 0; i < predecessor.size(); i++) {
System.out.println(predecessor.size());
if (predecessor.get(i).getText().equals("") && i != predecessor.size() - 2 && i != predecessor.size() - 1) {
getChildren().remove(predecessor.get(i));
predecessor.remove(predecessor.get(i));
}
}
});
}
};
t2.scheduleAtFixedRate(task2, 100, 100);
}
public ArrayList<TextField> getPredecessor() {
return predecessor;
}
public void setPredecessor(ArrayList<TextField> predecessor) {
this.predecessor = predecessor;
}
public TextField getDuration() {
return duration;
}
public void setDuration(TextField duration) {
this.duration = duration;
}
public TextField getActivity() {
return activity;
}
public void setActivity(TextField activity) {
this.activity = activity;
}
public Timer getT2() {
return t2;
}
public void setT2(Timer t2) {
this.t2 = t2;
}
}
主要:
package javaapplication2;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TestApp3 extends Application {
private ArrayList<edge> graph;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
VBox root = new VBox();
graph = new ArrayList();
for (int i = 0; i < 10; i++) {
edge edge = new edge(true);
graph.add(edge);
root.getChildren().add(edge);
}
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setWidth(500);
stage.setHeight(500);
stage.show();
}
}