This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

HELP: Using MediaTracker causes OutOfMemoryError  RSS feed

 
K Duke
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone:
I am trying to create a simple JApplet animation sequence using (about) 129 JPEG images. I am using a MediaTracker object to keep track of image loading.
--------------------------------------------------------------

-------------------------------------------------------------------
After adding all images to a Mediatracker object, I call the waitForAll() method to ensure all images are loaded before I call paint. I am using a Swing Timer object that fires every 100 ms, and calls repaint(). Decreasing the number of images to 10 or so obviously prevents the OutOfMemoryError.
However, I MUST to be able to load all 129 images before I render the animation sequence.
Any suggestions? Thanks in advance.
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use smaller images?
 
K Duke
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Ess:
Use smaller images?

Assuming that is *NOT* an option, Is there any other way?
 
K Duke
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, the images are around 70 KB each.
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only thing I can think of, if indeed loading the images is the problem, is that the VM is so busy loading all 129 images, it doesn't get a chance to request more memory from the system and that's what causes it to run out of memory. Try loading the images in blocks of 10 or 20. Call Thread.yield() at the end of each loop to let the system catch up. It's not the most efficient thing, but it's better than hitting the wall when running out of memory.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not going to stop an OutOfMemoryError... that actually means you have used up all the available JVM heap memory. There are commands to configure the maximum heap sizes of the JVM, but none of these are going to be available to you if you're running as an Applet. You're going to have to cut down on the number of images you're loading or their size ( very, very drastically... because I'm getting OutOfMemoryErrors with as few as *7* 50K images...) or use some kind of actual video package (i.e. JMF or Quicktime for Java ) for this.
[ October 23, 2003: Message edited by: Nathan Pruett ]
 
K Duke
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to add each image to a MediaTracker object using a loop:
for(int i=imgFirst, j=0; i<=imgLast; i++, j++) {
images[j] = getImage(getCodeBase(), imgDir + separator + imgName
+ i + imgExt);
tracker.addImage(images[j], j+1);
}
Once I am done displaying the image, will the call:

followed by a call:

free memory used to load images that have been displayed already.
I was thinking of freeing memory in a separate thread while the main thread displays the image. I am having some problems synchronizing the two threads.
Do you think this is a good idea?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh! I forgot about removeImage!

Yes, it looks like the reason MediaTracker is causing an OutOfMemoryException is because the MediaTracker saves an extra copy. Calling removeImage() must null this copy out.

Here's the code I was using to test it out... it might help you -

 
K Duke
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nathan,
Thanks a lot. It does work. I was initially using a Timer to fire events every X msec, and loading the images in a separate thread. I faced some synchronization problems.
Your approach works fine. The problem is that I am trying to simulate a movie effect by displaying about 10 fps or more. So the time delay between slices is about 100 ms or less. The thread loop takes more than 100 ms to remove the old image, load the new image and call the garbage collector. So the time between 2 images is now > 100 ms (on an average about 200 ms). I am trying to get the delay between images down to 50 ms.
I call System.currentMillis() before image display, and the second time after it does the remove-load-gc sequence, and this difference is about 200 ms.
Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!