question

Android avatar image
Android asked

Process kept alive

We are porting a popular series of games to Android and in particular the Kindle. Here's how we are handling each of the lifecycle/view methods: surfaceCreated() - start a thread which handles the game logic/update surfaceDestroyed() - change flag to bring thread to an end and join() onPause() - stop all sound effects and pause music (if any) onResume() - resume music and if in main game state, call pause menu onDestroy() - set any static references to null to help with garbage collection. The problem we are having is that if the back softkey from the toolbar is pressed, the Activity is closed and onDestroy() is called. All fine. However - the process itself is not stopped and is listed under settings as 'Cached Background Process'. When the game is launched again from the carousel onCreate() etc. are all called again as you would expect, but the heap seems to still be occupied by the previous process. Since our game uses a lot of graphics this then causes an OutOfMemoryException and the game crashes. This DOES NOT happen when we have tested the game on other Android devices. It also seems that the ClassLoader is not unloading the classes after the Activity is exited, which is why I am setting any static references to null. Again, I'm not sure if this is expected behaviour or not. One fix I have discovered is to call System.exit(0) in onDestroy() - however my understanding is that I'm not supposed to be doing this in Android, and it seems a little 'hackish'. I'd like to understand what the underlying problem is so that it can properly be addressed!
fire tablet
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Bipin@Amazon avatar image
Bipin@Amazon answered
Hi Jonathan Fisher, The Kindle system attempts to keep application process around for as long as possible, but eventually will need to remove old processes when memory runs low. As described in Activity Lifecycle, the decision about which process to remove is intimately tied to the state of the user's interaction with it. Dalvik heap, garbage collection and Outofmemory are key to game preformance Every Device has there own hard limit on heap for Apps ActivityManager.getMemoryClass() returns in megabytes this will help to dynamically manage. you can also choose this option. But yes, the application needs to release and manage resources effectively. In general, there are four states a process can be in based on the activities running in it.The system will kill less important processes (the last ones) before it resorts to killing more important processes (the first ones).
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Android avatar image
Android answered
This doesn't answer the questions I have: 1) After onDestroy() the process is still left running as a 'Cached Background Process'. What is exactly is being kept 'alive' at this point? 2) Why should this cause an OutOfMemoryException when I am setting all static references to null? 3) Is it acceptable to call System.exit(0) in the onDestroy() method? Thanks.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

tc-teegarcs avatar image
tc-teegarcs answered
Was an answer ever provided for this question? I am currently in the process of optimizing an Application for Kindle Fire use and this information is key to improving user experience. Is there better documentation on the Amazon life cycle and garbage collector?
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

tc-teegarcs avatar image
tc-teegarcs answered
Was an answer ever provided for this question? I am currently in the process of optimizing an Application for Kindle Fire use and this information is key to improving user experience. Is there better documentation on the Amazon life cycle and garbage collector?
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Android avatar image
Android answered
No, this question has not been fully answered.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered
Hi all, When an application starts, its own vm is started and the main activity is started. When that activity is left (eg. back button pressed or you call finish()) onDestroy() is executed. While that technically ends your application your application process and vm is still running. That includes running threads. So all static members remain intact. The system can kill the vm once it requires more memory at any time while it is not showing. Static variables are created and initialized when the class containing them is loaded into the VM by the class loader. When the class is unloaded or the VM ended, the static variables go "poof". There is generally no need to clear them. I suppose if you wanted to you could clear them in an onTerminate() method (in the application) or an onDestroy() method (in an activity), but there isn't much point in doing so, and there's no guarantee that either of those two methods will be called. If you're for some reason worried about the variables not being cleared when created (a serious violation of the VM's "contract"), you could clear them in an onCreate() method. Also calling System.exit(0) in the onDestroy() could be another way to solve the problem. [b]This behavior is unique in all generic Android devices and Kindles.[/b]
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.