Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applet: Java heap space

Due to a small implementation mistake I discovered how quickly I could reach a Java heap space issue

now the bug is fixed everything is fine but it did get me looking into how to solve this and I foudn multiple solution such as

java -Xms5m -Xmx15m MyApp

the problem is that this changes the java memory on my computer, but I'm working on a Applet that is going to be used in a webrowser.

Therefore, is there a way, at RUNTIME in an APPLET to change the heap size ?

like image 350
Jason Rogers Avatar asked Feb 17 '11 07:02

Jason Rogers


People also ask

How do I fix Java out of memory error heap space?

OutOfMemoryError: Java heap space. 1) An easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options "-Xmx512M", this will immediately solve your OutOfMemoryError.

What are the limitations of applet in Java?

Applets cannot stream data directly into the browser. Applets cannot subscribe to events detected by the browser that are triggered outside of the applet area. For example, applets cannot detect that a user followed a bookmark. Similarly, applets cannot detect that a user typed in a URL that should be followed.

How do I manage Java heap space?

The short answer is that you use these java command-line parameters to help control the RAM use of application: Use -Xmx to specify the maximum heap size. Use -Xms to specify the initial Java heap size. Use -Xss to set the Java thread stack size.


4 Answers

You can add parameters to the applet tag. But the parameter you are interested on is available only on Java6 u10 or later.

Example:

<APPLET archive="my_applet.jar" code="MyApplet" width="300" height="300">
    <PARAM name="java_arguments" value="-Xmx256m">
</APPLET>

Here more information http://www.oracle.com/technetwork/java/javase/plugin2-142482.html#JAVA_ARGUMENTS

like image 155
Dubas Avatar answered Oct 14 '22 11:10

Dubas


AFAIK, only user can change JRE heap settings. Applet can not change this settings.

Update:

It seems that in the latest versions of JDK this is possible. Look at: How can I start an Java applet with more memory?

Update2:

Memory settings can only be set for JNLP apps, not for Applets.

like image 41
Peter Knego Avatar answered Oct 14 '22 10:10

Peter Knego


If it's not specified on the command line, you have to get it from the JVM settings. So when you deploy your applet to the web, it will be dependent on what memory settings they have on their computer when they run it. Typically it's set to 60-90Mb by default, so try to keep it under that.

Consider the ramifications if the applet could change those settings... what else it might be able to change. That's just asking for a security exploit eventually, and Java aims for security before functionality :)

like image 21
corsiKa Avatar answered Oct 14 '22 09:10

corsiKa


the JVM may have started long before you Applet. It is too late now to change heap size. Try Java Web Start where you can control that, spawning a new JVM for your Applet/application.

like image 24
Eduard Avatar answered Oct 14 '22 10:10

Eduard