LWJGL
June 19, 2013, 06:41:14 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: LWJGL is now using GitHub
 
   Home   Help Search Login Register  



Pages: [1]
  Print  
Author Topic: LWJGL Program continually using RAM  (Read 1043 times)
Pitbull917
Newbie
*
Posts: 2


« on: February 20, 2012, 18:38:11 »

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]
Code:
// 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();
}

}


Code:
// 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);
}
}


Code:
// 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!
Logged
kappa
Administrator
Nerdus Imperius
*****
Posts: 1117



« Reply #1 on: February 21, 2012, 01:42:31 »

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.
Logged
Pitbull917
Newbie
*
Posts: 2


« Reply #2 on: February 21, 2012, 13:08:04 »

Thank you!  Smiley I figured it out already though.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
SMFAds for Free Forums
Valid XHTML 1.0! Valid CSS!