Getting Started with iOS development: the use of performance-optimized –autorelease

Source: Internet
Author: User

Releasing objects in MRR is implemented through release or autorelease messages, which immediately frees reference count-1, sending autorelease messages to put objects in a memory-free pool for deferred release, and the object's reference count does not really change. Instead, a record is added to the memory-free pool until all objects in the pool are notified to send the release message and the reference count is actually reduced before the pool is destroyed.

Because it will delay the release of the object, do not use Autorelease to release the object, unless it is necessary, the release of the default memory release pool in the iOS program is at the end of the program, the application entry MAIN.M code:

int main (int argc, char *argv[]) 
     
{ 
     
@autoreleasepool {return 
     
uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Appdelegate class])); 
     
} 
     

Code is wrapped in @autoreleasepool {...} Between, which is the scope of the pool, the default is the entire application. A memory leak can also occur if a large number of objects are generated with Autorelease release. So when does autorelease have to be? Let's look at the following code:

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath { 
     
static NSString *cellidentifier = @ "Cellidentifier"; 
     
UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier]; if (cell = = nil) {cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:celli 
     
Dentifier] autorelease]; 
     
Nsuinteger row = [Indexpath row]; 
     
Nsdictionary *rowdict = [Self.listteams objectatindex:row]; 
     
Cell.textLabel.text = [rowdict objectforkey:@ "name"]; 
     
NSString *imagepath = [rowdict objectforkey:@ "image"]; 
     
ImagePath = [ImagePath stringbyappendingstring:@ ". png"]; 
     
Cell.imageView.image = [UIImage Imagenamed:imagepath]; 
     
Cell.accessorytype = Uitableviewcellaccessorydisclosureindicator; 
     
return cell; }

The cell object cannot be immediately release, and we need to use it to set the table view screen. Autorelease is generally applied to methods that provide objects to other callers, where the object cannot be released immediately, and needs to be deferred.

In addition, there is a situation where the autorelease is used, which is referred to as the "class-level construction method":

NSString *message = [NSString stringwithformat:@ "You have selected the%@ team. ", Rowvalue];

The ownership of this object is not the current caller, but it is put into the pool by the iOS system by sending autorelease messages, which are not visible to developers, and we should also be careful to reduce the use of such statements.

This article is from the "Dongsheng" blog, please be sure to keep this source http://2009315319.blog.51cto.com/701759/1132754

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.