JVM consumes all my RAM for 40 lines of code

I am using a 64-bit Linux machine (8 GB of RAM) in KDE with Eclipse as my IDE. I also use Oracle JDK. I made a little animation using JavaFX and a few shots on the Internet to revitalize the Earth orbiting the Sun. Whenever I run it, the animation works fine, but it constantly eats all the RAM on my computer until everything hangs. This usually takes less than 5 minutes.

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();
    }
}

I installed -Xms512m, -Xmx512m and -Xss512m. Is there something I'm doing wrong that could be causing this, and could you explain why this is happening or how to avoid it?

Also, if something is wrong with my question, please let me know.

Edited: additional information added

2356x2356, 25x25px . 750x750, 50x50 . 1920x1080, 512x512 .

: https://www.thesun.co.uk/wp-content/uploads/2016/06/download.jpg?w=750&strip=all

: https://openclipart.org/image/2400px/svg_to_png/218125/3d-Earth-Globe.png

: http://www.gunnars.com/wp-content/uploads/2014/08/Space.jpg

+4
4

. , JavaFX, . , , Luke, , Linux-. ? , - .

: javafx-unexplainable-leaks-memory-on-linux

Test

Mac, , .

+1

gc.drawImage(space, 0, 0); . IMHO, .

. Graphics, . , JavaFX.

JavaFX - , . , "", , .. Animation, "".

, , . . //.

: Path.setInterpolator(Interpolator.LINEAR)

import javafx.animation.PathTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.BoxBlur;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.util.Duration;

import static javafx.animation.Animation.INDEFINITE;

public class Animation extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("orbit");

        Group root = new Group();
        Scene scene = new Scene(root, 800, 800, Color.BLACK);
        primaryStage.setScene(scene);

        Circle sun = new Circle(50, Color.web("yellow", 1.0));
        sun.setCenterX(400);
        sun.setCenterY(400);
        sun.setEffect(new BoxBlur(10, 10, 3));

        Circle earth = new Circle(10, Color.web("blue"));
        earth.setEffect(new BoxBlur(4, 4, 3));

        root.getChildren().add(sun);
        root.getChildren().add(earth);

        Path path = new Path();
        ArcTo arcTo = new ArcTo();
        arcTo.setX(20);
        arcTo.setY(401);
        arcTo.setSweepFlag(true);
        arcTo.setLargeArcFlag(true);
        arcTo.setRadiusX(400);
        arcTo.setRadiusY(400);
        arcTo.setXAxisRotation(0);

        path.getElements().add(new MoveTo(20, 400));
        path.getElements().add(arcTo);
        path.getElements().add(new ClosePath());
        path.setVisible(false);

        PathTransition pt = new PathTransition(Duration.seconds(10), path, earth);
        pt.setInterpolator(Interpolator.LINEAR); // No acceleration/deceleration
        pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
        pt.setCycleCount(INDEFINITE);
        pt.play();

        primaryStage.show();
    }
}
+1

tl; dr -D.prism=sw JVM, .

JavaFX , , Mesa Xorg . , VDPAU, , . -D.prism=sw JVM, , , . , , .

, , gc.drawimage(), , .

new AnimationTimer() { //This is how I reduced the number of times gc.drawImage is called

        long lastupdate = 0 ;

        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

            if(currentNanoTime - lastupdate >= 33333333) {
                gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
                gc.drawImage(space, 0, 0);
                gc.drawImage(sun, 196, 196);
                gc.drawImage(earth, x, y);

                lastupdate = currentNanoTime;
            }


        }
    }.start();

, . VisualVM , .

. , . JavaFX, , VDPAU. !

:

https://github.com/jfoenixadmin/JFoenix/issues/52

https://bugs.openjdk.java.net/browse/JDK-8161997

0

Windows 7 8- java 8 .

VisualVM: enter image description here

, , , , .

: gc.drawImage(, 0, 0); , .

enter image description here

Your program "works", but its really inefficient

Also, you do not need a 512 MB stack size (-Xss512m)!

check out a good example of an animation example in JavaFx here

0
source

Source: https://habr.com/ru/post/1662469/


All Articles