简体   繁体   中英

iOS - ARC/Memory management issues with ModalViewControllers

I'm writing an app (in iOS 5 using ARC!) that presents several hundred objects within a custom UIViewController I've written that a user may scroll through and select, each of which is presented as a thumbnail image that the user may touch.

Each of these objects has associated with it a special UIViewController subclass that handles the custom presentation of the information associated with that object. For example, it might be an image that can be zoomed and panned, or simply some text that needs to be formatted.

Now, each of my image objects has a large image associated with it. Using the Instruments app to profile my code and running Activity Monitor, I see that these take up roughly 5-10MB each when the custom UIViewController subclass loads and the image is displayed. This is fine for a few images, but eventually my app takes up too much memory and crashes.

I have written in my subclass's -viewDidUnload method apparently all of the things necessary to tell ARC to free up this memory, but no memory is being freed until warnings are issued, which typically occurs when the app is about to crash. A couple of times, I've noticed that if I get close enough to the threshold but don't go over it, the UIViewController subclasses that I've previously viewed are eventually flushed from memory, although the -viewDidUnload method is not apparently called (rather, the -didReceiveMemoryWarning message is sent to my custom UIViewControllers ). However, most often, my app crashes as it runs out of memory.

So my core question is, should I assume that an object will be disposed of by ARC as soon as possible, or does it always wait until space becomes tight? The behavior I want is for the custom view controller and its data to be flushed right away, so that memory never becomes an issue.

I do not believe there are any strong references from objects to my custom view controllers, and I instantiate them in my primary view controller using this code:

[self presentViewController:[cObj grabModalViewController]
                   animated:YES completion:nil];

where cObj is a custom class with the object's information. grabModalViewController simply instantiates an object of the correct type and returns the pointer, so presumably the local reference to the object should be trashed as soon as the method completes.

As a result, I would expect that, when I later call

[self dismissModalViewControllerAnimated:YES];

the custom view controller that was only pointed to be the primary view controller via presentedViewController should be flushed from memory, but this does not happen.

Could this be because I have strong references within my custom view controller object?

Essentially what I'm trying to accomplish is to avoid as many memory warnings as possible by managing things better up front, but perhaps this is not the right attitude.

I'd appreciate any suggestions and I'm happy to post any and all code that would be helpful.

So my core question is, should I assume that an object will be disposed of by ARC as soon as possible, or does it always wait until space becomes tight?

ARC is a compile time technology. It knows nothing about your runtime memory heap. Hence, it cannot wait to release items until space becomes tight. A specific answer to your question is that ARC will release anything as soon as you no longer need it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM