title: JVM consumes all of my RAM for 40 lines of code
body: <p>I am using a 64 bit linux machine (8GB of RAM) on KDE with Eclipse as my IDE. I am also using Oracle's JDK. I made a small animation using JavaFX and a few pictures off the web to animate earth rotating around the sun. Whenever I run it, the animation works normally, but it steadily eats all of the RAM on my computer until everything freezes. This usually takes less than 5 minutes. </p>
  
  <pre><code>package Practice;
  /**
   * For some reason, this code gobbles up memory, and freezes computers
   */
  
  import javafx.scene.canvas.Canvas;
  import javafx.scene.canvas.GraphicsContext;
  import javafx.scene.image.Image;
  import javafx.animation.AnimationTimer;
  import javafx.application.Application;
  import javafx.scene.Group;
  import javafx.scene.Scene;
  import javafx.stage.Stage;
  
  public class BasicAnimation extends Application {
  
      public BasicAnimation() {
          // TODO Auto-generated constructor stub
      }
  
      public void start(Stage primaryStage) throws Exception {
          primaryStage.setTitle("Orbit");
  
          Group root = new Group();
          Scene scene = new Scene(root);
          primaryStage.setScene(scene);
  
          Canvas canvas = new Canvas(512,512);
          root.getChildren().add(canvas);
  
          GraphicsContext gc = canvas.getGraphicsContext2D();
  
              Image earth = new Image(getClass().getResourceAsStream("earth.png"), 25.0, 25.0 ,false, false);
          Image sun = new Image(getClass().getResourceAsStream("sun.jpg"), 50.0, 50.0, false, false);
          Image space = new Image(getClass().getResourceAsStream("space.jpg"));
  
          final long startNanoTime = System.nanoTime();
  
          new AnimationTimer() {
  
              public void handle(long currentNanoTime) {
  
                  double t = (currentNanoTime - startNanoTime) / 1000000000.0 ;
  
                  double x = 232 + 128 * Math.cos(t);
                  double y = 232 + 128 * Math.sin(t);
  
                  //background image clears canvas
  
                  gc.drawImage(space, 0, 0);
                  gc.drawImage(earth, x, y);
                  gc.drawImage(sun, 196, 196);
  
              }
          }.start();
  
          primaryStage.show();
      }
  }
  </code></pre>
  
  <p>I've set -Xms512m, -Xmx512m, and -Xss512m. Is there something I'm doing wrong that could be causing this, and could you explain why that happens or how to avoid it? </p>
  
  <p>Also if there is something wrong with my question, please let me know. </p>
  
  <p>Edits: Added more information</p>
  
  <p>The Earth image is 2356x2356, and I set it to 25x25px in the program. The Sun image is 750x750, and I set it to 50x50 in the program . The space image is 1920x1080, and it is the background which is 512x512 px.</p>
  
  <p>Links to images </p>
  
  <p>Sun : <a href="https://www.thesun.co.uk/wp-content/uploads/2016/06/download.jpg?w=750&amp;strip=all" rel="nofollow noreferrer">https://www.thesun.co.uk/wp-content/uploads/2016/06/download.jpg?w=750&amp;strip=all</a></p>
  
  <p>Earth : <a href="https://openclipart.org/image/2400px/svg_to_png/218125/3d-Earth-Globe.png" rel="nofollow noreferrer">https://openclipart.org/image/2400px/svg_to_png/218125/3d-Earth-Globe.png</a></p>
  
  <p>Space : <a href="http://www.gunnars.com/wp-content/uploads/2014/08/Space.jpg" rel="nofollow noreferrer">http://www.gunnars.com/wp-content/uploads/2014/08/Space.jpg</a></p>