0

I have a couple of services and activities in my application.

When the onDestroy() method is fired, I currently set all my variables to null. Does this free up memory? Or does Android do that anyway?

Also, what about setting Threads and Runnables to null, should you even do that?

Once the user has pressed/tapped "Done" in the activity, I call finish() on the activity, which then fires the onDestroy() and I set variables to null.

However, when I go to the Application Manager > Running Services, it shows my application is using like 20-25MB of RAM, does Android still keep the activities, even when you call finish() on them?

PS: When I use a task-manager to kill it, then the memory usage goes down to 4-7MB.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user959631
  • 1,004
  • 2
  • 14
  • 34

2 Answers2

1

The only thing that should be left in memory are your services if you didn't unbind them but even then most Services can destroy themselves if no tasks are being performed. That is if it's the Service class or any of its kind you're talking about.

You do not need to null your variables in onDestroy, onDestroy handles that for you

Pontus Backlund
  • 1,017
  • 1
  • 10
  • 17
  • Ok, could please explain to me why the `Application Manager` shows it's using 20MB+, even though `onDestroy()` is called? – user959631 Dec 19 '13 at 01:21
  • I would need to see your code for this, I'm not a magician ;) also you can check logcat to see what's happening – Pontus Backlund Dec 19 '13 at 01:23
  • @user959631 check this video https://www.youtube.com/watch?v=_CruQY55HOk for memory management. – Raghunandan Dec 19 '13 at 01:24
  • @PontusBacklund Haha, yeah I know, I'm just curious as to why this is happening, I do have an image (splash), so it may be that. (I will have to have a look at that, didn't really think of it). I'll watch the video that Raghuandan posted, and will come back here, if need be, I am going to sleep now though, it's getting really late for me =]]. I didn't know I would get such a quick response, I love StackOverflow (and the commuunity)!! PS: Your name sounds a little like a magician xD – user959631 Dec 19 '13 at 01:28
  • I have a `Context` variable that I hold, until `onDestroy()` or `onLowMemory()` is called, which then I set the `Context` to `null`. Would that cause a memory leak? I watched the video, but I can't seem to understand it =[[ – user959631 Dec 20 '13 at 19:26
  • @PontusBacklund Ok, I have a weird one, I can't seem to figure out, I have my activity, in it's `onCreate()` I call `setContentView(R.layout.splash_activity);` and I get a `GC_CONCURRENT`. My layout file has a `TextView` and an `ImageView`. Why am I getting that leak as soon as the Activity is loaded?!!?!?!? I am getting so frustated -.- – user959631 Dec 21 '13 at 00:35
  • It's not really a leak, GC means garbage collection and CONCURRENT means that it's collecting, just means it's collecting garbage so it doesn't need to increase the heap size. These should try to be kept to a minimum but they are impossible to avoid. `GC_FOR_MALLOC` means that the program needs more memory and allocates more, this one you should try to avoid – Pontus Backlund Dec 26 '13 at 14:38
0

If you use Bitmap avaiables, you must recycle it

hungkk
  • 338
  • 3
  • 11
  • If I set `null` to a variable, will it be collected? For example, if I have this sub-routine: `private boolean isGpsEnabled(){ LocationManager gps = (LocationManager) oContext.getSystemService(Context.LOCATION_SERVICE); return gps.isProviderEnabled(LocationManager.GPS_PROVIDER); }` do I need to set `gps` to null? Or at the end of this method, it's collected? Thanks – user959631 Dec 20 '13 at 19:41