 import java.util.Random; 
    import javafx.application.Application; 
    import javafx.stage.Stage; 
    import javafx.scene.Group; 
    import javafx.scene.Scene; 
    import javafx.scene.control.Button; 
    import javafx.scene.layout.VBox; 
    import javafx.scene.shape.Sphere; 

    public class SphereMemoryTest extends Application { 
        @Override 
        public void start(Stage primaryStage) { 
            try { 

                // --- User defined code ----------------- 
                VBox root = new VBox(); 
                Button btn = new Button("Add spheres..."); 
                Group container = new Group(); 
                root.getChildren().addAll(btn, container); 
                btn.setOnAction(e -> { 
                    createSpheres(container); 
                }); 
                // --------------------------------------- 
                Scene scene = new Scene(root,400,400); 
                primaryStage.setScene(scene); 
                primaryStage.show(); 
            } catch(Exception e) { 
                e.printStackTrace(); 
            } 
        } 

        public static void main(String[] args) { 
            launch(args); 
        } 

        // --- User defined code ------------------------------------------------------------------------ 
        // Each call increases the used memory although the container is cleared and the GC is triggered. 
        // The problem does not occur when all spheres have the same radius. 
        // ---------------------------------------------------------------------------------------------- 
        private void createSpheres(Group container) { 
                container.getChildren().clear(); 
                Runtime.getRuntime().gc(); 
                Random random = new Random(); 
                for (int i = 0; i < 500; i++) { 
                    //double d = 100; // OK 
                    double d = 100 * random.nextDouble() + 1; // Problem 
                    container.getChildren().add(new Sphere(d)); 
                } 
                System.out.printf("Spheres added. Total number of spheres: %d. Used memory: %d Bytes of %d Bytes.\n", 
                        container.getChildren().size(), 
                        Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(), 
                        Runtime.getRuntime().maxMemory()); 
        } 
        // ---------------------------------------------------------------------------------------------- 
    } 