7 Replies Latest reply: May 13, 2013 9:04 AM by 866233 RSS

    Memory leak within Media / MediaPlayer / MediaView

    978093
      Hi,

      I have a very simple program that plays 3 video one after another in a loop on Windows 7.
      With visualvm I do not see the heap increasing, but with the Windows Task Manager the memory usage is growing rapidly. After an amount of hours the movieplayer stops playing movies.
      I do not see anywhere where I can clean up.

      Can anybody help me please?

      The code is:

      Media media = null;
      MediaPlayer mediaPlayer = null;
      MediaView mediaView1 = new MediaView();
      private final MediaHandler mh;
      static public Group root;

      @Override
      public void start(Stage stage)
      {
      root = new Group();
      stage.setTitle("JavaFX test");
      stage.setResizable(true);
      stage.setFullScreen(true);
      Scene scene = new Scene(root);
      scene.setFill(Color.WHITE);
      stage.setScene(scene);
      stage.show();
      mh = new MediaHandler();
      mh.start();
      }

      /**
      *
      *
      */
      public class MediaHandler extends Thread
      {
      /**
      *
      */
      public MediaHandler()
      {
      mediaView1.setX(0);
      mediaView1.setY(0);
      root.getChildren().add(mediaView1);
      }

      /**
      *
      */
      @Override
      public void run()
      {
      synchronized (root)
      {
      do
      {
      createMediaplayer();
      try
      {
      root.wait();
      }
      catch (InterruptedException e)
      {
      }
      }
      while (true);
      }
      }
      }

      /**
      *
      * @param mediaPlayer
      */
      private void createMediaplayer()
      {
      mediaView1.setVisible(true);
      if (media == null || media.getSource().equals(url1))
      {
      media = new Media(url2);
      mediaPlayer = new MediaPlayer(media);
      mediaPlayer.setCycleCount(1);
      mediaPlayer.setAutoPlay(false);
      mediaView1.setMediaPlayer(mediaPlayer);
      mediaPlayer.play();
      }
      else if (media.getSource().equals(url2))
      {
      media = new Media(url3);
      mediaPlayer = new MediaPlayer(media);
      mediaPlayer.setCycleCount(1);
      mediaPlayer.setAutoPlay(false);
      mediaView1.setMediaPlayer(mediaPlayer);
      mediaPlayer.play();
      }
      else
      {
      media = new Media(url1);
      mediaPlayer = new MediaPlayer(media);
      mediaPlayer.setCycleCount(1);
      mediaPlayer.setAutoPlay(false);
      mediaView1.setMediaPlayer(mediaPlayer);
      mediaPlayer.play();
      }

      mediaPlayer.setOnEndOfMedia(new Runnable()
      {
      @Override
      public void run()
      {
      synchronized (root)
      {
      root.notifyAll();
      }
      }
      });
      }

      public static void main(String[] args)
      {
      launch(args);
      }
      }

      Edited by: 975090 on 5-dec-2012 2:56