Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LWJGL Memory leak/Function running many times  (Read 1590 times)
0 Members and 1 Guest are viewing this topic.
Offline GamerIDGoesHere

Junior Member





« Posted 2014-02-13 03:29:26 »

Hi,

Something in my code is making my draw function run tons of times, which is causing a memory leak, here is the draw code for the menu:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
      public void draw() {
          Color.white.bind();
          loadText();
          System.out.println("this shouldnt be running more than once"); // what i am using to test
         backg();
          handleInput();
             font.drawString(Display.getWidth() / 2 - 150, Display.getHeight() - 100, "Play", colorSelected);
             font.drawString(Display.getWidth() / 2 - 50, Display.getHeight() - 50, "Quit", colorSelected2);
         if(currentChoice == 0) {
            colorSelected = MenuButton.COLOR_SELECTED;
            colorSelected1 = MenuButton.COLOR_UNSELECTED;
            colorSelected2 = MenuButton.COLOR_UNSELECTED;
         }
         if(currentChoice == 1) {
            colorSelected = MenuButton.COLOR_UNSELECTED;
            colorSelected1 = MenuButton.COLOR_UNSELECTED;
            colorSelected2 = MenuButton.COLOR_SELECTED;
         }
         font.drawString(20, Display.getHeight() - 30, Game.VERSION, Color.black);
          cleanUp();
      }


Thanks,
- Dan
Offline opiop65

JGO Kernel


Medals: 124
Projects: 7
Exp: 3 years


Team Alluminum


« Reply #1 - Posted 2014-02-13 03:32:46 »

You do know that draw will be called every frame correct? It's not causing a memory leak. What do you except to happen?

Offline nerb
« Reply #2 - Posted 2014-02-13 04:21:48 »

Something in my code is making my draw function run tons of times, which is causing a memory leak

G'day Dan,

Obviously your draw function is meant to run 'tons of times' per second within your game loop, but are you implying that your draw method is being run more than once per loop? Or is one call to draw() resulting in draw() being called again and again? The only thing I can suggest is that you need to scour your code for calls to draw(), and ensure that it is only being called once, and is not being recursively called from within draw() itself.

As for the 'memory leak', are you creating new objects or similar during your draw() method? Are these objects being properly disposed or handled? Ideally, you should avoid object creation during a loop; it is generally bad news. If you have any resources that are repeatedly used within a loop, then create them outside of the loop and hang on to them. Assuming you have plenty of memory at your disposal, it could be favourable to create an object/allocate memory once, as opposed to continually re-creating and disposing of it. Just as a very, very wild guess, have a look at your loadText() method; what is happening in there?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GamerIDGoesHere

Junior Member





« Reply #3 - Posted 2014-02-13 08:30:05 »

Ok Ill have a look at that thanks, as for loadText(), this is all its doing:

1  
2  
3  
4  
   public void loadText() {
      glEnable(GL_BLEND);
       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   }


and this is cleanUp()
1  
2  
3  
   public static void cleanUp() {
      glDisable(GL_BLEND);
   }


Thanks,
- Dan
Offline trollwarrior1
« Reply #4 - Posted 2014-02-13 08:34:57 »

The line where memory leak happens would be helpful..
Offline GamerIDGoesHere

Junior Member





« Reply #5 - Posted 2014-02-13 08:50:45 »

Yeah well thats the problem, I'm trying to find out where the problem is or why its happening

