问题
the program is a system that shows media: images, videos and keeps alternating between them. the problem is the use of increasing memory: after the programming running for 30 minutes, it consumes 1.2gb of ram
I do not have much idea of what I can do, I believe that the reason for the increasing memory consumption would be recursion (the function calls itself) or the fact that every time it gives a picture it creates a thread, and when it video it uses the technically 'correct' which is a runnable (.setOnEndOfMedia ()) Remembering that I can not use timer / timeline, because I have videos with different durations, this way would work with image
package testevideo2;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import javafx.util.Duration;
public class TesteVideo2 extends Application{
StackPane stack = new StackPane();
int xImagem = 0;
int xVideo = 0;
public void start(Stage primaryStage) throws Exception {
//primaryStage.setScene(videoCena);
primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
primaryStage.setFullScreen(true);
primaryStage.setTitle("Titulo bonito");
primaryStage.show();
proximo(primaryStage);
/*player.play();
player.setOnEndOfMedia(new Runnable() { //Classe Anônima
@Override
public void run() {
primaryStage.setScene(imagemCena);
//primaryStage.centerOnScreen();
}
});*/
}
private void proximo(Stage primaryStage){
//valores serao pego da api...
boolean[] eVideo = {false, false, true, false, true};
String[] nomeImagens = {"doido.jfif", "eu.jpg", "resultado.jpg", "37Teste.jpg"};
String[] nomeVideos = {"xx.mp4", "carinha.mp4"};
final String diretorioBase = "file:/C:/Users/Thiago/Desktop/arquivos_projetoandre/";
if(xImagem + xVideo < eVideo.length){
//look if the next file is a video or an image
if(eVideo[xImagem + xVideo]){
//criador de video
Media media = new Media(diretorioBase + nomeVideos[xVideo]);
MediaPlayer player = new MediaPlayer(media);
Scene videoCena = new Scene(new Group(new MediaView(player)), 1366, 720);
videoCena.setCursor(Cursor.NONE);
player.play();
player.setOnEndOfMedia(new Runnable() { //Classe Anônima
@Override
public void run() {
proximo(primaryStage);
//primaryStage.centerOnScreen();
}
});
primaryStage.setScene(videoCena);
xVideo++;
} else {
//criador de imagem
Pane pane = new HBox();
Image img = new Image(diretorioBase + nomeImagens[xImagem]);
pane.getChildren().add(new ImageView(img));
Scene imagemCena = new Scene(pane, 1366, 720);
//PROBABLY PROBLEM HERE --- CREATE A NEW THREAD ONLY TO WAIT 4 SECONDS
Thread a = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(4000);
//force to the application run on 'javaFx thread'
Platform.runLater(new Runnable(){
@Override
public void run() {
proximo(primaryStage);
}
});
} catch (InterruptedException ex) {
Logger.getLogger(TesteVideo2.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
a.start();
primaryStage.setScene(imagemCena);
xImagem++;
//Thread.sleep(4000);
//proximo(primaryStage);
}
} else {
xVideo = 0;
xImagem = 0;
proximo(primaryStage);
}
}
public static void main(String [] args) {
Application.launch();
}
}
I hope it does the same function as it is now, except in a way that the use of processing is increasing over time, because this application will run for hours ...
回答1:
You need to call dispose()
on your MediaPlayer
object if you stop using it to free all its resources.
Also make sure your Java version is 8 or higher (there is a memory leak in older versions).
来源:https://stackoverflow.com/questions/57014752/javafx-media-serious-optimization-problem