Objective-C 2.0 garbage collectionIs the content to be introduced in this article, mainly to learnObjective-C 2.0 garbage collectionFor more information, see this document.Objective-C 2.0The biggest possible enhancement isGarbage CollectionGarbage Collection ). And"Garbage Collection"Corresponds to the traditional Reference Count) memory management form.
The use of garbage collection means that in most cases, you do not need to do the work described in Very simple rules for memory management in Cocoa>. The system will automatically help you manage these problems. If you have not read Very simple rules for memory management in Cocoa>, read it first. Otherwise, you may not know what garbage collection is about.
In addition, the iPhone operating system currently does not support garbage collection. If you want to develop an iPhone or migrate your program to the iPhone in the future, try not to use garbage collection in the program.
What is garbage collection?
In short, garbage collection can be considered as "Automatic Memory Management", which means that objects in memory that are no longer used will be called "garbage ", the purpose of garbage collection is to help you automatically release these "spam" to avoid excessive unnecessary memory occupation by the program.
The traditional memory management form has already been introduced on this site. The so-called garbage collection is mainly used to solve the memory leakage problem. When you create an object manually without releasing it when you do not need it, it is saved in the memory. If many objects are created, a large amount of memory is required. A small amount of memory leakage is not a big problem, but when there are too many memories not released, there will be a lot of problems, such as slow program running and high memory usage. If your program is running continuously and these objects are not released when they are not needed, they will slowly "Eat up" your memory.
Garbage collection is especially useful for people who do not know how to manage memory, but have many good ideas and can write certain programs. Because they can ignore the troublesome memory management and focus most of their time on how to implement a good idea that can run.
How to Use garbage collection?
Open the garbage collection option in the Project Settings ,.
Description
The following describes several key points about Objective-C 2.0 garbage collection (GC.
1. Because the garbage collection package is included in the Objective-C 2.0 runtime Library, if your program uses garbage collection, it can only run on Leopard and later versions.
2. By default, XCode disables GC. To enable GC, You need to modify the Build option of the project.
3. The gcc parameter for enabling garbage collection is-fobjc-gc.
4. Core Foundation objects also support garbage collection
5. After garbage collection is enabled, retain, release, and autorelease will no longer work. However, if your program includes these memory management methods, opening GC will not affect your program.
6. If your Objective-C program and C ++ coexist, you can choose not to consider memory release when using Objective-C, however, in C or C ++, you must consider the issue of memory release.
7. Use the-finalize method if an object needs to be released when garbage collection is enabled.
8. Apple suggests not to support garbage collection and traditional reference counting at the same time, although the compiler allows this.
Benefits of garbage collection:
1. For a lazy or beginner, you don't have to worry about whether the object is released or when it will be released. This means you can save more time and reduce the possibility of errors and memory leaks.
2. Over time, more Java or other developers may be transferred to Objective-C development due to reduced workload and learning threshold.
3. programs using the old reference counting memory management method will not be affected because GC is enabled.
4. If a multi-threaded program is developed, it is easier to manually manage the memory than previously.
Disadvantages of garbage collection:
1. If you want to make the program compatible with Tiger, you cannot use garbage collection, or write the program in the form of reference count.
2. Possible performance degradation. If you have used Java, you may have some knowledge about the impact of garbage collection on performance. However, this is only one of the disadvantages, because in most cases, you will not feel the slow speed of GC programs.
3. If you want to save an object for a long time without being released, you may need to use it as a global variable.
4. As mentioned above, the iPhone does not support garbage collection. If you want your app to be compatible with the iPhone in the future, do not consider using garbage collection.
Summary: AboutObjective-C 2.0 garbage collectionThe introduction is complete. I hope this article will help you!