Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (540)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  Is this a memory leak or am I just paranoid?  (Read 1199 times)
0 Members and 1 Guest are viewing this topic.
Offline lincore

Senior Newbie


Medals: 1



« Posted 2011-05-31 13:51:42 »

Hi everybody,

out of curiosity I monitored the memory usage of my game engine and noticed that it's memory usage rises at a rate of about 4k/second (task manager, Windows 7 x64). So far the program only consists of an entity/component system and a simple game class for testing. Essentially my game loop looks like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
void gameLoop() {
   if (scene == null) throw new RuntimeException("Critical: Game.scene must not be null.");
   millisPassed = System.currentTimeMillis();
   while (!gameEnd) {
      fdelta = (System.currentTimeMillis() - millisPassed) / 1000f;
      scene.update(fdelta);
      scene.render(fdelta);
      millisPassed = System.currentTimeMillis();
      calculateFps();
      yield();
   }
}


I already found out that when commenting the scene.update and scene.render method calls out the memory usage is constant, but the only thing I do in those methods is iterating over an empty ArrayList<Entity>:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public class Scene implements IScene {

   protected ArrayList<Entity>   entities;

   public Scene() {
      entities = new ArrayList<Entity>();
   }

[...]

     @Override
   public void update(final float fdelta) {
      for (Entity entity : entities) // <--- isEmpty
         entity.update(fdelta);
   }

   @Override
   public void render(final float fdelta) {
      for (Entity entity : entities)
         entity.render(fdelta);
   }
}


In their update and render method entities then call the update/render methods of their respective components which do the actual updating/rendering. As already mentioned the program currently iterates over an empty list of entities, so I don't understand why the memory usage increases constantly.
It's not a dramatic increase, from initially 6,000 KB it goes up to 15,000 in about half an hour, but it still concerns me as I assume that it'll be much harder to find the reason for this when my engine is actually doing something.

Here are the Entity's update and render methods:
1  
2  
3  
4  
5  
6  
7  
8  
9  
   public void render(final float fdelta) {
      for (IRenderCallback c : renderers)
         c.render(fdelta);
   }

   public void update(final float fdelta) {
      for (IUpdateCallback c : updates)
         c.update(fdelta);
   }


I'm new to java and have no clue if this behaviour is normal or what causes it, so I hope you guys can explain it to me.
Thanks in advance!
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 4
Exp: 8 years



« Reply #1 - Posted 2011-05-31 14:22:29 »

This is normal. The for:each loop expands to something like :

Quote
for(Iterator<Entity> i = entities.iterator(); i.hasNext(); ) {
  Entity entity= i.next();
...
}

So you see you have at least a new reference to a Iterator each iteration (even if the list is empty, you have to check the i.hasNext() .
So in half an hour all these references will sum up to something . The garbage collector will not run to clean up just a few KB,  it still has a lot of memory to spend before wanting to run.


Offline zoto

Senior Devvie


Medals: 4



« Reply #2 - Posted 2011-05-31 15:09:00 »

You will have a program that comes with java called jvisualvm.exe it is extremely useful for figuring out whats going on.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline lincore

Senior Newbie


Medals: 1



« Reply #3 - Posted 2011-05-31 17:18:49 »

The garbage collector will not run to clean up just a few KB,  it still has a lot of memory to spend before wanting to run.
Good to know, actually pretty obvious. Thanks for pointing that out.

You will have a program that comes with java called jvisualvm.exe it is extremely useful for figuring out whats going on.
I'll check it out, thanks.
Offline philfrei
« Reply #4 - Posted 2011-05-31 19:28:09 »

There is also JConsole. Mine is located in the jdk/bin or jdk/lib depending upon whether one wants to use a .jar or a .exe.

There is this article about how to use it.
http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html

I'd like to hear about the relative merits of the different profilers, if others are willing. I've never used jvisualvm.exe, didn't even know about it until reading the above.

I had good luck discovering some "leaking" threads (continually being created but never dying) during a session with jconsole. It is fascinating watching the GC cycles, as data is moved from "Eden" to "Survivor" to ...

[EDIT: JVisualVM.exe is really great! I've started using it. Found this article that might help others also new to this tool: http://java.dzone.com/news/visual-vm-free-and-open-source. The utility itself has links to "official" articles which are very helpful, too.]

"It's after the end of the world! Don't you know that yet?"
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2011-06-01 07:19:23 »

Really off topic but quite curious naming you have there.
What is with the "I"? Smiley

Offline zoto

Senior Devvie


Medals: 4



« Reply #6 - Posted 2011-06-01 08:03:45 »

Quote
Really off topic but quite curious naming you have there.
What is with the "I"?
My guess is "I" is for interface.
http://en.wikipedia.org/wiki/Hungarian_notation
Offline lincore

Senior Newbie


Medals: 1



« Reply #7 - Posted 2011-06-01 08:28:00 »

What is with the "I"? Smiley
Yes, that denotes an interface. I tried to use adjectives for interface names but to me it started to look silly pretty soon (for each Updateable do update...). So I ended up using substantives. My problem is that they tend to clash with class names and thats why prefixing interfaces with "I" works for me.

I thought this was pretty common:
http://java.about.com/od/javasyntax/a/nameconventions.htm
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2011-06-01 08:46:12 »

Oh I've never seen that before Tongue

Offline BoBear2681

JGO Coder


Medals: 19



« Reply #9 - Posted 2011-06-01 13:01:42 »

It seems to be the standard in the Eclipse platform.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (23 views)
2014-12-23 03:34:11

rwatson462 (53 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (85 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (78 views)
2014-12-05 12:39:02

SHC (89 views)
2014-12-03 16:27:13

CopyableCougar4 (96 views)
2014-11-29 21:32:03

toopeicgaming1999 (155 views)
2014-11-26 15:22:04

toopeicgaming1999 (152 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!