SethGunnells
SethGunnells

Reputation: 1269

Java Applet does not start properly when Max Heap Size is set

I have been struggling with developing an applet recently. I have been trying to launch it with JNLP and the "deployJava.js" script. The issue is that this is a memory intensive applet I am developing and will definitely require more than the basic memory allocation that the JVM is usually given.

I attempted to set this using the max-heap-size attribute of the j2se element in the JNLP. This does in fact work, but the problem is that it seems the applet restarts somehow because I get two "java console" windows that open. One receives no output from the applet while the other shows the logging info that I am outputting. Also, the applet does not appear to run. However, if I, without closing the browser, clear the classloader cache and reload the page, the applet works with the correctly adjusted max heap size and everything.

I have no idea what is causing this strange behavior, but I desperately need a solution here. If I do not set a max heap size, the applet behaves fine (except for running out of memory.) Also, if I use the applet tag in the HTML setting the max heap size via <param name="java_arguments" value="-Xmx1g"> it works fine. However, I understand that the applet tag is deprecated and may not work for all browsers so I do not want to stick with it. I hope someone can provide some insight into what I'm missing here.

The bottom line: When I add max-heap-size the applet does not work properly. I need help figuring out why.

Here's the HTML/JavaScript/PHP:

<script src="http://www.java.com/js/deployJava.js"></script>
<script> 
    var attributes = {
        code: 'floodsim.SimApplet.class', archive: 'SimApplet.jar', 
        width: 500, height: <?php echo "$height"; ?>}; 
    var parameters = { 
        <?php
            echo "imgwidth: $imgwidth, imgheight: $imgheight, imgcount: $imgcount, key: '$key',";
        ?> jnlp_href: 'simApplet.jnlp'}; 
    deployJava.runApplet(attributes, parameters, '1.6');
</script>

Here's the JNLP (I also used JaNeLa to check it):

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="http://localhost:8888/floodsim/simulation/" href="simApplet.jnlp">
    <information>
        <title>Simulation Viewer</title>
        <vendor>Seth Gunnells</vendor>
        <offline-allowed />
    </information>
    <resources>
        <!-- Application Resources -->
        <j2se version="1.6+" max-heap-size="1024m" />
        <jar href="SimApplet.jar" main="true" />
    </resources>
    <applet-desc 
        name="Simulation Viewer"
        main-class="edu.tntech.floodsim.SimApplet"
        width="500"
        height="600">
    </applet-desc>
</jnlp>

Upvotes: 1

Views: 793

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168825

Setting heap size in an applet

Nowhere in the JNLP does it specify max-heap-size. The example given is:

<j2se version="1.3" initial-heap-size="64m" max-heap-size="128m"/>

Nowhere in the applet element does it specify Java arguments. The example given is:

<PARAM name="java_arguments" value="-Xmx128m">

Upvotes: 1

Related Questions