OC memory management summary, clear and clear !, Oc Memory Management Summary

Source: Internet
Author: User

OC memory management summary, clear and clear !, Oc Memory Management Summary

<Span style = "font-size: 18px;"> OC memory management I. Basic Principles (I) Why should I perform memory management. Because the memory of mobile devices is extremely limited, the memory occupied by each APP is also limited. When the app occupies a large amount of memory, the system will issue a memory warning, in this case, you need to reclaim some memory space that you do not need to continue using, for example, reclaim some objects and variables that are no longer in use. Management scope: any object that inherits NSObject, which is invalid for other basic data types. The essential reason is that objects and other data types have different storage spaces in the system. Other local variables are mainly stored in the stack, while objects are stored in the heap, when the code block ends, all the local variables involved in the code block will be recycled, and the pointer to the object will be recycled. At this time, no pointer is directed to the object, but it still exists in the memory, memory leakage. (2) Basic Structure of the object each OC object has its own reference counter, which is an integer that indicates the number of times the object is referenced, that is, how many items are currently using this object. When an object is created, the default counter value is 1. When the counter value is 0, the object is destroyed. Each OC object has a 4-byte storage space to store reference counters. (3) the only basis for determining whether an object needs to be recycled by referencing a counter is whether the counter is 0. Otherwise, the counter exists. (4) send messages to objects and perform corresponding counter operations. Retain message: Make counter + 1, change the method to return the Release message of the object itself: Make counter-1 (does not represent the Release object) retainCount message: Get the current reference counter value of the object (5) object destruction when the reference counter of an object is 0, it will be destroyed and the memory occupied by it will be recycled by the system. When an object is destroyed, the system automatically sends a dealloc message to the object. The dealloc method is rewritten to release related resources. dealloc is like the last words of the object ". Once the dealloc method is rewritten, you must call [super dealloc] and put it at the end of the code block (the dealloc method cannot be called directly ). Once the object is recycled, the storage space occupied by it will no longer be available. sticking to it will cause the program to crash (the wild pointer is incorrect ). Ii. Related Concepts and Usage Note: Incorrect wild pointer: access to a bad memory (recycled, unavailable memory ). Zombie object: the occupied memory has been recycled, and the zombie object cannot be used again. (Detect zombie objects) null pointer: no pointer pointing to anything (stored items are 0, null, nil). No error is reported when messages are sent to null pointers: [p retaion] cannot be used to restore dead zombie objects. Iii. Memory Management Principle (I) As long as someone else is using an object, this object will not be recycled; as long as you want to use this object, then, the Reference Counter of this object should be set to + 1; when you do not want to use this object, the Reference Counter of the object should be set to-1; (2) who created and who release (1) if you create an object through alloc, new, and copy, you must call the release or autorelease method (2). If it is not created by you, you will not be responsible for it (3) if you call retain, no matter how the object is generated, you must call release (4). Conclusion: There is a beginning and end, and there should be a reduction in addition. If you used to add 1 to an object counter, you should add it to the last-1. iv. Memory Management Code specification (1) as long as alloc is called, the release (autorelease) (2) code specification of the Set Method (1) Basic data type: copy directly-(void) setAge :( int) age {_ age = age;} (2) OC object type-(void) setCar :( Car *) car {// 1. first, determine If it is the new object If (car! = _ Car) {// 2 perform a release [_ car release] on the old object; // if there is no old object, no effect is made. // 3. retain_car = [car retain] ;}} (3) code specification of the dealloc method (1) be sure to [super dealloc] and put it at the end (2) perform a release operation on other objects owned by self (current)-(void) dealloc {[_ car release]; [super dealloc];} 5. @ property parameter (1) memory management related parameter Retain: Old value of release and new value of retain (applicable to OC object type) Assign: direct value assignment (default, applicable to non-oc object types) Copy: release old value, copy new value (2) whether to generate the set Method (if it is a read-only attribute, It is not generated) Readonly: Read-only, only the getter statement and Readwrite implementation will be generated: the default one will be generated at the same time. Description and implementation of setter and getter (3) multi-thread management (Apple shields multi-thread operations to a certain extent) Nonatomic: high performance, generally using this Atomic: low performance (4) the Set and get methods are used to modify the names of the set and get methods. The returned Boolean method name generally starts with "is", and the modified name is generally used in the getter Of The boolean type. @ Propery (setter = setAbc, getter = isRich) BOOL rich; BOOL B = p. isRich; // call 6. Cyclic reference problem in memory management and solution case: each person has an ID card, each ID card corresponds to a person, and the # import method cannot be used for mutual inclusion, this forms a circular reference. New keywords: @ class name; -- Solve the Problem of loop reference and improve the performance. @ class only tells the compiler to process the subsequent name as a class during compilation. (1) @ class: declares a class and tells the compiler that a name is a class. (2) references the specification of a class in development. (1) In. in the H file, use @ class to declare class 2) in. when the m file is actually about to be used, use # import to include everything in the class. (3) Use retain at one end of the solution for circular reference at both ends, one end uses assign (dealloc does not need to be release if assign is used) VII. Autorelease (1) basic usage (1) puts the object in an automatic release pool (2) when the automatic release pool is destroyed, a release (3) will be performed on all objects in the pool, and the object itself will be returned (4) After the autorelease method is called, the counter of the object is not affected (impact upon destruction) (2) benefits (1) No need to care about the time when the object is released (2) No need to care about when to call release (3) use note (1) objects that occupy a large amount of memory, do not use autorelease, use release to precisely control (2) objects that occupy a small amount of memory use Utorelease does not have much impact (4) incorrect syntax (1) Multiple autorelease calls in a row and two release executions (-1?) when the release pool is destroyed ?) (2) After Alloc, autorelease is called and then release is called. (5) automatically release the pool (1) when the ios program is running, countless pools will be created, all of which exist in the stack structure (Advanced and later. (2) When an object calls autorelease, it is placed in the release pool located at the top of the stack (6) method of creating an automatic release pool (1) the method used before ios 5.0 to create the nyothoreleasepool * pool = [[nyothoreleasepool alloc] init]; ''' [pool release]; // [pool drain]; used for mac (2) @ autoreleasepool after Ios5.0 {// start to create an automatic release pool...} // end to destroy the automatic release pool (7) Autorelease note (1) in the methods provided by the system, if alloc new copy is not included, all objects returned by these methods are autorelisted, such as [NSDate date]; (2) during development, some class methods are often written to quickly create an autorelease object. When creating an object, do not directly use the class name, but use the self 8 and ARC memory management mechanisms. (1) ARC Judgment Principle: as long as there is no strong pointer to the object, the object will be released. (2) pointer classification: (1) strong pointer: by default, all pointers are strong pointers, and the keyword strong (2) Weak pointer: the Pointer Modified by the _ weak keyword declares a weak pointer as follows: _ weak Person * p; in ARC, as long as the object pointed by the weak pointer is no longer present, the weak pointer is directly cleared. _ Weak Person * p = [[Person alloc] init]; // The object is released when it is created. After the object is released, the ARC automatically clears the pointer. In ARC, the retain is no longer used at the property, but strong is used. In dealloc, [super dealloc] is not required. @ Property (nonatomic, strong) Dog * dog; // indicates that the generated member Variable _ dog is a strong pointer, which is equivalent to the previous retain. If it is replaced by a weak pointer, it is replaced by weak without adding __. (3) Summary of features of ARC: (1) events such as release, retain, and retainCount (2) events not allowed to override dealloc, but [super dealloc] (3) Events Not Allowed) @ property parameter: Strong: equivalent to the original retain (applicable to the OC object type), the member variable is a Strong pointer Weak: equivalent to the original assign (applicable to the oc object type ), the member variable is a weak pointer Assign: applicable to non-OC object types (basic types) (4) Supplement to make programs compatible with the ARC and non-ARC parts. Transition from non-ARC-fno-objc-arc to ARC-f-objc-arc. ARC also needs to consider the circular reference problem: one end uses retain and the other end uses assign. Tip: the string is a special object, but it does not need to be manually released using release. This string object is autorelease by default, and no extra memory is needed. </Span>


