Java Applet Memory Hog

I am creating a simple vector based game and just noticed something. My basic applet uses 22mb of memory! I learned this by opening the task manager and looking at the process java.exe on my windows xp machine. All my applet does it clear the screen and draw a (int)320 by (int)20 rectangle. The applets size is 320 by 240 so it can’t possibly be using that much memory for such a small screen. Is this correct or am i missing something? If this is correct is this going to impact my game in any way? Any assistance would be greatly appreciated.

Sincerely,

8BitOoze

The Java Virtual Machine has a heap on which it allocates objects. The JVM starts out with a rather large heap, which is nearly empty. Once you start creating new objects, it will fill the heap. All this time the TaskManager will say your JVM takes 22mb, because it only sees the heap the JVM has created.

Besides that, yes, a typical Java application requires a bit more memory than your average C application. Nothing to worry about though, it’s certainly not extreme. Every object has a 8 byte header, so you can do the math: an int is still 4 byte, and a float[100] is still 400 bytes plus object overhead. As the JVM doesn’t free each object independently, you’ll always have a fair amount of ‘garbage’ in the heap. Don’t worry, the JVM can move objects around, ‘compact’ them, and shrink the heap, when it notices you released a lot of objects.

Also since plugin2 you can set the heap size for applets using Xmx or Xms parameters. By default each applet has a maximum heap size of 64mb, so if you want a small heap and you know your app won’t go over it, you can set it as follows:

// set a maximum heap size of 16mb, default is 64mb
<PARAM name="java_arguments" value="-Xmx16m">

Ok, I will try that in the next update to my game and see if it helps manage the memory better.

Just wondering cause if an applet used much memory for a simple display without the above info, as a consumer i would’ve freaked out.

Sincerely,

8BitOoze

22mb isn’t really that bad for a VM running with libraries like AWT, Java2D, etc. A typical youtube video can easily use over 50mb.

Indeed, an empty tab in Chrome takes up something like 9mb. RAM’s not the valuable stuff it once was…

Cas :slight_smile: