JavaFx2 OutOfMemoryError:Java heap space

I creat a simple JavaFx application with FXML and CSS in order to change UI Theme at runtime. There are three Button and one Label in the scene. I define different CSS for Label and "-fx-background-image"(PNG format, size 1.23M) for BorderPane in the CSS file. Application can switch UI Theme by click "Style" Button. But Mem usage will raise and don't free when I click one of "Style" Button. Application will OutOfMemoryError after switch Theme about 30 times. I don't know how to fix it.Someone can help me?

java.lang.OutOfMemoryError: Java heap space
    at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:75)
    at java.awt.image.Raster.createPackedRaster(Raster.java:467)
    at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032)
    at java.awt.image.BufferedImage.<init>(BufferedImage.java:359)
    at com.sun.prism.j2d.J2DTexture.<init>(J2DTexture.java:46)
    at com.sun.prism.j2d.J2DResourceFactory.createTexture(J2DResourceFactory.java:72)
    at com.sun.prism.impl.BaseResourceFactory.createTexture(BaseResourceFactory.java:127)

My environment: jdk1.7.0_51, Windows Server 2003 x86, 3G RAM

I am sorry that I don't know how to post IMG file(background.png) to forum.

Click "AutoSwitch" Button for auto Switching Theme.

Source Code: Main.Java

public class Main extends Application {    
    private Stage primaryStage;     

public Stage getPrimaryStage() {
    return primaryStage;
}

public void setPrimaryStage(Stage primaryStage) {
    this.primaryStage = primaryStage;
}

private BorderPane rootPane;

public BorderPane getRootPane() {
    return rootPane;
}


@Override
public void start(Stage primaryStage) {
    try {       
        setPrimaryStage(primaryStage);

        URL url = new URL(getClass().getResource("AppFrame.fxml").toExternalForm());

        FXMLLoader loader =new FXMLLoader(url);
        rootPane = (BorderPane)loader.load();

        Scene scene = new Scene(rootPane,400,400);

        primaryStage.setScene(scene);

        //MAX screen
        Screen screen = Screen.getPrimary();
        Rectangle2D bounds = screen.getVisualBounds();

        primaryStage.setX(bounds.getMinX());
        primaryStage.setY(bounds.getMinY());
        primaryStage.setWidth(bounds.getWidth());
        primaryStage.setHeight(bounds.getHeight());

        primaryStage.show();

        AppFrameController appFrameController = (AppFrameController)loader.getController();
        appFrameController.setAppMain(this);

    } catch(Exception e) {
        e.printStackTrace();
    }


}

public static void main(String[] args) {
    launch(args);
}
}

AppFrameController.java

public class AppFrameController  {
    private String style1Url = getClass().getResource("FxStyle1.css").toExternalForm();
    private String style2Url = getClass().getResource("FxStyle2.css").toExternalForm();
    @FXML private Label lblSceneTitle;

    private Main appMain;
public Main getAppMain() {
    return appMain;
}

public void setAppMain(Main appMain) {
    this.appMain = appMain;
}


private int switchCount=0;

public AppFrameController()
{
}



public void handleClickStyle1()
{       
    ObservableList<String> stylesheets = getAppMain().getRootPane().getStylesheets();

    stylesheets.remove(style2Url);
    if(!stylesheets.contains(style1Url))
    {
        stylesheets.add(style1Url);
    }

}

public void handleClickStyle2()
{

    ObservableList<String> stylesheets = getAppMain().getRootPane().getStylesheets();

    stylesheets.remove(style1Url);
    if(!stylesheets.contains(style2Url))
    {
        stylesheets.add(style2Url);
    }

}

public void handleClickSwitch()
{
    SwitchCssTask handleTask = new SwitchCssTask();     
    new Thread(handleTask).start();             

}

private class SwitchCssTask implements Runnable {

    @Override
    public void run() {
        while (true) {

            if (switchCount % 2 == 0) {
                handleClickStyle1();
            } else {
                handleClickStyle2();
            }

            switchCount++;

            System.out.println("Switch count=" + switchCount);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }


}   
}

AppFrame.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane prefHeight="362.0" prefWidth="749.3739013671875" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.AppFrameController">
  <!-- TODO Add Nodes -->
  <center>
    <BorderPane id="center-pane" prefHeight="-1.0" prefWidth="-1.0" BorderPane.alignment="CENTER">
      <center>
        <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" spacing="20.0">
          <children>
            <Button mnemonicParsing="false" onAction="#handleClickStyle1" prefHeight="70.0" prefWidth="148.0" text="Style1" />
            <Button mnemonicParsing="false" onAction="#handleClickStyle2" prefHeight="70.0" prefWidth="148.0" text="Style2" />
            <Button mnemonicParsing="false" onAction="#handleClickSwitch" prefHeight="70.0" prefWidth="148.0" text="AutoSwitch" />
          </children>
        </VBox>
      </center>
    </BorderPane>
  </center>
  <stylesheets>
    <URL value="@FxStyle2.css" />
  </stylesheets>
  <top>
    <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="10.0" BorderPane.alignment="CENTER">
      <children>
        <Label id="scene-title" fx:id="lblSceneTitle" alignment="TOP_CENTER" prefWidth="371.0" text="Test FxStyle" />
      </children>
      <padding>
        <Insets bottom="15.0" left="12.0" right="12.0" top="15.0" />
      </padding>
    </HBox>
  </top>
</BorderPane>

FxStyle1.css

#center-pane {
      -fx-background-image: url("background.png");
}



#scene-title {
    -fx-font-family: "Tahoma";
    -fx-background-insets: 0;
    -fx-text-fill: green;
    -fx-font-weight: bold;       
    -fx-font-size: 4em; 

    -fx-effect: dropshadow( one-pass-box , rgba(0,0,0,0.9) , 1, 0.0 , 0 , 1 );
}

FxStyle2.css

#center-pane {
      -fx-background-image: url("background.png");
}


#scene-title {
    -fx-font-family: "Courier New";
    -fx-background-insets: 0;
    -fx-text-fill: red;
    -fx-font-weight: bold;       
    -fx-font-size: 4em; 

    -fx-effect: dropshadow( one-pass-box , rgba(0,0,0,0.9) , 1, 0.0 , 0 , 1 );
}

Answers


I get answer from Oracle Communtiy. please see here.


Need Your Help

Abstraction and Encapsulation

java oop encapsulation abstract

When studying OOP, mainly java, these two concepts are always linked, but I see no real relationship or similarity between them. Correct my if I'm wrong but: