JDK-8118993 : Media Objects cannot be closed or released.
  • Type: Bug
  • Component: javafx
  • Sub-Component: media
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2011-11-26
  • Updated: 2015-06-17
  • Resolved: 2012-02-01
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
fx2.1Fixed
Related Reports
Relates :  
Description
How come there's no way to close a media object when it's no longer in need.

I have a mediaview that constantly needs to be given a new Media object to play, but I don't see a method to close or dispose the previous media object before I make the switch.
The result of this is that, even though I no longer need a media file, I can't delete it or anything until I close the app because it is still being used somewhere in memory a la memory leak.


Comments
Hey there, oh boy, where I got my eyes. Thanks a lot for the hint, works like a charme :)
28-08-2013

You need to call dispose() method on MediaPlayer, if you do not want to wait for GC to release resources. This API is available since 2.2.40. See RT-28817.
27-08-2013

Hey there, doesn't work for me either. I'm using the MediaPlayer without the MediaView and I have the same problem. javafx.version: 2.2.25 java.runtime.name: Java(TM) SE Runtime Environment java.vm.name: Java HotSpot(TM) 64-Bit Server VM java.runtime.version: 1.7.0_25-b15 java.version: 1.7.0_25 os.name: Linux os.version: 3.2.0-52-generic Changing the GC doesn't seem to influence this behaviour, also. Any application trying to deal with kind of a playlist can't work with the MediaPlayer behaving like right now :(
27-08-2013

The suggested approach does not work. After executing the said code and doing a manual gc, the javafx thread still holds on to the media file and as such it cannot be deleted. Please verify if the inputstream is closed propery in all situations. Also, as has been noted, explicit api should be provided. See also: http://stackoverflow.com/questions/14282691/javafx-2-2-mediaplayer-how-to-release-media-file-handle-on-windows
06-02-2013

Yes, it will be freed when garbage collected. Setting another instance of MediaPlayer to MediaView will unregistered old one, so no need to call mediaView.setMediaPlayer(null) for each sequential media player. Only for last one. Example: MediaView mediaView = new MediaView(); MediaPlayer mediaPlayer1 = new MediaPlayer(new Media(URL)); MediaPlayer mediaPlayer2 = new MediaPlayer(new Media(URL)); mediaView.setMediaPlayer(mediaPlayer1); mediaPlayer1.play(); mediaPlayer1.stop() mediaPlayer1 = null; mediaView.setMediaPlayer(mediaPlayer2); // After line above GC will free mediaPlayer1 when it runs next time mediaPlayer2.play(); mediaPlayer2.stop() mediaView.setMediaPlayer(null); mediaPlayer2 = null; // After line above GC will free mediaPlayer2 when it runs next time
02-02-2012

So other than that, without the null, it just keeps holding on to memory of all other Media Objects? I assume by it being freed you mean by garbage collection. If that's the case, I suppose this is usable now. But a direct close method would still be required in the long run for better memory management. thanks guys.
01-02-2012

When MediaPlayer is stopped and removed from MediaView, then it should be disposed by garbage collection and files can be deleted. mediaPlayer.stop(); // Stop player mediaView.setMediaPlayer(null); // Remove association with MediaView
01-02-2012

http://jfxsrc.us.oracle.com/javafx/2.1/scrum/media/rt-closed/source/hg/javafx/2.1/scrum/media/rt-closed/rev/eededb2ded80 http://reavers.us.oracle.com/~alexander/2012/RT-18224/webrev3/
01-02-2012

So was there a plan at all to dispose stuff or ? ...
29-11-2011