Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does NetBeans declare low memory when I run an LWJGL-based Java app several times?

Currently experimenting with OpenGL in Java. After running the following test code several cycles within NetBeans, I receive a low memory error and the program terminates. The issue occurs some time after having run the application through a few successful cycles.

Why does this happen and how can it be fixed?

Code:

package test3d;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.input.Keyboard;

class ColoredTriangle {
    public void start() {
        try {
            Display.setFullscreen(true);
            DisplayMode dm = new DisplayMode(34,34);
           // Display.setDisplayMode(new DisplayMode(DisplayMode.get));
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }

         // Init OpenGL
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(-3, 3, -2.4, 2.4, -1, 1);
        GL11.glRotatef(0.0f,5.0f,1.0f,0.0f); 
        //GL11.glOrtho(0, 640, 480, 0, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        boolean quit = false;

        while (!quit) {         
            // Clear the screen.
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
            //GL11.glFrontFace(GL11.GL_CCW);
            // Begin drawing
            GL11.glBegin(GL11.GL_QUADS);
            GL11.glColor3f(1.0f,0.0f,0.0f); //Red   

     /*
                GL11.glVertex3f(0.0f,0.0f, 0.0f);

                GL11.glVertex3f(0.0f,1.0f, 0.0f);

                GL11.glVertex3f(1.0f,1.0f, 0.0f);

                GL11.glVertex3f(1.0f,0.0f, 0.0f); //*/


                 GL11.glVertex3f(1.0f,0.0f, -1f);

                GL11.glVertex3f(1.0f,1.0f, -1f);

                GL11.glVertex3f(2.0f,1.0f, -1f);

                GL11.glVertex3f(2.0f,0.0f, -1f);

              GL11.glEnd();  



            Display.update();

            if (Display.isCloseRequested() || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
                quit = true;
        }

        Display.destroy();
        System.exit(0);
    }
}
class Test3d
{
    public static void main(String args[]) {
        ColoredTriangle ct = new ColoredTriangle();
        ct.start();
    }

}
like image 316
varuog Avatar asked Feb 10 '13 00:02

varuog


2 Answers

This is a known problem with NetBeans.

NetBeans JVM is not unloading the LWJGL DLL after each recycle, which is called via JNI through LWJGL on each execution of your application.

I know of a similar issue (see this SO question referencing it) with the Tomcat Application Server running a web application which uses JNI. If you unload and reload said JNI-accessing web application using the Tomcat administrator, the DLLs referenced by JNI are not unloaded and all sorts of problems and conflicts are encountered when the application is started back up. The proper usage in said case is to completely stop the Tomcat service, then start Tomcat again. It creates headaches for those who want to use the admin to update their JNI-referencing deployables; they have to do it manually.

Based on your evidence and the first link, NetBeans also suffers from this issue and the only workarounds are to either:

  1. not reload your application so often
  2. restart the IDE after a couple of application reloads
  3. use a different IDE that might not do this (perhaps Eclipse?)
like image 142
JoshDM Avatar answered Sep 17 '22 11:09

JoshDM


Had a similar memory problem on my Linux Debian.

Here is how to fix it:

  1. run a terminal
  2. log in as root
  3. type crontab -e
  4. scroll to the bottom or the file and type * * * * * sync; echo 3 > /proc/sys/vm/drop_caches

This magic line cleared all unused ram every minute. It removed the unused memory NetBeans was producing (including any other memory-consuming programs).

This should work on most UNIX like OS's.

like image 1
grantperry Avatar answered Sep 21 '22 11:09

grantperry