this is my state code, this could be causing it:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
   // Render Game States
  private void render() {
      switch (state) {
      case INTRO:
         IntroState.draw();
         state = State.FADING;
         break;
      case FADING:
         try {
            Thread.sleep(1000);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
            state = State.MAIN_MENU;
         break;
      case MAIN_MENU:
         menu.draw();
         break;
      case GAME:
         gameState.Background();
         player.draw();
         enemy.draw();
         for (Particle c : particleList) c.draw();
      //   gameState.draw();
        break;
      case RAND:
         new RandomTerrain();
         break;
      }
   }


Thanks,
- Dan
Offline trollwarrior1
« Reply #6 - Posted 2014-02-13 08:52:12 »

You just said that you're getting memory leak. That means you're getting an exception somewhere. Post that exception and the code corresponding that exception here please.
Offline Grunnt

JGO Wizard


Medals: 55
Projects: 9
Exp: 5 years


Complex != complicated


« Reply #7 - Posted 2014-02-13 10:29:12 »

There doesn't seem to be anything remarkable about your code (other than the debugging println statement which would make things veeeery slow). Only thing that surprises me is that you do not seem to want your draw procedure to run each frame. When do you want draw() to be called? Also, memory leaks are quite hard to cause in Java, so why do you think you have one here?

Offline nerb
« Reply #8 - Posted 2014-02-13 10:32:24 »

So nothing much has changed since this post: http://www.java-gaming.org/topics/lwjgl-how-to-delay-between-states/32016/msg/298308/view.html Huh Anyway, I won't harp on about it.

I can potentially see a problem here:

1  
2  
3  
case RAND:
   new RandomTerrain();
   break;


Unless your RandomTerrain constructor changes the state, or you do it elsewhere, you will be stuck in the 'RAND' state, and will continuously create RandomTerrain objects.

You could always do some profiling to determine where your memory is being eaten.
Offline trollwarrior1
« Reply #9 - Posted 2014-02-13 10:34:00 »

Cheesy The only case when I cause memory leak is when I don't handle lists properly and they receive too many objects Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Longarmx
« Reply #10 - Posted 2014-02-15 05:03:13 »

How do you know that there is a memory leak? What code or resources have you used to find this problem? Also, that fading code looks a little strange...

Offline GamerIDGoesHere

Junior Member





« Reply #11 - Posted 2014-02-18 05:05:45 »

Well, in the RandomTerrain code, it runs fine at constant memory level, however in the menu state, the memory keeps going up and up until it crashes, also, it only seems to leak on some computers, on others it just goes up and down, like from 2.5GB to 3.5GB and back again not sure why.

Thanks,
- Dan
Offline trollwarrior1
« Reply #12 - Posted 2014-02-18 10:04:19 »

What is the line where the error occurs? Or does it simply crash?
Online Longarmx
« Reply #13 - Posted 2014-02-18 14:09:01 »

Have you tried calling glGetError();?

Offline Roquen
« Reply #14 - Posted 2014-02-18 14:16:49 »

Or to back up even further:  why do you think you have a leak?
Offline GamerIDGoesHere

Junior Member





« Reply #15 - Posted 2014-02-20 02:50:12 »

Well this is the error given when it crashes:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
Exception in thread "main" java.lang.OutOfMemoryError
   at sun.misc.Unsafe.allocateMemory(Native Method)
   at java.nio.DirectByteBuffer.<init>(Unknown Source)
   at java.nio.ByteBuffer.allocateDirect(Unknown Source)
   at org.lwjgl.BufferUtils.createByteBuffer(BufferUtils.java:60)
   at org.newdawn.slick.opengl.PNGImageData.loadImage(PNGImageData.java:96)
   at org.newdawn.slick.opengl.CompositeImageData.loadImage(CompositeImageData.java:62)
   at org.newdawn.slick.opengl.CompositeImageData.loadImage(CompositeImageData.java:43)
   at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:292)
   at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:254)
   at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:200)
   at org.newdawn.slick.opengl.TextureLoader.getTexture(TextureLoader.java:64)
   at org.newdawn.slick.opengl.TextureLoader.getTexture(TextureLoader.java:24)
   at ms.nac.spacejunk.states.IntroState.texture(IntroState.java:17)
   at ms.nac.spacejunk.main.Game.<init>(Game.java:77)
   at ms.nac.spacejunk.main.Game.main(Game.java:157)
AL lib: (EE) alc_cleanup: 1 device not closed
java.lang.OutOfMemoryError


Thanks,
- Dan
Offline ctomni231

JGO Ninja


Medals: 71
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #16 - Posted 2014-02-20 05:53:19 »

There is a loop that is creating objects but not destroying them causing Java to run out of memory. It definitely isn't a memory leak. A quick solution would be to increase the memory...

 -Xmx1024m 

Putting this within the VM Arguments or before you run your .jar can help...

