Neoh
Neoh

Reputation: 16164

Which type of data can be garbage collected when app is in background?

Let's say I start from activity A -> activity B. While in activity B I press home to exit. After a long time, gc may be called because other apps take higher priority. My question is, which of the following type of data will be garbage collected (I'm pretty sure static fields can be garbage collected any time but I'm not sure about these):

i) fields that are declared final or static final

ii) intent and its data that I passed from activity A to activity B

iii) onSavedInstanceState when orientation is changed during the app running

I ask this because I want to ensure that my app won't crash when I restore activity B from background after a long period.

Upvotes: 1

Views: 1845

Answers (4)

Droid Teahouse
Droid Teahouse

Reputation: 1013

This thread is old but does not address a very important point from: https://developer.android.com/topic/performance/memory#release

Android can reclaim memory from your app in several ways or kill your app entirely if necessary to free up memory for critical tasks

at any time.

Killing your app process means it restarts which implies that if you have not saved any state into the bundle, db, or sharedprefs, it will be re-initialized. There are also the ComponentCallbacks2interface callbacks to listen for low memory events.

.

Upvotes: 0

Patrick
Patrick

Reputation: 35234

Static variables cannot be elected for garbage collection while the class is loaded. They can be collected when the respective class loader (that was responsible for loading this class) is itself collected for garbage. (see this answer)

Other than that your object with every instace variable can be gc if you design your app correctly and do not hold onto references of Activities, Fragments, etc. yourself. But don't worry about that the Android lifecycle takes care of that for you, it will provide the an activity that was destroyed/gc with your bundl that you have saved onSavedInstanceState onCreate() and so on, so you can retrieve that state you had before going to background

Upvotes: 2

Alex MDC
Alex MDC

Reputation: 2456

I think you are confusing the concept of garbage collection with the component lifecycle that Android provides.

Garbage collection will only free objects sitting in memory if they are no longer reachable, that is no more strong references to them exist. If you are using finalizers to trigger behavior when an object is collected then there is you are doing something wrong.

Meanwhile, the component lifecycle is managed by the Android OS in a determinate fashion - when it wishes to kill an activity (or service, or application) then it will call some methods on that component (e.g. onDestroy()) and that's it - once Android has destroyed it then it should be gone from your perspective to.

The only difficulty comes in when you wish to start saving and restoring activity state - then it becomes important to you to manage a few things when your activity is being paused/stopped/destroyed. Again, these are fully determinate events and unrelated to garbage collection.

To answer your questions specifically:

i) it doesn't matter how fields are declared. If you have a reference to an object then you automatically have a reference to all of its fields, meaning they won't be garbage collected.

ii) when you create the intent, do you keep a reference to it? If not, then it may be garbage collected, but this should not bother you because you don't have a reference to it anyway :)

iii) saved instance state will be kept by the Android system and given back to you when the activity is recreated. You should not keep your own reference to it.

Hope that helps!

Upvotes: 1

SpongeBobFan
SpongeBobFan

Reputation: 964

Your activity can be garbage collected and recreated again when restoring from background. If you want to save some data, use SharedPreferences or other persistent storage.

Upvotes: 0

Related Questions