1

Let's say we have a reference (strong reference) to an activity object.

What happens to Activity object after it was:

  • finished or
  • changed orientation or
  • put on pause or stopped because of switch to another activity

I am talking about a case when application wasn't killed by OS but when a user continue to use the application.

Will activity ever be released, if we continue to keep this strong reference? One one hand, as I understand, if somebody keeps a reference to an object it can't be released/deallocated. On other hand, Android is quite aggressive with releasing unused activity and I wonder whether Dalvik machine has some exception which will allow to release/deallocate an activity which is referenced.

These article doesn't talk directly about a case, when somebody is keeping a reference: http://developer.android.com/training/basics/activity-lifecycle/starting.html#Destroy http://developer.android.com/reference/android/app/Activity.html

There is only one hint which I got:

There is a method Activity.isDestroyed() which kind of implies that object won't go away immediately.

Victor Ronin
  • 22,758
  • 18
  • 92
  • 184
  • 1
    References to activities are not treated specially. In the situation you describe, the activity (and anything it references) would remain allocated. Do this enough times and eventually you will get an `OutOfMemoryError`. – Kevin Krumwiede Sep 15 '14 at 17:16
  • Do you want to past it as an answer? BTW. How do you know that? Is there any documentation or reference in AOSP to check that? – Victor Ronin Sep 15 '14 at 17:19
  • https://developer.android.com/tools/debugging/debugging-memory.html Just search for 'leaked Activity' – Morrison Chang Sep 15 '14 at 17:22
  • Woo-hoo :) Thanks. Again, please post it as the answer. Deserve an upvote. – Victor Ronin Sep 15 '14 at 17:29
  • Awareness of gamification strategies -> stopped caring about upvotes. – Kevin Krumwiede Sep 15 '14 at 18:38
  • @VictorRonin This feels like duplicate of http://stackoverflow.com/questions/13696621/check-and-avoid-memory-leaks-in-application or http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java so feel free to remove. – Morrison Chang Sep 15 '14 at 19:30

0 Answers0