Optimizing Memory Leakage in Javafx

Optimizing memory leakage in JavaFX

There is a memory leak in JavaFX with Mesa >=11.0 (meaning any up to date Linux distribution). JavaFX developers say it's a bug in Mesa, but I couldn't find a bug report in Mesa (nor could I file one, as I don't know how to reproduce it outside of JavaFX).

The only solutions as of now are -

1. Use an older Linux (the key is having Mesa 10 or lower)

2. Use an NVidia GPU - they have their own OpenGL implementation and don't rely on Mesa.

3. Use Windows.

Update (November 2016)
This issue seems to have been resolved in newer versions of Mesa and/or X.org. Updating to Mesa 13.0 and X.org >=1.18.4 should solve this issue.

Related links:

  • openjfx-dev discussion thread.
  • reddit discussion.
  • Mesa discussion.
  • Related SO post: JavaXF 8 ProgressBar and ProgressIndicator with process=-1 memory leak on Linux

Javafx growing memory usage when drawing image

There are numerous bug reports about memory leaks in JavaFX on Linux.
For example JDK-8156051 or JDK-8161997.
To verify if you are hit by this bug try to run your program with -Dprism.order=sw and see if the bug persists.

General strategy to resolve Java memory leak?

If you are using Java from Sun and you use at least Java 6 update 10 (i.e. the newest), then try running jvisualvm from the JDK on the same machine as your program is running, and attach to it and enable profiling.

This is most likely the simplest way to get started.

JavaFX - Memory Consumption

You are creating a new Label and StackPane for every non-empty cell update, which may explain why the heap usage rises but may be garbage-collected.

You can try solving it by caching your node (Label in a StackPane in your case) - creating it only once:

final TableCell<Visit, Visit> cell = new TableCell<Visit, Visit>() {
private Label label;
private StackPane pane;
{
// This is the constructor of the anonymous class. Alternatively, you may choose to create the label and pane lazily the first time they're needed.
label = new Label();
pane = new StackPane(label);
pane.setAlignment(Pos.CENTER);
label.setMaxWidth(10);
label.setMinWidth(10);
label.setMinHeight(30);
}

@Override
public void updateItem(Visit item, boolean empty) {
super.updateItem(item, empty);
if (empty || getIndex() < 0) {
setGraphic(null);
setText(null);
return;
}
item = getTableView().getItems().get(getIndex());

setText(item.getStatus().display());

String background = FXMLConstants.toHexString(ColorUtils.getVisitBackgroundColor(item));
pane.setStyle(String.format("-fx-background-color:%s;;", background));
setGraphic(pane);
setText(item.getStatus().display());
setStyle(getStyle() + "-fx-alignment: CENTER_LEFT;");
}
};

Also - what is the use of the label, if you never set its text? What is the contentDisplay of the cell? Are the nodes even shown?



Related Topics



Leave a reply



Submit