LWJGLShaderUtil consuming too much native memory [FIX]

Hello all,



due to this JVM bug: http://bugs.sun.com/view_bug.do?bug_id=4469299 I’m facing a number of java.lang.OutOfMemoryError in my shader code, coming from LWJGLShaderUtil.updateUniformLocation();



So I decided to minimize byteBuffer creation by simply referring to a shared byteBuffer allocated statically.



Here’s the patch for LWJGLShaderUtil. It works very well and now the application does not crash anymore for OutOfMemoryError.



Index: LWJGLShaderUtil.java
===================================================================
--- LWJGLShaderUtil.java   (revision 5353)
+++ LWJGLShaderUtil.java   (working copy)
@@ -62,6 +62,8 @@
 /** Utility class for updating shadervariables(uniforms and attributes) */
 public class LWJGLShaderUtil {
     private static final Logger logger = Logger.getLogger(LWJGLShaderUtil.class.getName());
+    
+    private static ByteBuffer   nameBuf   = BufferUtils.createByteBuffer(2048);
 
     /**
      * Updates a uniform shadervariable.
@@ -107,10 +109,10 @@
     public static void updateUniformLocation(ShaderVariable variable,
             int programID) {
         if (variable.variableID == -1) {
-            ByteBuffer nameBuf = BufferUtils
-                    .createByteBuffer(variable.name.getBytes().length + 1);
+          
             nameBuf.clear();
             nameBuf.put(variable.name.getBytes());
+            nameBuf.put((byte)0);
             nameBuf.rewind();
 
             variable.variableID = ARBShaderObjects
@@ -207,10 +209,10 @@
     public static void updateAttributeLocation(ShaderVariable variable,
             int programID) {
         if (variable.variableID == -1) {
-            ByteBuffer nameBuf = BufferUtils
-                    .createByteBuffer(variable.name.getBytes().length + 1);
+
             nameBuf.clear();
             nameBuf.put(variable.name.getBytes());
+            nameBuf.put((byte)0);
             nameBuf.rewind();
 
             variable.variableID = ARBVertexShader


A small note, if the LWJGLShaderUtil is used from multiple threads it will cause threading issues. However many other jME2 classes suffer from the same issue so I don't think its a problem.

I approve.

Yeah JME 2 is hopelessly not thread safe.  :slight_smile:

Hi Mik.



I applied and committed your patch.



thx,

dhdd