import com.sun.javafx.perf.PerformanceTracker; import java.util.ArrayList; import java.util.List; import javafx.animation.AnimationTimer; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.collections.*; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.TreeItem; import javafx.scene.control.TreeView; import javafx.stage.Stage; import javafx.util.Callback; import javafx.util.Duration; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author ekaterina.pavlova@oracle.com */ public class TreeViewMemTest extends Application { enum TestMode {STATIC, DYNAMIC }; static TestMode testMode = TestMode.STATIC; static double SCENE_WEIGHT = 400.0; static double SCENE_HEIGHT = 400.0; static int ITEMS = 30; static TreeItem staticTreeRoot; static void initStaticTreeViewItems() { staticTreeRoot = new TreeItem("Static root"); staticTreeRoot.setExpanded(true); for (int i=0; i obj = new TreeItem("static item " + i); staticTreeRoot.getChildren().add(obj); } } TreeItem createTreeViewItems(int iter) { TreeItem treeRoot = new TreeItem("Dynamic root " + iters); treeRoot.setExpanded(true); for (int i=0; i obj = new TreeItem("dynamic item " + i); treeRoot.getChildren().add(obj); } return treeRoot; } Stage stage; PerformanceTracker perfTracker; Group rootGroup; int iters=1; @Override public void start(Stage primaryStage) { stage = new Stage(); rootGroup = new Group(); Scene scene = new Scene(rootGroup, SCENE_WEIGHT, SCENE_HEIGHT); stage.setScene(scene); stage.sizeToScene(); stage.show(); perfTracker = PerformanceTracker.getSceneTracker(scene); Timeline fpsTimeline = new Timeline(); fpsTimeline.setCycleCount(Timeline.INDEFINITE); KeyFrame fpsKeyFrame = new KeyFrame(new Duration(2000), new EventHandler() { @Override public void handle(ActionEvent e) { String fpsText = String.format("FPS: %.2f", perfTracker.getInstantFPS()); System.out.println(fpsText); } }); fpsTimeline.getKeyFrames().add(fpsKeyFrame); initStaticTreeViewItems(); AnimationTimer objectsAllocator = new AnimationTimer() { boolean stop = false; @Override public void handle(long l) { stage.setTitle("TreeView " + iters); TreeItem treeRoot = staticTreeRoot; if (testMode == TestMode.DYNAMIC) { treeRoot = createTreeViewItems(iters); } TreeView treeView = new TreeView(); treeView.setShowRoot(true); treeView.setRoot(treeRoot); treeView.setMaxSize(150, 200); treeView.setLayoutX(iters % (SCENE_WEIGHT - 150)); rootGroup.getChildren().clear(); rootGroup.getChildren().add(0, treeView); //System.out.println("Created " + iters + "th TreeView"); //System.out.println("rootGrp size: " + rootGroup.getChildren().size()); // System.gc(); try { Thread.sleep(100); } catch (Exception e) {} // System.gc(); try { Thread.sleep(100); } catch (Exception e) {} iters++; } }; fpsTimeline.play(); objectsAllocator.start(); } public static void main(String[] args) { for (int index = 0; index < args.length; index++) { String arg = args[index]; if ("-mode".equals(arg)) { if (index+1 >= args.length) { System.err.println("-mode option requires an argument"); System.exit(1); } try { testMode = TestMode.valueOf(args[index+1].toUpperCase()); } catch (IllegalArgumentException e) { System.err.print("-mode option must be from {"); for (int i=0; i