How can I improve my speech ability? For example, if I want to describe one thing, how can I give a simple and clear description to the other party?

I teach you: I rely on it! (Although there are only two words in this reply, it deeply expresses the deep blessings and deep feelings of the respondent. It is concise and concise, with thousands of gold in a word, it can be seen that the author has a solid writing skills and creative skills. I really admire it! In addition, I ended with an exclamation point, reached my eye, and gave a wonderful life. I took care of the previous article and sublimate the topic. I expressed my feelings to the fullest, and I felt deeply touched and disappointed, in fact, it is the best in the reply, and the best in the blessing .)
 
Who has a graduation internship diary or clear report on interior decoration?

This is the first time that we have officially started our job in line with the society and started a completely different life. Every day at the specified time to go to and from work, you must complete your work on time and conscientiously during work, do not rashly perfunctory. We start to shoulder our responsibilities and take care of everything. Otherwise, we may have to pay a huge price for the serious consequences of a small mistake at any time. It is no longer a word of sorry and a letter of apology.

I have gained a lot from my internship. It is necessary to summarize it after the internship. As a student who has just got in touch with professional knowledge, it is unscientific to have direct access to profound professional knowledge before learning professional courses, this internship allows us to gain a perceptual knowledge of our upcoming major in practice, laying a solid foundation for future study of professional courses.