I am suspecting your "Particle" class, since this is the only place I actually see some kind of a loop that can generate a lot of Objects. I would look around your code to places in where you are creating a lot of objects and start to comment out the loops, or if you are familiar with how to use a profiler, then you can check to see which Objects are taking up the most space in memory and are not getting collected by the Java GC.

Online BurntPizza
« Reply #17 - Posted 2014-02-20 06:20:43 »

Do not simply increase memory limit and pray.

Figure out what went wrong; if you simply need more memory then fine, otherwise raising the limit won't help.
That error tells me that you are creating lots of Textures and not disposing / destroying them and thus "leaking" memory.

Disclaimer: I don't use slick, and I don't know what's still current, but this is similar to what davedes said here: http://slick.ninjacave.com/forum/viewtopic.php?f=3&t=4652
Also check through those lines listed in that stacktrace in the slick source: https://bitbucket.org/kevglass/slick
Offline trollwarrior1
« Reply #18 - Posted 2014-02-20 09:13:19 »

This is why I'm "annoying" person to most people. Sorry I'm about to say this.. but...

Are you f**king serious?
I asked the line at which the error occurs like 2 or 3 or more days ago. Still no answer. How do you expect to get help, if you can't even show us where the error occurs?

From the error message, it is quite clear that you're allocating memory for some kind of image. You need to stop loading images every time you update/render game.
Offline Roquen
« Reply #19 - Posted 2014-02-20 09:37:00 »

My question would be:  what's with the constructor (<init>)?  If this is happening after many frame, then a new Main is being made many times.
Offline Gibbo3771
« Reply #20 - Posted 2014-02-20 10:05:42 »

Why is something that should only run one inside the draw loop?

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline The Lion King
« Reply #21 - Posted 2014-02-20 17:14:03 »

My question would be:  what's with the constructor (<init>)?  If this is happening after many frame, then a new Main is being made many times.

I looked at the LWJGL tutorials on their website and most of them do something like :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public class Game
{
       public Game()
       {
               //init
              //drawloop
      }
       public static void main(String[] args)
       {
                new Game();
       }
}


so the entire game runs in the main constructor the whole time. Im not saying this is what is happening, I haven't seen his code either but i'm pretty sure it is.

he seems to be creating a texture every frame

"You have to want it more than you want to breath, then you will be successful"
Offline Roquen
« Reply #22 - Posted 2014-02-20 18:19:10 »

Never do work in a constructor.
Offline The Lion King
« Reply #23 - Posted 2014-02-20 19:19:41 »

Never do work in a constructor.

I personally don't, but most people learned at least partly from LWJGL tutorials and those all do the work in the constructor. Ex: http://lwjgl.org/wiki/index.php?title=The_Quad_with_DrawArrays

"You have to want it more than you want to breath, then you will be successful"
Offline opiop65

JGO Kernel


Medals: 124
Projects: 7
Exp: 3 years


Team Alluminum


« Reply #24 - Posted 2014-02-20 20:47:13 »

I think those tutorials assume you know the basics of do's and dont's in programming. They are small code examples and you are supposed to pull out the information you need from them. Pretty much everyone knows not to do extensive work in constructors, especially not run entire games in them.

Offline princec

JGO Kernel


Medals: 282
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #25 - Posted 2014-02-21 01:09:31 »

Another tip - the OOME is being caused by a direct buffer allocation; these live outside of the heap and are unaffected by the -Xmx parameter. The switch governing the amount of direct memory available is -XX:MaxDirectMemorySize=...

Cas Smiley

Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (50 views)
2014-04-15 18:08:23

BurntPizza (46 views)
2014-04-15 03:46:01

UprightPath (62 views)
2014-04-14 17:39:50

UprightPath (44 views)
2014-04-14 17:35:47

Porlus (61 views)
2014-04-14 15:48:38

tom_mai78101 (84 views)
2014-04-10 04:04:31

BurntPizza (142 views)
2014-04-08 23:06:04

tom_mai78101 (242 views)
2014-04-05 13:34:39

trollwarrior1 (201 views)
2014-04-04 12:06:45

CJLetsGame (208 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!