JDK-8227619 : Potential memory leak in javafx.scene.control.ListView
  • Type: Bug
  • Component: javafx
  • Sub-Component: controls
  • Affected Version: openjfx11,openjfx12,openjfx13
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_10
  • CPU: x86_64
  • Submitted: 2019-07-11
  • Updated: 2020-07-21
  • Resolved: 2020-02-11
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
openjfx15Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
Windows 10 64-bit

A DESCRIPTION OF THE PROBLEM :
Changing the ListView's data model using setItems prevents ListView from being garbage collected because SelectedItemsReadOnlyObservableList adds a non-weak listener to the data model (ObservableList)

REGRESSION : Last worked in version 8u192

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
- create a ListView
- change its data model by calling setItems( ObservableList<T> ) 
- clear all references to the ListView but keep the data model
- ListView cannot be garbage collected. This worked in earlier releases (JavaFX 8)


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
In earlier releases the ListView could be garbage collected even when the data model was kept
ACTUAL -
see UnitTest: test succeeded with JavaFX 8, but fails with JavaFX 11.0.2 and 12.0.1

---------- BEGIN SOURCE ----------
package listview;

import static org.junit.Assert.fail;

import java.lang.ref.WeakReference;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.ListView;
import javafx.stage.Stage;

public class ListViewTest {
	static CountDownLatch startupLatch;

	private WeakReference<ListView<?>> listViewRef;

	public static class TestApp extends Application {

		@Override
		public void start(Stage stage) throws Exception {
			startupLatch.countDown();
		}
	}

	private static ObservableList<String> items = FXCollections.observableArrayList();

	public ListViewTest() {

	}

	@BeforeClass
	public static void initJavaFX() {
		startupLatch = new CountDownLatch(1);
		new Thread(() -> Application.launch(TestApp.class, (String[]) null)).start();
		try {
			if (!startupLatch.await(15, TimeUnit.SECONDS)) {
				fail("Timeout waiting for FX runtime to start");
			}
		} catch (InterruptedException ex) {
			fail("Unexpected exception: " + ex);
		}
	}

	@Before
	public void initListView() throws Exception {
		CountDownLatch latch = new CountDownLatch(1);

		Platform.runLater(() -> {
			listViewRef = new WeakReference<>(new ListView<>(items));

			latch.countDown();

		});

		if (!latch.await(15, TimeUnit.SECONDS)) {
			fail("Timeout waiting for FX listview initialization");
		}

	}
	@Test
	public void testGarbageCollect() {
		for (int i = 0; i < 10; i++) {
			System.gc();
			System.runFinalization();

			if (listViewRef.get() == null) {
				break;
			}

			try {
				Thread.sleep(500);
			} catch (Exception ex) {

			}
		}
		Assert.assertTrue("Could not garbage collect listview", listViewRef.get() == null);
	}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
clear data model by calling setItems(null)  to dispose ListView

FREQUENCY : always



Comments
Submitter has confirmed that the fix has resolved this issue - "I am not seeing the problem with 15 ea. (I also had to download openjfx 15 ea from here to test it: https://gluonhq.com/products/javafx/). It seems resolved."
21-07-2020

Changeset: e9864593 Author: Ambarish Rapte <arapte@openjdk.org> Date: 2020-02-11 10:17:09 +0000 URL: https://git.openjdk.java.net/jfx/commit/e9864593
11-02-2020

Regression of JDK-8154216
10-01-2020

Issue is reproducible with latest build of Openjfx13 on Windows7.
17-07-2019