Attributes and memory management (attributes and memory management are related), and memory management is correlated.

Source: Internet
Author: User

Attributes and memory management (attributes and memory management are related), and memory management is correlated.

<Span style = "font-size: 18px;"> attributes and memory management (attributes and memory management are mutually related) Part 1. attributes: the attribute is a new syntax after OC2.0, used to replace the setter and getter methods. You can use the attribute to quickly create the declaration of the setter and getter methods, and implement the setter and getter methods, in addition, the setter method is used to safely process instance variable operations (which is implemented through memory management). The setter method is used to re-assign values to a single instance variable. The rule is as follows) no return value. The name starts with set and ends with the name of the instance variable to be set. This method has only one parameter. The parameter type is the same as the instance variable type. The getter method is used: obtain the value of a single variable. Standard: The (-) method has a return value. The return value type is the same as the type of the instance variable to be obtained, the name is the same as the name of the instance variable to be obtained. The non-parameter instance variable depends on the existence of the object, and the open space for the object is based on the size of the space of the instance variable, attributes The specific usage of semantic features is not described in detail here. The following will explain) major features: (1) readonly: tells the compiler that when an attribute is automatically generated, only the getter method is generated, the setter method is not generated (2). readwrite: tells the compiler that when the output method is automatically generated, both the getter method and the setter method must be generated. The second feature of the system's default read/write feature is: atomic features (1), atomic: atomic features to ensure thread Security, Internal Security Processing (locking and unlocking) (2), nonatomic: Non-atomic features, does not ensure thread security, because the getter and setter methods are frequently used, they may need to be accessed multiple times in a period of time. Using atomic will consume system resources and reduce program execution efficiency, although the use of nonatomic does not guarantee thread security, it is generally safe to use. Therefore, for atomic properties, the third major feature of nonatomic is: semantic features (1), assign: direct assignment, applicable to basic data types. Used for object types. The default semantic feature of the system (2). copy: applies to objects of the object type that comply with the NSCopying Protocol. A new object is copied, new object ownership, (reference count + 1) (this will be explained in detail for the moment), (3), retain: applies to object types, the reference count of the Object + 1; 2. Attribute definition @ property (keyword, used to define attributes) + attribute characteristics + attribute type (same as the instance variable type) + attribute name (same as the instance variable name); where @ property is in. in the H file, only the setter and getter methods are automatically declared. 3. in the m file, the implementation of the @ synthesize (keyword) attribute implements the setter and getter Methods automatically generated by the attribute. in the m file, @ synthesize is used to synthesize the attributes. The system will automatically synthesize the attributes. However, in this case, the instance variable operated by the system's default setter and getter methods is the _ attribute name.. The getter and setter methods in the m file do not write anything (1). If the specified instance variable is in. if this parameter is not defined in the H file, the system automatically generates the specified instance variable, but the generated instance variable is private, and the subclass cannot be accessed directly. If the instance variable wants to inherit the subclass, it must be in. instance variables are defined in the H file. if the implementation part of the attribute does not specify the instance variable to be operated internally, the system will automatically generate an instance variable with the same name as the attribute (2 ), if we have implemented the setter and getter methods, the system will not generate them automatically (3. in the m file, @ synthesize is used to synthesize the attributes. The system automatically merges the attributes, in this case, the instance variable operated by the system's default setter and getter methods is the _ property name. The following is an example of the Person class (1. @ interface Person: NSObject // In the hfile, the instance variables are defined here to allow subclass inheritance. If this parameter is not specified, the system generates private instance variables by default, (If NO subclass exists All can be left blank) {NSString * _ name; // name NSString * _ sex; // gender NSInteger _ age; // age NSString * _ job; // work} // define the attribute to declare the semantic feature of name as retain, the semantic feature of sex is declared as copy, and the semantic feature of job is declared as assign (assign by default in the system, do not write here) @ property (nonatomic, retain) NSString * name; @ property (nonatomic, copy) NSString * sex; @ property (nonatomic) NSInteger age; @ property (nonatomic) NSString * _ job; @ end (2), in. m file (write the internal implementation of setter and getter methods of different features in detail, so that you can clearly understand the generation such Code) before the implementation method, it is described that when the setter and getter methods are implemented, the self. + attribute name. because the write method is equivalent to calling itself, it will form an endless loop 1). When the name semantic feature is declared as retain, the setter and getter methods implement the setter method internally:-(void) setName :( NSString *) name {if (_ name! = Name) {[_ namerelease]; _ name = [name retain] ;}} getter method:-(NSString *) name {return [[_ nameretain] autorelease];} 2) When the sex semantic feature is declared as copy, the setter and getter Methods internally implement the setter method:-(void) setSex :( NSString *) sex {if (_ sex! = Sex) {[_ sexrelease]; _ sex = [sex copy] ;}} getter method:-(NSString *) sex {return [[_ sexretain] autorelease];} 3) when the job semantic feature is declared as assign, the setter and getter Methods internally implement the setter method:-(void) setJob :( NSString *) job {_ job = job )} getter method:-(NSString *) job {return_job;} 4). When the default semantic feature of age is assign, the setter and getter methods are internally implemented using the setter method:-(void) setAge :( NSInteger) age {_ age! = Age)} getter method:-(NSString *) age {return_age;} the reason for this writing is as follows: Memory Management Introduction 2, (1), memory management Introduction: 1, crash occurs in iOS applications. More than 90% of the causes are memory problems. In a project with dozens or even hundreds of classes, it is extremely difficult to find out memory problems. Understanding the common memory problems can reduce the chance of errors. 2. Memory problems are reflected in two aspects: memory overflow and abnormal wild pointers. Understanding memory management can help us improve program performance and greatly reduce debugging bug time. 3. memory management mechanisms are divided into three types: (1) garbage collection (gc): programmers only need to open up memory space and do not need to display and release code, the system determines which space is no longer used and recycles the memory space for further allocation. No code is required for the whole collection process, and the system automatically completes garbage collection. Garbage Collection Technology (2) is always used in Java Development. MRC (Manual Reference Count) Manual Reference Count: The development and release of memory are controlled by program code. Compared with garbage collection, memory control is more flexible and can be released in time when it is needed. programmers have high requirements and must be familiar with the memory management mechanism (3 ), ARCAuto Reference Count): the compiler feature of iOS 5.0, which allows users to only open up space without releasing space. It is not garbage collection! Its essence is MRC, but the compiler adds the released code to the programmer by default. 4. iOS supports two memory management methods: in ARC and MRC, use malloc and free to create and release heap memory. The heap memory can only be in use or destroyed state. In actual development, we may encounter that more than two pointers use the same memory. C language cannot record the number of memory users. The OC uses the reference counting mechanism to manage the memory. When a new reference points to an object, the reference counter increases progressively. When a reference is removed, the reference count decreases progressively. When the reference count reaches zero, this object will release the occupied Resources (2). Basic Principle of Memory Management: If you perform alloc, retain, and copy operations on an object, you will have the right to use the object, the object must be release or autorelease. that is, (who uses the + 1 operation, who needs to perform the-1 operation) 1. The OC can only use the following five methods to change the reference count: (1), alloc :( +) (corresponding to dealloc) to open up the heap memory space and change the reference count of the object from 0 to 1 (+ 1); (2), copy) the size of the opened space is the same as that of the object to be copied (that is, the space). The content stored in the space is the same, but the address is different, the reference count is from 0 to 1 (+ 1 operation, not counting the reference of the original object + 1 here, but adding 1 to the newly Copied object); (3), retain :(-Method) Use the reference count of objects corresponding to release + 1 (+ 1 immediately after the operation), (4), release :(-method) use the reference count of the object corresponding to the retain-1 (-1 immediately after the operation), (5), autorelease :(-method) the reference count of the object-1 (not immediately-1, will reference the count-1 at a certain time in the future). As long as an object uses the autorelease operation, the corresponding automatic release pool autoreleasePool {} is required. How the automatic release pool works: it will put the object declared as autorelease into the automatic release pool closest to it. When the automatic release pool is destroyed, it will send a release message to each object in the pool, therefore, the actual-1 operation using autorelease is not performed by autorelease or is performed by release. retainCount is used to obtain the reference count of the current object (for custom classes, we don't need to know about the system class because it has done a lot of internal operations), 3, dealloc: Reclaim space (corresponding to alloc) when the reference count of this type of object is 0, the system will automatically call the dealloc method to reclaim the space. This method does not need to be called manually. 4, when the reference count of an object is 0, you cannot access the object, that is, do not perform any operations on the object. If you continue the operation, a wild pointer problem will occur, at this time, the system may suddenly crash when writing a line of code (even if it does not write any Code related to the reference count), because the system has automatically reclaimed the space pointed to by the pointer variable, the variable has no permission to use the space to be withdrawn, so you cannot access objects without permission. 5. Find the line of code when the system crashes due to the wild pointer problem: on the menu bar, click Product-> Scheme-> Edit Scheme-> select Enable Zomible Object and the last row under Debugger in line Objective-c, and run the command again to find the error. 6. After writing the Code related to the reference count, the program crash immediately because the reference count is used after the system is recycled- 1. delete the related operations. 7. We need to keep the current reference count of the object after the object at any time to reduce the error probability. 8, verify whether the object space is recycled. You can only check whether the dealloc method of this class is executed. The following describes the setter and getter methods using the Person method as an example, the internal operation is to assign values to instance variables and to instance variables. To operate instance variable 1 in all methods, when the name semantic attribute is declared as retain, setter and getter internal implementation setter Methods:-(void) setName :( NSString *) name {if (_ name! = Name) {[_ namerelease]; _ name = [name retain] ;}} getter method:-(NSString *) name {return [[_ nameretain] autorelease];} (1), if (_ name! = Name) Purpose: To determine whether the original object and the new object are the same. If the same object is the same, there is no need to re-assign the value; otherwise, the system first release, after release, the space will be reclaimed by the system. If retain is retained, the wild pointer problem will occur. (2) [_ namerelease] the purpose of the operation is to release the ownership of the previously preserved object, if the instance is not released, the memory will be leaked because the previous object is no longer used (3). The _ name = [name retain] operation aims to keep the instance variables with new object ownership, retain solves the wild pointer problem 2. When the sex semantic feature is declared as copy, the setter and getter Methods internally implement the setter method:-(void) setSex :( NSString *) sex {if (_ sex! = Sex) {[_ sexrelease]; _ sex = [sex copy];} For setter method (1), if (_ sex! = Sex) Purpose: To determine whether the original object and the new object are the same. If the same object is the same, there is no need to re-assign the value; otherwise, the new object will be release first, after release, the space will be reclaimed by the system. If retain is retained, the wild pointer problem will occur (2), [_ sexrelease]; purpose: to release the ownership of the previously preserved object, if the object is not released, the memory will be leaked because the previous object is no longer used (3), _ sex = [sex copy]; The copy operation here is different from the retain operation above, copy refers to copying the space that sex points to (that is, re-opening a space. The size is the same as the size of the space that sex points to, and the data stored in the space is the same: let the instance variable retain the new object ownership, retain solves the wild pointer problem, getter method:-(NSString *) sex {return [[_ sexretain] autorelease];} 3, setter and g The setter method is internally implemented by the etter method:-(void) setJob :( NSString *) job {_ job = job)} Because the semantic feature of the job is assign, therefore, the internal operation is to assign values directly to the getter method:-(NSString *) job {return_job;} 4. When the default semantic feature of age is assign, setter and getter Methods:-(void) setAge :( NSInteger) age {_ age! = Age)} Here, age is the basic data type, and the space opened for it is in the stack area. Except for the heap area, manual management is required, and other memory areas are all managed by the system, the default semantic feature of the basic data type system is assign, so the internal operation is also the direct assignment method getter method:-(NSString *) age {return_age;} </span>


My computer Property Memory is different from the Resource Manager memory?

The number you see in the Task Manager represents the number of memory on your system page (virtual memory). This value is freely allocated based on the number of physical memory, for example, your memory is 512 MB, the recommended virtual memory is about 1 GB. M is your physical memory, that is, the size of the memory stick.
The virtual memory can be set in the "my computer" right-click "attribute" -- "advanced" -- "performance" -- "advanced" option.

Why is the memory in the Task Manager different from that in the task manager when right-clicking my computer properties?

The property shows available physical memory, which is the available value after the memory used by the video card is deducted. It is generally equal to the total memory capacity of your memory, except that you are using an integrated video card, some memory is used
The task manager adds physical memory and virtual memory. The virtual memory is a part of the capacity in the hard disk partition. It is used to buffer and speed up reading. The cache has a positive effect on hard disk life.

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.