Reusing a MediaTracker

C

Chris Berg

My Java application is handling a lot of images. For some of the
images I need to load them completely before handlng them, so I use a
MediaTracker. Here is my utility method for that purpose:

private static MediaTracker mt = new MediaTracker(new Canvas());
private static int mtID;

// return 0 if OK
public static int trackImage(Image img) {
if (img == null) {
return 16;
}
try{ // .. finally
try {
mt.addImage(img, ++mtID);
mt.waitForID(mtID, 5000);
} catch (Throwable e) {
return 16;
}
int r = mt.statusID(mtID, true);
if ( (r & MediaTracker.COMPLETE)!=0) {
return 0;
} else {
return r;
}
}finally{
mt.removeImage(img,mtID);
}
}

My concern is, do I really get everything properly cleaned-up? or is
the MediaTracker at risk of getting filled up with images, eventually
causing a memory leak?

Is it OK to re-use the MediaTracker this way? All the code examples
I've seen, creates a new instance with every image, and that seems to
me to be a waste of ressources. Or?

Chris
 
O

Oliver Wong

Chris Berg said:
My Java application is handling a lot of images. For some of the
images I need to load them completely before handlng them, so I use a
MediaTracker. Here is my utility method for that purpose:
[code snipped]

My concern is, do I really get everything properly cleaned-up? or is
the MediaTracker at risk of getting filled up with images, eventually
causing a memory leak?

Have you looked at the removeImage() methods? The javadocs aren't clear
on what they do, but perhaps they only make the MediaTracker stop keeping
track of them, while leaving the image data itself uncorrupted. Could that
be useful for what you're trying to do?

- Oliver
 
C

Chris Berg

Oh,

I just realise that there is also a thread-safe issue here. I have
multiple threads that call trackImage. Maybe I should start by
protecting mtID a little better:

private static MediaTracker mt = new MediaTracker(new Canvas());
private static volatile int mtID; // volatile forces write to mem

// return 0 if OK
public static int trackImage(Image img) {
if (img == null) {
return 16;
}
int id = ++mtID; // make a local copy
try{ // .. finally
try {
mt.addImage(img, id);
mt.waitForID(id, 5000);
} catch (Throwable e) {
return 16;
}
int r = mt.statusID(id, true);
if ( (r & MediaTracker.COMPLETE)!=0) {
return 0;
} else {
return r;
}
}finally{
mt.removeImage(img,id);
}
}

- - but is the MediaTracker itself thread-safe? If not, I may as well
forget all about it, I guess.... :-(


Chris
 
C

Chris Berg

Yes, that's the exact way to express it:

Does removeImage(..) virginalize the (already deflowered) MediaTracker
or not?

Chris

Chris Berg said:
My Java application is handling a lot of images. For some of the
images I need to load them completely before handlng them, so I use a
MediaTracker. Here is my utility method for that purpose:
[code snipped]

My concern is, do I really get everything properly cleaned-up? or is
the MediaTracker at risk of getting filled up with images, eventually
causing a memory leak?

Have you looked at the removeImage() methods? The javadocs aren't clear
on what they do, but perhaps they only make the MediaTracker stop keeping
track of them, while leaving the image data itself uncorrupted. Could that
be useful for what you're trying to do?

- Oliver
 
O

Oliver Wong

Chris Berg said:
- - but is the MediaTracker itself thread-safe? If not, I may as well
forget all about it, I guess.... :-(

While the JavaDocs never explicitly say "MediaTracker is thread-safe",
the examples on there uses threads, and some of the API only makes sense in
the context of threads (e.g. the waitForID() methods), so I'd assume that
they are thread safe until new evidence presents itself.

- Oliver
 
C

Chris Berg

You are propably right.
Looking at MediaTracker's code, some or all the methods are
synchronized, which implies that multiple threads could add images to
a single MediaTracker. If that was not the intent, I fail to see why
the methods are synchronized.

Chris
 
S

steve

You are propably right.
Looking at MediaTracker's code, some or all the methods are
synchronized, which implies that multiple threads could add images to
a single MediaTracker. If that was not the intent, I fail to see why
the methods are synchronized.

Chris

there is code floating about that adds multiple images to a media tracker,
either on the oracle site or on suns.

I always wondered why someone would want to do that.
Steve
 
O

Oliver Wong

steve said:
there is code floating about that adds multiple images to a media tracker,
either on the oracle site or on suns.

I always wondered why someone would want to do that.

Are you serious? I thought the typical usage would be to add more than
one image. The MediaTracker's main purpose is to facilitate asynchronous
media loading. That is, when youre applet starts, you just give a list of
all the images you want loaded, and then you paint a "Please wait... loading
media..." until the MediaTracker says "Okay, all your images are loaded."

What have you been using the MediaTracker for?

- Oliver
 
A

Andrew Thompson

Oliver Wong wrote:
....
What have you been using the MediaTracker for?

I cannot speak for steve, but I commonly use a MediaTracker
on single images for this reason.

An Image takes up a lot more memory than the corresponding
byte[] that represents the image. As a result of this, you
can easily run out of working RAM if you load 'a lot' of images.

The easy workaround is to only ever retain one Image, and
to use Toolkit.creatImage(byte[]), when you need to change
from viewing one image to another.

Even the createImage method is asynchronus, so by adding the
single Image to the MediaTracker, you can ensure it is 'fully
formed' before attempting to display it/write it.
 
S

steve

Are you serious? I thought the typical usage would be to add more than
one image. The MediaTracker's main purpose is to facilitate asynchronous
media loading. That is, when youre applet starts, you just give a list of
all the images you want loaded, and then you paint a "Please wait... loading
media..." until the MediaTracker says "Okay, all your images are loaded."

What have you been using the MediaTracker for?

- Oliver

well I don't know about you oliver, but i use it to keep track of my VCD
collection, and my hard to find LD classics.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top