How to modify my app/game so that it can load quickly? - android

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.

Related

LibGDX game freezes (because of a mulithreading issue?)

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)

android mapview app runs into memory issues (Grow Heap): Help choose design AsyncTask or IntentService or Service?

I've a mapview which needs to display plenty of overlay from the network. Now my current design is using AsyncTask where in a socket connection is open in doInBackground() and in publishProgress() it displays the overlay on the mapview.
Now the problem with this design is:
First there is overlays (ItemizedOverlays<Overlayitem>) about 100 or so which allocates some memory during the data processed. Next with overlay the map tiles has also to load, where in I run in memory problems of Grow Heap (frag case) as it has to load bitmaps (although I'm using AsyncTask to load and display in parallel executor).
The panning and zooming will take long time as there are overlays displayed and same time map tiles has to be loaded. And sometimes ANR dialog pops up.
Now my socket connection has to be open most of the time (say about 30mins) as I'm not sure at what time a new data is received, meaning the AsyncTask has to be running in the background for most of the time.
Now my plan is to move this AsyncTask class to IntentService class where this socket connection can be open most of the time, but how would I make it communicate with the FragmentActivity? Before moving to that, would it make sense to move to IntentServiceor Service at all from AsyncTask class that I'm already using?
Note: The only reason the I want to move to IntentService or Service is that I'm running into memory issues, and most of them by allocated memory to bitmap (maptiles). I tried cleaning the maptile cache as well, by not much luck. I tried calling System.gc() in a Timer thread to some extent it seems working, but long term not sure how it would perform.
EDIT:
Bottom line is GC is causing my app to slow down. Also please have a look at this link, how I'm actually loading my maptiles: optimize android code snippet - better design approach?
As a heads up, the maptiles loading is quite fast and good when no overlays are there. Its only when data is received through network, and tried to overlay on it, GC triggers multiple times.
Based on the comments it sounds like the GC is causing the app to slow down. How about instead of recycling your Bitmaps, you reuse them.
This is listing different questions, which are in fact completely distinct.
issue about osmdroid memory consumption due to map tiles
question about how to receive network data in something looking like Push mode
issue about handling plenty of overlays => how much? (100?) Of which kind? Even not clear for me if this one is really an issue for you.
From my point of view, as it is, this question should be removed, to be replaced by specific questions, properly formulated.

GC_FOR_ALLOC causing lag in Android game

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!

Android: OpenGL rendering pauses when heavy background task running

I am rendering objects via OpenGL, and got a nice smooth framerate of 60fps in most situations.
UNTIL I do something heavy in a background thread, like fetching stuff from a REST API, processing it, and adding objects to the graph (low-priority stuff, I care more about UI fluidity). The renderer will then pause for a very long period, up to 1 second (ca. as long as the background thread runs), and then resume as if nothing had happened. I noticed this because an animation is started at the same time, and it gets stuck for this period. The background thread is set to minimum priority, and garbage collection does take up to 100-200ms, but not the whole second. When I set a debug point anywhere in the background task, rendering continues just fine, without any delays.
Is it possible that my heavy background thread starves the OpenGL thread? If so, what can I do?
Of course! A GPU needs to be fed data, and that's done by the CPU. So, if something in the system bottlenecks, like I/O or CPU processing, then the GPU can't get fed. For example, animation is traditionally done on the CPU. This is why you get a lot of games on the PC getting higher frame rates with the same graphic chips but with different CPUs.
I also agree that profiling is a very good idea. If you can, I would suggest profiling to make sure it's actually the REST call, or if the REST call is one of many things
One thing I noticed about REST processing, and this happened to me. Since REST sometimes processes a lot of strings, and if you don't use StringBuilder, you can end up firing off a lot of garbage collection. However, it doesn't sound like you are getting this.

android: How to manage memory between application starts (application failure on second run)?

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.

Categories

Resources