OutOfMemoryError when all memory is not used

Are you sure there should be plenty of memory left? Geometry might be a little more expensive than you realize. I haven't looked into great detail at the implementation of cylinder, but here is an estimate of your memory consumption:



4 bytes per int or float * (4 color floats + 3 vertex floats + 2 texture floats + 1 (or more) index floats + 3 normal floats)

= roughly 52 bytes per vertex



According to your code your cylinders are 12 x 4 samples, that will lead to roughly:



(12 + 1) * (4 + 1) * 2 (two triangles per quad) vertices per cylinder = 130 vertices



That's 130 vertices * 52 bytes / vertex + a "guestimate" of 100 bytes for the cylinder for the objects

= about 7,760 bytes per cylinder



Your loop creates 100,000 of theses cylinders which is roughly 776,000,000 bytes and I believe this is the lower bound. Depending on how the exact details work out, more vertices could be created, more indexes are probably needed, etc. Also, there will be memory not visible to the JVM that lives in the native code space where the opengl libraries live that could cause a discrepancy in what memory you are actually able to use.



This is easily close to your upper limit of 1000m and could cause an out of memory. Have you looked at using a SharedMesh instead of creating essentially the same geometry over and over again?

When you specify memory limit with Xmx you're only specifying java heap memory. jME uses native buffers which reside in a different memory pool. You need to use -XX:MaxDirectMemorySize to change direct memory size. E.g

-XX:MaxDirectMemorySize=1000M



Also, it is preferable to re-use a mesh if possible rather than creating 32000 unique cylinders. You can use the SharedMesh class to help you with that.

You might want to consider geometry batching. You combine multiple meshes into one, if they are in the same "block". This reduces the number of meshes sent to the video card, and thus improves performance drastically.



Also, it would help a lot if you said what exactly are you trying to achieve with this, there is probably a faster and more efficient way to do what you're trying to do.