Hi!
I’m working on a pretty simple 2D platform game for android, now the problem is that on some devices it works great but in a bit older devices it lags a bit which if weird because the game is very simple. I probably have a memory leak somwhere, how can I find it?
Thanks!
Is there any particular reason you think it’s a memory leak and not something else? Also, by lag I assume you mean that the frame rate is low or performance is otherwise poor?
You can’t really have memory leaks in Java since the JRE manages the memory itself via the gc.
Just something that is generally computationally expensive.
And the answer to how to find it is to run it through a profiler and see which bits are taking the longest. Any good IDE will have a decent profiler built in.
It doesn’t matter, there may be a lot going on under the hood you don’t know about. It may also be possible that you are allocating and de-allocating lots of memory (i.e. creating lost of new objects) which would definitely impact performance on “slower” phones.
I don’t believe you can because the JVM handles memory allocation for you. If I’m wrong, someone tell me please!
First thing would be to check if you create a lot of objects. Check for instances of the “new” keyword. Do you load or recreate textures every render call? I quite honestly have never used the profiler built into Eclipse, so I won’t be of much help there, but you can use that to see exactly what is causing your program to hang up. Hopefully someone else can be of more help in that area.
watch the memory graph. if it goes up over time you might have a leak. also watch the allocated objects, try to filter a bit. maybe you spot a object count growing and never purged by GC. the class will lead you to the spot of leakage. - if any.
Just in case you don’t know where JVisualVM.exe is, it’s in the JDK directory, the bin subfolder.
When you run it, and there are other Java programs running, JVisualVM can see them and offer you the service of attaching and monitoring/profiling them.
I’d probably run the app outside of Eclipse, though, and keep Eclipse off while checking it out.
You need to profile your application. You’re jumping to the conclusion that your problem is caused by a “memory leak”, but you don’t really have a reason to believe that. That’s like choosing a random disease to blame your headache on.
There are a ton of ways to profile an application, and google is your friend. Like others have said, you can monitor how much memory your application uses over time. You could also monitor how much time various parts of your code are taking. You might even step through your code and look for problems.
If you can’t narrow it down by profiling, then try to boil your problem down to the absolute smallest example you can create that still exhibits the behavior. Start with a completely blank application, then only add one tiny thing at a time. That’ll make it much easier to figure out what’s going on.