Skip to main content
Inspiring
March 5, 2012
Question

Does System.gc() work on iOS?

  • March 5, 2012
  • 2 replies
  • 939 views

I'm trying to force garage collection in my iOS app, but am not convinced that System.gc() does anything.  I've read all the posts about how to use it, have also tried calling it twice:

System.gc();

System.gc();

However from testing it's not apparent that this has any effect.

Can anyone confirm whether or not garbage collection works on iOS?  If not, what are options for cleaning up?  Are there any?

This topic has been closed for replies.

2 replies

Colin Holgate
Inspiring
March 5, 2012

I have seen cases on iPad 1 where going from one demanding scene to another demanding scene could cause a crash. It could be that although the garbage collection was going to work, just for a moment there was too many things going on. So, I would System.gc() as I left one scene, giving the new scene a better chance. It seemed to stop the crashes.

sinious
Legend
March 5, 2012

Unfortunately iPad1 is a bit slow on releasing resources in my experience too. You'd have to expect it being a single core, previous generation CPU and GPU with less ram. I more than less get boggy transition animations rather than actual crashes.

I find adding a small delay between scenes really helps smooth things out. If you 'do something simple' between there it will give the user constant feedback of what's going on while you just use it to let the ipad1 clean up.

As long as you're disposing all objects properly, nulling out everything in sight, etc etc you shouldn't crash, as long as you stay within ram limits.

sinious
Legend
March 5, 2012

What you may consider garbage, iOS may not. Cache is still relevant at all times. As long as you are very careful about cleaning up all references to objects when you don't need them (including listeners, etc) then you don't need to worry about trying to force a garbage collection. Once iOS knows it needs more ram (play a video, load a large PDF, etc) it will perform its own GC. GC will also not solve any problems of failing to dispose of objects properly. 

If you have Flash Builder Premium with the profiling you can watch your app and you will probably notice when you call gc() it does nothing, until you do something else that requires that memory. Until iOS would rather cache it in memory for speedy recovery rather than dump it. This is very normal nature.