Simple Object Memory analysis and Simple Object Memory Analysis
In today's learning process, we are more about how to use various methods or routines, such as when designing an app, we may say that the models we have previously designed are used directly, or we need to find similar demos on the Internet. With the accumulation of time and experience, we are more and more comfortable in this aspect, I have always been copying and copying, but I don't know much about some simple essential things. Although these things do not have to be known, but I think it is good to know more about some basic things. The following is a simple example of some memory problems of objects: 1. let's first create a Person class:
#import <Foundation/Foundation.h>@interface DCPerson : NSObject{ int age; NSString *name;}-(void)eat;-(void)walk;@end
The code is very simple, so no comments are written. First, create a DCPerson class, which has two attributes: age and name. There are two methods at the same time, namely-(void) eat and
-(Void) walk.
When we create a class in the program, the class will be allocated memory in the memory. First, the class must have an address, and the second class will have a corresponding method.
When we use a class to create an object, a bucket is allocated to the object. For example, we create two objects: person1 and person2.
-(Void) viewDidLoad {[super viewDidLoad]; // create the person1 object DCPerson * person1 = [[DCPerson alloc] init]; [person1 age]; [person1 walk]; self. person1 = person1; // create a person2 object DCPerson * person2 = [[DCPerson alloc] init]; [person2 age]; [person2 walk]; self. person2 = person2 ;}
In this way, there will be two more areas in the memory to store the person1 object and person2 object.
Next, we will analyze the general situation with images.
When we create an object using a class, each object has an isa pointer. When we send a message to the object, the object uses its isa pointer to find the corresponding method in the class to implement the method. Attributes are unique to each object, while methods are common to all objects, with only one copy. Classes are loaded only once in the memory.
The above is a simple analysis of the object memory. I just did some simple analysis. If there is something wrong, I hope you can correct it. Thank you!
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.