GamerIDGoesHere
|
 |
«
Posted
2014-02-13 02: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"); 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
|
|
|
|
Opiop
|
 |
«
Reply #1 - Posted
2014-02-13 02: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?
|
|
|
|
nerb
|
 |
«
Reply #2 - Posted
2014-02-13 03: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!
|
|
GamerIDGoesHere
|
 |
«
Reply #3 - Posted
2014-02-13 07: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
|
|
|
|
trollwarrior1
|
 |
«
Reply #4 - Posted
2014-02-13 07:34:57 » |
|
The line where memory leak happens would be helpful..
|
|
|
|
GamerIDGoesHere
|
 |
«
Reply #5 - Posted
2014-02-13 07: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
| 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(); break; case RAND: new RandomTerrain(); break; } } |
Thanks, - Dan
|
|
|
|
trollwarrior1
|
 |
«
Reply #6 - Posted
2014-02-13 07: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.
|
|
|
|
Grunnt
|
 |
«
Reply #7 - Posted
2014-02-13 09: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?
|
|
|
|
nerb
|
 |
«
Reply #8 - Posted
2014-02-13 09: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  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.
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Longarmx
|
 |
«
Reply #10 - Posted
2014-02-15 04: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...
|
|
|
|
GamerIDGoesHere
|
 |
«
Reply #11 - Posted
2014-02-18 04: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
|
|
|
|
trollwarrior1
|
 |
«
Reply #12 - Posted
2014-02-18 09:04:19 » |
|
What is the line where the error occurs? Or does it simply crash?
|
|
|
|
Longarmx
|
 |
«
Reply #13 - Posted
2014-02-18 13:09:01 » |
|
Have you tried calling glGetError();?
|
|
|
|
Roquen
|
 |
«
Reply #14 - Posted
2014-02-18 13:16:49 » |
|
Or to back up even further: why do you think you have a leak?
|
|
|
|
GamerIDGoesHere
|
 |
«
Reply #15 - Posted
2014-02-20 01: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
|
|
|
|
ctomni231
|
 |
«
Reply #16 - Posted
2014-02-20 04: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... 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.
|
|
|
|
BurntPizza
|
 |
«
Reply #17 - Posted
2014-02-20 05: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=4652Also check through those lines listed in that stacktrace in the slick source: https://bitbucket.org/kevglass/slick
|
|
|
|
trollwarrior1
|
 |
«
Reply #18 - Posted
2014-02-20 08: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.
|
|
|
|
Roquen
|
 |
«
Reply #19 - Posted
2014-02-20 08: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.
|
|
|
|
Gibbo3771
|
 |
«
Reply #20 - Posted
2014-02-20 09: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
|
|
|
The Lion King
|
 |
«
Reply #21 - Posted
2014-02-20 16: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() { } 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"
|
|
|
Roquen
|
 |
«
Reply #22 - Posted
2014-02-20 17:19:10 » |
|
Never do work in a constructor.
|
|
|
|
|
Opiop
|
 |
«
Reply #24 - Posted
2014-02-20 19: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.
|
|
|
|
princec
|
 |
«
Reply #25 - Posted
2014-02-21 00: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 
|
|
|
|
|