I'm developing a videogame in Android using OpenglES. But I'm having a problem with the main game loop or the renderer, because the game lags every 3 seconds (more or less) and I don't know why. The only thing I know is that it happens all the time, no matter what is going on in the game.
I have a main loop in where I call the gameInput, gameLogic and gameRenderer. When I call the gameRenderer, I wait until it finishes to continue... There are 20 fps every second.
Have this happened to anybody? Or do anybody have a clue about what's going on?
In theory if it's garbage collector you can pass something like this -Xms2G -Xmx2G (heap size) as VM arguments then garbage collector will not run before VM eats all 2G so lag should disappear and you will know that it is garbage collector problem.
Do you use java, right? It look like garbage collection. Try to disable parts of code and check if lag disappears. You can localize place where problem occures.
Also if you use Eclipse plug-in has DDMS thing. You can use Allocation Tracker from there to see which objects are created and how many of them.
Related
After playing my libgdx-game for some time, it just freezes. the game loop is not being executed anymore and it does not react to any input; the game can just be killed.
Logcat says this:
The memory graph shows:
(The freeze starts at about 8m30s. Before this time the game is running normally.)
The question: What is happing here? What do I have to do to avoid this game crash?
Could you post more logcat? It's hard do gather any information as to what is causing the problem just by looking at it saying it's suspending all threads.
This usually happens when you run out of memory. Do you have peices of code which load stuff into memory? You may want to take a look at that. Just a possibility, but maybe you are running out of memory. I know what the memory graph shows, but I had a similar problem and I fixed it by optimizing some asset loading.
(you may want to use asset manager to handle memory optimization)
https://github.com/libgdx/libgdx/wiki/Managing-your-assets
If that is not the case, please post some code. (or more logcat)
I have created a simple game using a class a which extends SurfaceView and implements Runnable. In the game the drawing is done inside the public void run. Which is targeted by a thread as soon as the activity launches. The game is taking a lot of time(sometimes 10-15 sec) to load. As well as when the game is paused(thread. join()) and resumed(thread = new Thread(this); thread.start()) it takes too much time.
What might be making the game load slow? And what can be the solutions?
The most common cause is resource loading.
I/O operations are very slow, so, if you try and load all your bitmaps before actually drawing, this can cause significant delay before the drawing actually starts.
One strategy to avoid this is to preload your assets before you actually need to start using them. A lot of games do this in the so - called Splash screen. Also, don't be too fast to recycle your assets and free the memory they are using. This means that you will need to reload them the next time you start.
Overall, there is no exact recipe of how to do it - there is a constant trade - off between load times and memory consumption. You just need to experiment and try to see what fits your use case best.
Be sure to profile your app, examine the object creations and memory allocation calls using your IDE. Most of the time you can easily pinpoint the causes of problems there.
I found that the game was loading slow because the thread has been running continuously, without any sleep. So just adding Thread.sleep(15); inside the run, with all exceptions handled, solved this problem. Thank you for supporting.
I created an app in android studio that plots a graph(using mp android chart) from the data received from a source. Here's the problem: when I try to plot the data at high frequencies (like 150hz plus) the app slows down and then crashes. At lower frequencies it can keep plotting for as long as I like so I know its not running out of memory. Maybe it needs a minimum amount of time to clear its memory? Is their anything I can do that can help me clear the memory when I want?
You can call System.gc() manually to invoke garbage collection, but Android will still kick in a GC when you are running out of memory.
Note that a garbage collection can easily take 100ms and when you have a refresh rate of 150hz you only have 1000/150 = 6.667 ms for your calculations together with garbage collection. So your first step should probably be to check your object allocations and try to minimize those. (in fact, in your methods which are called every frame you should have no object allocations at all)
I am working on a cannon game for Android, which requires procedural generation of levels. As the player flies through the air, new sections get added on to the end of the level to keep it going indefinitely. As those sections are added, sections that the player has already passed are deleted. The problem is that the creation of a section usually leads to a GC_FOR_ALLOC, which pauses the game for around 30ms and causes noticeable lag.
The free memory stays fairly consistent, as objects are deleted while others are added, so there doesn't seem to be a memory leak.
The engine I am using is the cocos2D-android-1 port from iOS.
I saw many solutions along the lines of "don't initialize things while the game is running", but the nature of the level generation requires this, since an indefinite-length level can't be created right at the start of the game.
Thanks for your help!
You are not very specific. If the lag is too big, you must try to reduce it.
One way could be to collect more often - resulting in shorter, but more often occurring pauses.
For example you might create smaller or only partial sections and try to nudge the garbage collector afterwards to run. You would use System.gc() for this. But be aware that System.gc() is not guaranteed to result in it actually starting to run.
An even better solution would be to reuse objects. Objects that were created before the level was started. This way even an endless level would not result in any garbage collection since no garbage accumulates. But be sure to not create any non-pooled objects!
Noob question!
My whiteboard/drawing app runs fine, using a combination of simple image views and bitmaps with me rendering a path to a bitmap and copying over as needed. I have it multitasking on my ICS Transformer without problems. However, if I exit the app with the Back button and then run it again, it fails; I get a memory error on the second run when I try to draw something.
Out of memory on a 4096016-byte allocation
Although sometimes I don't get that and it runs a second consecutive time. When I run it a third time, it works, and the fourth, again it Out-of-memory's.
What manual cleanup do I have to do when an Android app exits? Should I remove all created objects and bitmaps and paths and listeners and stuff?
Looks like you have a memory leak. Make sure you follow recommendations provided here. Often Memory Analyzer Tool is very useful in such cases. Here is a video how to use it.