views:

705

answers:

2

I am writing a Java applet that downloads images from a web server and displays them to the user. It works fine in Java 1.6.0_3 and later, but on older versions it will completely crash the process about once every 20 page views. There are no error messages in the Java console, because the process is completely frozen. I've waited for almost 15 minutes sometimes, but it never un-freezes.

I added a debug message after every line of code, and determined that the line that is causing the crash is this: InputStream data = urlConn.getInputStream().

urlConn is a URLConnection object that is pointed at the image I want to load. I've tried every combination of options that I can think of, but nothing helps. I haven't been able to find anything in the Java bug database or the release notes for 1.6.0_3.

Has anyone encountered this problem before? Any idea how to fix it?

+1  A: 

To determine if it really is the whole JVM process that's frozen, or something else:

(1) get a java stack dump (sigquit/ctrl-break/jstack)

(2) have another background thread doing something you can observe; does it stop?

(3) check if another process (browser/etc) can contact server during freeze? (There's a chance the real problem is server connection depletion)

Is it randomly once-in-every-20-fetches (for example, 5% of the time, sometimes the first fetch in the JVM run), or always after about 20 fetches? If the latter, it sounds like something isn't being closed properly.

If on Linux you can use 'netstat -t' or 'lsof' (with certain options or grepped to show only some lines) to see open sockets; if after each fetch, one more is open, and the count never goes down, you're not closing things properly.

If so, calling close() on the stream you get back and/or disconnect() on the HttpUrlConnection after each try may help. (There may also be more severe limits on the number of connections an applet can leave open, so you're hitting this more quickly than you would in a standalone app.)

The fact that it 'works' in later Javas is also suggestive that some sort of automatic cleanup might be happening more effectively/regularly by finalization/GC. It's best to close things up cleanly yourself but you could also try forcing a GC/runFinalization in the earlier Javas showing the problem.

gojomo
I tried #2 a while back, and every thread froze at the same time.It's a random 5% of page views - sometimes it happens the first time the applet is loaded.I forgot to close the data stream, so that could have been the issue. But when I added code to do that, it didn't solve the problem.So, I'm still stuck.
A: 

I'm unsure the cause of the problem you are facing, but I use the following code successfully for synchronously loading images from within applets (loads from either jar file or the server):

public Image loadImage(String imageName) {

    // get the image
    Image image = getImage(getCodeBase(), imageName);

    // wait for it to fully load
    MediaTracker tracker = new MediaTracker(this);
    tracker.addImage(image, 0);
    boolean interrupted = false;
    try {
        tracker.waitForID(0);
    } catch (InterruptedException e) {
        interrupted = true;
    }

    int status = tracker.statusID(thisImageTrackerID, false);
    if (status != MediaTracker.COMPLETE) {
        throw new RuntimeException("Failed to load " + imageName + ", interrupted:" + interrupted + ", status:" + status);
    }

    return image;
}
Pool