JAVAFX媒体:严重的优化问题 [英] JAVAFX media : Serious optimization problem
问题描述
该程序是一个显示媒体的系统:图像,视频并在它们之间保持交替.问题是使用增加的内存:程序运行30分钟后,它消耗了1.2gb的内存
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
我对我能做什么没有太多的想法,我认为内存消耗增加的原因是递归(函数调用自身)或每次提供图片时都会创建线程的事实,以及视频时,它使用技术上可运行的正确"(.setOnEndOfMedia()) 请记住,我不能使用计时器/时间轴,因为我的视频时长不同,所以这种方式适用于图像
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 ...
推荐答案
如果您停止使用dispose()
对象来释放其所有资源,则需要在其上调用dispose()
.
You need to call dispose()
on your MediaPlayer
object if you stop using it to free all its resources.
还要确保您的Java版本为8或更高(旧版本中存在内存泄漏).
Also make sure your Java version is 8 or higher (there is a memory leak in older versions).
这篇关于JAVAFX媒体:严重的优化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!