The internship task mainly refers to some practices, such as drawing, simple cost and off-site construction. First, let me take a look at the drawings. To better understand the designer's design principles, I read the gallery in the office and conducted in-depth research on the drawings, in this process, I also raised a lot of insightful questions. I have had a heated discussion with everyone, and I want to understand everything I don't understand. Because I have to work after my internship, I can't make a reasonable cost because I don't understand the picture, so it is really important to look at the picture. During the internship, we conducted budget internships for the electrical installation of mobile communication business offices such as Suning Weinan Middle Road, government departments, Hebei branch, and Hebei construction service center.

During the internship, we also went off to the construction site for practice. The Hebei Construction Service Center is located at Xinhua West Road in Shijiazhuang, not far from my internship company, so it created favorable conditions for my internship at the construction site, on the construction site, I learned the contents of piping and pipe threading, installation of sockets, cable tray wiring, line direction, and so on that I cannot fully understand on the drawings, they also felt the hard work of the workers and their orderly work arrangements. I learned a lot of what I could not learn in books. Fan Wen China, original Fan Wen Park

"The heroes of the world are all my generation, and they immediately rush into the rivers and lakes ." From the change of the school environment to the social environment, people who are in contact with each other have completely changed their roles, teachers become bosses, and classmates become colleagues. The way they get along is totally different. In this huge change, we may be confused and cannot adapt to the new environment immediately. We may not get used to the brutal competition between companies, and we cannot stand the indifference of our colleagues. Most of the time, I feel that I have not been reused by leaders. What I do is just a few irrelevant chores. My proposal or work cannot be affirmed by the boss. When you fail to make a score, there will be pressure from all aspects, and the boss's colleagues will be ridiculed. In school, some students care and support it silently. They attend classes every day and it is easy. As the saying goes, I have learned a lot about being a person, doing things, and doing things, even though I have been working for less than a decade for a month.

Experience:

The practice mainly promotes our ability to work and business in the future, enhances our competitiveness in the future, and adds a cornerstone for our future foothold. This internship enriched my budget knowledge and made me move towards a deeper level, which will promote my future social development. However, I also realized that, if you want to do a good job in this area, you will not be able to do it over the past few days. You still need to enrich your experience by accumulating 1.1 points in my daily study and work. The road ahead of me is still very long, and it takes constant efforts and hard work to make it really better. I firmly believe that my internship and practical experience have benefited me for my life and will be continuously verified in my practical work after graduation, I will continue to understand and understand the knowledge I have learned during my internship. In my future work, I will continuously apply the theoretical knowledge and practical experience I have learned to practical work, fully demonstrates your personal and life values.

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.