LWJGL Program continually using RAM

Started by Pitbull917, February 21, 2012, 02:38:11

Previous topic - Next topic

Pitbull917

Hey guys so I just started working on a game with LWJGL and I have the display working fine and I drew an image on the screen for the main menu with Slick2d but, when I run the program I notice my ram usage starts at 1.6GB and it just keeps going up 1.7GB 1.8GB ect.. But when I close it my ram usage goes back down to 1.6. Does anyone know why this is happening?

Here is my source:
[spoiler]
// Main class

package LWJGL2DGame;

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.opengl.*;

public class Game {
	
	public static final int WIDTH = 800;
	public static final int HEIGHT = 600;
	public static final String TITLE = "Rpg";
	public boolean running = true;
	
	private long lastFrame;
	
	Render render;
	MainMenu mainMenu;
	
	public Game() {
		try {
			Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
			Display.setTitle(TITLE);
			Display.create();
		}  catch(LWJGLException e) {
			System.out.println("ERROR IN CREATING DISPLATY!");
		}
		
		render = new Render();
		
		// Initialization
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GL11.glOrtho(0, WIDTH, HEIGHT, 0, -1, 1);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
		GL11.glEnable(GL11.GL_DEPTH_TEST);
		GL11.glEnable(GL11.GL_TEXTURE_2D);
		
		mainMenu = new MainMenu();
		
		getDelta();
		
		while(running && !Display.isCloseRequested()) {
			render();
			
			Display.sync(60);
			Display.update();
		}
		Display.destroy();
	}
	
	public long getTime() {
		return (Sys.getTime() * 1000) / Sys.getTimerResolution();
	}
	
	public int getDelta() {
		long time = getTime();
		int delta = (int) (time - lastFrame);
		lastFrame = time;
		return delta;
	}
	
	public void render() {
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		GL11.glLoadIdentity();
		
		mainMenu.loadMainMenu();
	}
	
	public static void main(String[] args) {
		new Game();
	}

}


// Main menu class

package LWJGL2DGame;

import org.newdawn.slick.opengl.Texture;

public class MainMenu {
	
	Render render = new Render();
	
	Texture mainMenuBg;
	
	public MainMenu() {
	}
	
	public void loadMainMenu() {
		mainMenuBg = render.loadImage("MainMenu");
		
	    render.drawImage(0, 0, 1025, 615, mainMenuBg);
	}
}


// Render class

package LWJGL2DGame;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class Render {
	
	public void drawImage(int x, int y, int width, int height, Texture tex) {
		tex.bind();
		GL11.glBegin(GL11.GL_QUADS);
		
		GL11.glTexCoord2d(0, 0);
		GL11.glVertex2i(x, y);
		
		GL11.glTexCoord2d(1, 0);
		GL11.glVertex2i(x + width, y);
		
		GL11.glTexCoord2d(1, 1);
		GL11.glVertex2i(x + width, y + height);
		
		GL11.glTexCoord2d(0, 1);
		GL11.glVertex2i(x, y + height);
		
		GL11.glEnd();
	}

	public Texture loadImage(String path) {
		try {
			return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/Gui/" + path + ".png")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}


[/spoiler]

Thanks in advance!

kappa

looks like you are creating a new texture every frame using the loadImage() via the mainMenu.loadMainMenu(); in the render() method, this is most likely why your ram usage is continually going up. You need to either only load the image on initialisation or destroy the image after you've finished using it to free up the memory.

Pitbull917

Thank you!  :) I figured it out already though.