Increasing maximum heap size

hi,

i am trying to create large terrains and am running out of memory. i ran this bit:



long heapSize = Runtime.getRuntime().totalMemory();

System.out.println("Heap Size = " + heapSize);



which told me that I have 16 mb to work with. I want to increase this to something like 64 or 128 mb. how should i do this?

___________________________

also,

i am trying to run this bit of code, which I got from the official tutorials, in eclipse:



heightmap = new ImageBasedHeightMap(

ImageToAwt.convert(heightMapImage.getImage(), false, true, 0));



but I am getting an error: The constructor ImageBasedHeightMap(BufferedImage) is undefined

I changed it to this:



BufferedImage bi = ImageToAwt.convert(heightMapImage.getImage(), false, true, 0);

heightmap = new ImageBasedHeightMap(bi);



and the second line now gives the error, and eclipse tells me to change the type of bi to “Image” - I don’t know which one.

I do change it to java.awt.Image



java.awt.Image bi = ImageToAwt.convert(heightMapImage.getImage(), false, true, 0);

heightmap = new ImageBasedHeightMap(bi);



-doesn’t work, second line still gives me error - “The constructor ImageBasedHeightMap(Image) is undefined”



the code works fine in jME SDK, and in general jMonkey works in eclipse. after a bit of poking around I realized that eclipse is getting confused between the common java Image class and jME’s Image class. how should i resolve this?

I think you mean something like this: java -XmxFILESIZEm -jar your_jar.jar

i.e : java -Xmx256m -jar mbox2eml.jar

1 Like

First of all… for future reference, it’s Runtime.maxMemory() that you are interested in printing. totalMemory() just shows you what the JVM has allocated at the moment.



The default on modern JVMs seems to be 256 meg.



Second, increasing the regular heap may only help a little and you may have to increase direct memory (native buffers) also.

-XX:MaxDirectMemorySize=1024m for example.



Be careful how you set these. If you set -Xmx too high then the garbage collector may never run even if you are out of direct memory (that’s where the vertex buffers, etc. are stored). Ideally, you want to have more direct memory setup than you will ever need and then only as much max heap as you can get away with.



For example, Mythruna used to have 1024m for both and I constantly had out of memory errors. Lowering -Xmx to 512m got rid of my out of memory errors. It’s counter intuitive until you understand what’s happening. Direct memory will never trigger GC on its own.



For the Eclipse issues, you are on your own. Though it sounds like you have a bad import to me or that Eclipse added a bad one for you or something. Since that has nothing to do with the title of this thread, I don’t know if any of the other Eclipse users will even drill in to see it.

2 Likes

For eclipse, you are thanks to the dleted project fiels left with the official build system.



Run the build.xml (with a jdk set in the settings) and it should give you a jar file with everything you need.

Thanks for the replies all, but after hearing of the risks associated with increasing heap space, I have decided to change my implementation so that I won’t need to increase the memory.



But the second problem with Eclipse remains. @Empire, I am not sure which build.xml you are referring to. I found one in the workspace for jME but I am not sure whether that’s the right one, because my problem is related to Eclipse.

1 Like

You should increase heap size and direct memory size. You’re life will be happier. You should also try to use less memory. :slight_smile:



If you do nothing else, at least increase direct memory size. The default is really quite small for 3D apps.

(I’m sorry about this noob question) I understand that I have to execute a command like this to increase direct memory: -XX:MaxDirectMemorySize=1024m



but how do I execute it? Do I go to Windows command prompt…? I’m a newbie at anything except for Unix system programming really, so excuse me :confused:

yes.



http://hub.jmonkeyengine.org/groups/beta-1-game-contest/forum/topic/wip-the-cove-contest-entry/?topic_page=7&num=15#post-156116

You can set it via jmp too:



2 Likes

thx a bunch for the info, i was actually wondering the same as lordsurya08,

on an added note, heres how to set it on java web start:





j2se version=“1.6+” java-vm-args="-Xmx512m -XX:MaxDirectMemorySize=1024m"

href=“http://java.sun.com/products/autodl/j2se”/>



you can also use: initial-heap-size=“64m” and/or max-heap-size=“512m”



as such:





j2se version=“1.6+” initial-heap-size=“64m” max-heap-size=“512m”

href=“http://java.sun.com/products/autodl/j2se”/>



BTW, if you change this settings, you will need to delete your internet temp folder and/or java temp folder, cuz the JNLP wont update itself if there was a cached copy.



and finally, on another added note about what pspeed was saying, i also noticed that if the garbage collection is too close to your used heap size, you get a lot of “mini-freezes”, i suppose it is becuz the garbage collector is getting called a lot… hope this helps someone :wink:



cheers…

A full GC will generally pause all threads… incremental GC doesn’t and will happen all the time if the garbage collector feels the need.



Ideally, you want to set the heap small enough so that the garbage collector feels the need to sweep up garbage but not so small that it has to run a full GC in order to satisfy allocation needs. And sometimes experimenting is the best way to find a sweet spot.



Though if all of your app can fit in a certain size head and a certain size direct buffer pool then just set them both larger than that and don’t worry. My game churns through a lot of direct memory so it’s important that I get the garbage collector to perform incremental sweeps as often as possible to avoid full GC stalls (or worse, out of memory errors).

Don’t run the heap m8 is for noobs. I use a 1024mb haxxed stack don’t do garbage collection ever. No classes no arrays. Wanna compete, see who’s faster?

@pspeed said:
A full GC will generally pause all threads... incremental GC doesn't and will happen all the time if the garbage collector feels the need.


ye, i noticed this behavior even without understanding what was actually going on... thx for clarifying it...