1
votes

How can you effectively free the memory of a modal viewcontroller with ARC ?

I saw a few topics on the subject, but I actually didn't find any answer to what seems like a common problem when dealing with modal view controllers.

I made a sample application in which viewcontroller1 presents modally viewcontroller2. When I dismiss viewcontroller2 (from viewcontroller1), the memory is not released ! :( (see memory log below)

Strange : there are no strong references of viewcontroller2 in viewcontroller1, and viewcontroller2 dealloc is indeed called ...

The viewcontrollers are presented/dismissed using presentModalViewController/dismissModalViewControllerAnimated

See my memory Usage :

ViewController1 Displayed : 7.41 Mb Usage
--ACTION : Show ViewController2--
ViewController2 Displayed : 8.11 Mb Usage
--ACTION : Dismiss ViewController2--
ViewController1 Displayed : 8.06 Mb Usage
--ACTION : Show ViewController2--
ViewController2 Displayed : 8.11 Mb Usage
--ACTION : Dismiss ViewController2--
ViewController1 Displayed : 8.06 Mb Usage
--ACTION : Show ViewController2--
ViewController2 Displayed : 8.11 Mb Usage
--ACTION :Dismiss ViewController2--
ViewController1 Displayed : 8.06 Mb Usage

Thanks

1
How are you calculating memory usage?Abdullah Umer
I use this : struct task_basic_info info; mach_msg_type_number_t size = sizeof(info); kern_return_t kerr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size); double sizeInMegaBytes = (double)info.resident_size / 1000000; Dev Oue
it's the same value as the one in Instruments -> Activity Monitor -> Real Memory UsageDev Oue

1 Answers

0
votes

The dealloc in viewcontroller2 generally releases the variables and resources allocated in that class and not the view controller itself. Also when you use ARC, the memory is handled automatically and may not be released when you want it to be. If you do not use ARC, you have complete control to manage the memory, which is something most of us do even today. Dont worry it will release ur object automatically. else you can search for something like the finalizer() method used in java to send ur object for garbage collection (although ARC is diff then Garbage collection).