Jal Jal - 1 year ago 94
Java Question

JVM consumes all of my RAM for 40 lines of code

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.

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 {

Group root = new Group();
Scene scene = new Scene(root);

Canvas canvas = new Canvas(512,512);

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



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?

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

Edits: Added more information

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.

Links to images

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

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

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

Answer Source

I can't see anything wrong in your code. It may not be the best way to do this in JavaFX but it looks perfectly valid to me and should not eat up any memory. Especially as you say that you have the same problem with the other code from Luke I suspect some Linux bug. Have you tried running your program on another OS? If you'd provide the links to your images someone else could try that too.

This link may be related to your problem: javafx-unexplainable-leaks-memory-on-linux


I ran your program on a Mac and there was no memory leak and almost no CPU usage, as I'd expected.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download