I\'m working on a major update to one of my applications and trying to cut down memory usage and making it cleaner and faster. I was using Instruments to profile the app and
The possible reason here is because of your Settings view controller was not deallocated (not released from memory). No matter how many images are in that view controller when view controller release from memory it will deallocates (remove) all image object from memory.
Steps:
Click on this button will start to profile your application.
Select Allocations section in Instruments window. There is a textfield above the allocation listing view. Click on that field and write your view controller name. (In your case SettingViewController).
I'll try to explain some way which I know to de allocated controller.
[ 1 ] Override dealloc method in your view controller and release objects in that. Mainly mutable variable and delegates. Put a debug breakpoint on -dealloc
method and make sure it is called when you left that controller.
Note: If you have create global variable for class like UIPickerView
, UIPopoverController
, UIImagePickerController
etc. and set delegates for that than these delegates must be nil
in -dealloc
method.
e.g.
Objective C code:
//---------------------------------------------------------------
#pragma mark
#pragma mark Memory management methods
//---------------------------------------------------------------
- (void) dealloc {
[self.tableView setDelegate:nil];
[self.tableView setDataSource:nil];
}
Swift code:
deinit {
self.tableView.dataSource = nil
self.tableView.delegate = nil
}
[ 2 ] Global object of subclass also needs to override -dealloc
method and release relevant objects and delegates. Similar their dealloc method must have to be called.
e.g.
Check this scenario: Let's say you have created a subclass of UIView
(or any other view) with the name MenuView
. And you have created a global variable of this class in your view controller, than it's dealloc
method will be called before view controller's dealloc
method.
@property (nonatomic, strong) MenuView *menuView;
So here MenuView
class needs to be override the -dealloc
method and must be called before your view controller's dealloc
method is called.
So that before leaving whole container its child views are release and removed from memory.
Check this in Instruments for MenuView
class also that object is still their or not?
If it's dealloc method is not called than object of MenuView
class is still in memory and your view controller class is referencing that object. So even if your view controller's -dealloc
is called it will not deallocated because MenuView
class object is live.
[ 3 ] You need to set weak
reference to the IBOutlet
. e.g.
Objective C code:
@property (nonatomic, weak) IBOutlet UITableView *tableView;
Swift Code:
@IBOutlet weak var tableView: UITableView!
Feel free to ask if anything is not clear.
Dont use the image asset... Copy the images to you a folder into your project an load it by code with imageWithContentOfFile it works to me
Try to use imageWithContentsOfFile
to load image.
As rmaddy said,imageNamed
will let your image stay in cache,this is the reason for your memory problem