java – 每次更新tableview数据时如何修复内存增加
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 每次更新tableview数据时如何修复内存增加,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3829字,纯文字阅读大概需要6分钟。
内容图文
随着时间的推移,我有一个javafx应用程序内存不断增加.这让我很困惑,因为我认为我的代码中有内存泄漏.
我发现每次更新tableview数据时内存都会增加,没有任何新对象只是更新旧数据.
以下代码的目的是重现相同的问题
public class TableViewSample extends Application {
private boolean running = false;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
TableView<Foo> table = new TableView<>();
ObservableList<Foo> data = FXCollections.observableArrayList(
new Foo(0, 0, 0, 0), new Foo(0, 0, 0, 0), new Foo(0, 0, 0, 0),
new Foo(0, 0, 0, 0), new Foo(0, 0, 0, 0), new Foo(0, 0, 0, 0),
new Foo(0, 0, 0, 0), new Foo(0, 0, 0, 0), new Foo(0, 0, 0, 0),
new Foo(0, 0, 0, 0), new Foo(0, 0, 0, 0), new Foo(0, 0, 0, 0),
new Foo(0, 0, 0, 0), new Foo(0, 0, 0, 0), new Foo(0, 0, 0, 0),
new Foo(0, 0, 0, 0), new Foo(0, 0, 0, 0), new Foo(0, 0, 0, 0),
new Foo(0, 0, 0, 0), new Foo(0, 0, 0, 0), new Foo(0, 0, 0, 0),
new Foo(0, 0, 0, 0), new Foo(0, 0, 0, 0), new Foo(0, 0, 0, 0),
new Foo(0, 0, 0, 0), new Foo(0, 0, 0, 0), new Foo(0, 0, 0, 0)
);
Random random = new Random();
TableColumn<Foo, Integer> num1Col = new TableColumn<>("num1");
num1Col.setCellValueFactory(new PropertyValueFactory<>("num1"));
TableColumn<Foo, Integer> num2Col = new TableColumn<>("num2");
num2Col.setCellValueFactory(new PropertyValueFactory<>("num2"));
TableColumn<Foo, Integer> num3Col = new TableColumn<>("num3");
num3Col.setCellValueFactory(new PropertyValueFactory<>("num3"));
TableColumn<Foo, Integer> num4Col = new TableColumn<>("num4");
num4Col.setCellValueFactory(new PropertyValueFactory<>("num4"));
table.setItems(data);
table.getColumns().addAll(num1Col, num2Col, num3Col, num4Col);
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), event -> {
for (Foo foo : data) {
foo.setNum1(random.nextInt(1000));
foo.setNum2(random.nextInt(1000));
foo.setNum3(random.nextInt(1000));
foo.setNum4(random.nextInt(1000));
}
}));
timeline.setCycleCount(Timeline.INDEFINITE);
Button btn = new Button("start");
btn.setOnAction(event -> {
if (running) {
timeline.stop();
running = false;
btn.setText("start");
} else {
timeline.play();
running = true;
btn.setText("stop");
}
event.consume();
});
VBox vbox = new VBox();
vbox.getChildren().addAll(btn, table);
((Group) scene.getRoot()).getChildren().add(vbox);
stage.setScene(scene);
stage.show();
}
public static class Foo {
private SimpleIntegerProperty num1;
private SimpleIntegerProperty num2;
private SimpleIntegerProperty num3;
private SimpleIntegerProperty num4;
public Foo(int num1, int num2, int num3, int num4) {
this.num1 = new SimpleIntegerProperty(num1);
this.num2 = new SimpleIntegerProperty(num2);
this.num3 = new SimpleIntegerProperty(num3);
this.num4 = new SimpleIntegerProperty(num4);
}
public int getNum1() {
return num1.get();
}
public SimpleIntegerProperty num1Property() {
return num1;
}
public void setNum1(int num1) {
this.num1.set(num1);
}
public int getNum2() {
return num2.get();
}
public SimpleIntegerProperty num2Property() {
return num2;
}
public void setNum2(int num2) {
this.num2.set(num2);
}
public int getNum3() {
return num3.get();
}
public SimpleIntegerProperty num3Property() {
return num3;
}
public void setNum3(int num3) {
this.num3.set(num3);
}
public int getNum4() {
return num4.get();
}
public SimpleIntegerProperty num4Property() {
return num4;
}
public void setNum4(int num4) {
this.num4.set(num4);
}
}
}
我做错了什么以及如何解决这种记忆增加的问题?
解决方法:
在相同的jdk版本的Windows上品尝相同的代码,一切都很好.
我使用不同的jdk版本在ubuntu上构建了两次可执行jar,并在windows和ubuntu上测试了它,并在两个平台上产生相同的问题.所以无论使用什么jdk版本,ubuntu jdk都会产生同样的问题,我现在将继续在Windows上编码,稍后会尝试新的ubuntu安装.谢谢你的帮助
编辑:
经过一番搜索发现一些相关的bug报告JDK-8161997,JDK-8161911似乎与JavaFX有关的问题是使用硬件管道.
在一个新的ubuntu 18.04安装后,一切正常.
内容总结
以上是互联网集市为您收集整理的java – 每次更新tableview数据时如何修复内存增加全部内容,希望文章能够帮你解决java – 每次更新tableview数据时如何修复内存增加所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。