Garbage Collector in Cocoa Programming in Mac
By: Aaron Hillegass
As long as you use only Objective-C objects, the garbage collector will do exactly what you want, without your needing to think about it. However, as soon as you start to malloc C data types and Core Foundation structures, you will need to be a bit more circumspect.
When the garbage collector runs, it is looking for unreachable objects. You can think of the objects in your applications as a directed graph: This object knows that object; it knows those objects; and they know those other objects. So, the garbage collector starts with the pointers on the stack and the global variables and wanders this directed graph until it has recorded every "reachable" object. The unreachable objects are deallocated. See Figure below.
Figure: Reachable Objects
Before an object is deallocated by the garbage collector, it is sent the finalize message. Overall, you shouldn't need to implement finalize very often, but if you do, it looks like this:
- (void)finalize { ...Do some last minute stuff here... [super finalize]; }
Archived Comments
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Listing of files in a tar file in Mac OS X.
Listing of files in a zip file in Mac OS X.
Multithreading versus Multiprocessing in Mac OS X.
Using NSOpenGLView in Cocoa Programming in Mac
Garbage Collector in Cocoa Programming in Mac
Weak References in Mac Cocoa Programming
Web Services in Mac Cocoa Programming
Printing in Mac Cocoa Programming
What is Cocoa? A brief history of Cocoa.
Programming Language used in Cocoa Programming
Objects, Classes, Methods, and Messages in Cocoa Programming