package MemoryLeakTester; import java.awt.event.ActionEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javafx.application.Platform; import javafx.embed.swing.JFXPanel; import javafx.scene.Scene; import javafx.scene.layout.StackPane; import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer; import javafx.scene.media.MediaView; import javax.swing.AbstractAction; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.SwingUtilities; import javax.swing.Timer; import javax.swing.WindowConstants; public class MemoryLeakTester { public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { System.getProperties().list(System.out); run(); } private static void run() { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { final JFrame frame = new JFrame(); frame.setSize(200, 200); frame.setVisible(true); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); showVideo(frame); } private void showVideo(final JFrame frame) { final JDialog dlg = new JDialog(frame); final VideoPlayer videoPlayer = new VideoPlayer( "http://download.oracle.com/otndocs/javafx/JavaRap_ProRes_H264_768kbit_Widescreen.mp4"); dlg.add(videoPlayer); dlg.setSize(800, 400); dlg.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.out.println("Stop playing"); videoPlayer.stop(); showVideo(frame); } }); Timer closer = new Timer(10000, new AbstractAction() { @Override public void actionPerformed(ActionEvent arg0) { System.out.println("closing dialog"); dlg.dispatchEvent(new WindowEvent(dlg, WindowEvent.WINDOW_CLOSING)); } }); closer.setRepeats(false); closer.start(); dlg.setVisible(true); } }); } private static class VideoPlayer extends JFXPanel { Media media; MediaView mediaView; MediaPlayer mediaplayer; public VideoPlayer(final String url) { Platform.runLater(new Runnable() { @Override public void run() { media = new Media(url); mediaplayer = new MediaPlayer(media); mediaView = new MediaView(mediaplayer); StackPane root = new StackPane(); root.getChildren().add(mediaView); setScene(new Scene(root, 200, 200)); mediaplayer.play(); } }); } public void stop() { mediaplayer.stop(); media = null; mediaplayer = null; mediaView.setMediaPlayer(null); mediaView = null; this.removeAll(); System.gc(); } } }