Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8092928

[Charts,CSS] Memory leak in PieChart

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P2
    • Resolution: Fixed
    • Affects Version/s: 8u20
    • Fix Version/s: 8u40
    • Component/s: javafx
    • Environment:

      java version "1.8.0_20"
      Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
      Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)

      Description

      When updating a PieChart using any type of thread will create a heap out of memory exception. Please look at the code example attached with this bug.

      To speed things up please use the -Xms16m -Xmx16m setting to the virtual machine. Below is the example code. Please note that the code is a great example of cut-and-past coding.


      -------- FX ------------------------------------------------------------------------
      <AnchorPane fx:id="rootPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="446.0" prefWidth="546.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="com.zebware.PieController">
        <children>
          <VBox id="VBox" alignment="CENTER" layoutX="22.0" layoutY="0.0" spacing="5.0">
            <children>
              <Label text="Sample PieChart memory test" />
              <PieChart fx:id="storageAllocation" legendSide="RIGHT" animated="false" labelsVisible="false" startAngle="90.0" />
            </children>
          </VBox>
        </children>
      </AnchorPane>

      -- Java--------------------------------------------------------------------------------
      import java.net.URL;
      import java.util.ArrayList;
      import java.util.List;
      import java.util.Random;
      import java.util.ResourceBundle;

      import javafx.animation.AnimationTimer;
      import javafx.fxml.FXML;
      import javafx.fxml.Initializable;
      import javafx.scene.chart.PieChart;
      import javafx.stage.Stage;

      public class PieController extends Stage implements Initializable {

          @FXML private PieChart storageAllocation;

          private StorageUsageService sus = new StorageUsageService();
          private long lastUpdate = 0;

          @Override
          public void initialize(URL arg0, ResourceBundle arg1) {
              executePeriodicBackgroundTask();
          }


          public final void executePeriodicBackgroundTask() {
              AnimationTimer timer = new AnimationTimer() {

                  @Override
                  public void handle(long now) {
                      System.out.println("Update: " + (now - lastUpdate));
                      sus.succeeded();
                      lastUpdate = now;
                  }

              };

              timer.start();
          }

          // ---------------------------------------- Inner update class

          private class StorageUsageService {

              Random ran = new Random();

              private class StorageNotConfigured extends Exception {
                  private static final long serialVersionUID = 137978200558303650L;
              }

              public void succeeded() {
                  List<PieChart.Data> value = createTask();
                  storageAllocation.getData().clear();
                  storageAllocation.getData().addAll(value);
              }

              protected List<PieChart.Data> createTask() {

                          List<PieChart.Data> chartData = new ArrayList<PieChart.Data>();

                          try {
                              chartData.add(fetchAndAssemblePoolUsage());
                              chartData.addAll(fetchAndAssembleShareUsage());
                          } catch (StorageNotConfigured e) {
                              // pool is not configured, no problem
                          }

                          return chartData;

              }

              private PieChart.Data fetchAndAssemblePoolUsage() throws StorageNotConfigured {

                  return assembleChartData("Legend 1", ran.nextLong());
              }

              private List<PieChart.Data> fetchAndAssembleShareUsage() {
                  List<PieChart.Data> chartData = new ArrayList<PieChart.Data>();

                  chartData.add(assembleFolderAllocatedSizeData());
                  chartData.add(assembleVolumeAllocatedSizeData());

                  return chartData;
              }

              private PieChart.Data assembleFolderAllocatedSizeData() {
                  return assembleChartData("Legend 2", ran.nextLong());
              }

              private PieChart.Data assembleVolumeAllocatedSizeData() {
                  return assembleChartData("Legend 3", ran.nextLong());
              }

              private PieChart.Data assembleChartData(String textID, long size) {
                  return new PieChart.Data(textID, size);
              }

          }
      }

      ---- Starter class ----------------------------------------------------------------

      import javafx.application.Application;
      import javafx.fxml.FXMLLoader;
      import javafx.scene.Scene;
      import javafx.scene.layout.AnchorPane;
      import javafx.stage.Stage;


      public class FxTableApplication extends Application {

          public static Stage primaryAppStage = null;
          
          @Override
          public void start(Stage primaryStage) {
              try {
                  if( primaryAppStage == null ){
                      primaryAppStage = primaryStage;
                  }
                  AnchorPane page = (AnchorPane) FXMLLoader.load(FxTableApplication.class.getResource("PieExample.fxml"));
                  final Scene scene = new Scene(page);
                  primaryAppStage.setScene(scene);
                  primaryAppStage.setTitle("No idea...");
                  scene.getWindow().centerOnScreen();
                  primaryAppStage.show();
              } catch (Exception e) {
                 e.printStackTrace();
              }
          }

          public static void main(String[] args) {
              System.out.println("To speed things up please run with -Xms16m -Xmx16m (" + args.toString() + ")");
              launch(args);
          }
      }

        Attachments

          Activity

            People

            • Assignee:
              vadim Vadim Pakhnushev
              Reporter:
              duke J. Duke (Inactive)
            • Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported: