在如今学习的过程中,我们更多的是在练习如何使用各种方法或某种套路,例如需要设计某个app的时候,我们可能会说我们之前设计过的模型中直接套过来使用,或者在网上需找类似的demo,随着时间和经验的积累,我们在这一方面越来越得心应手,以后一直是copy、copy等,但对一些简单的本质东西不甚了解了,虽然这些东西也可以不必去知道,但我认为多了解一些基本的东西还是有好处的,下面就来简单的说一些对象的内存的问题:1.我们先来创建一个Person类:
#import <Foundation/Foundation.h>@interface DCPerson : NSObject{ int age; NSString *name;}-(void)eat;-(void)walk;@end
The code is simple and there is no comment written. The first is to create a Dcperson class that has two properties, age, and name. There are also two methods, namely-(void) eat and
-(void) walk.
When we create a class in the program, in memory will give the class memory, first class must have address, followed by the corresponding method in the class.
When we use classes to create objects, we assign storage space to the objects accordingly. For example we create two objects Person1 and Person2.
- (void)viewDidLoad { [super viewDidLoad]; //创建person1对象 DCPerson *person1=[[DCPerson alloc] init]; [person1 age]; [person1 walk]; self.person1=person1; //创建person2对象 DCPerson *person2=[[DCPerson alloc] init]; [person2 age]; [person2 walk]; self.person2=person2;}
In this way, there will be two more blocks in memory to hold Person1 objects and Person2 objects.
Let's take a picture to analyze the approximate situation.
When we use the class to create an object, each object will have an Isa pointer, and when we send a message to the object, the object will go through its own ISA pointer to find the corresponding method in the class, never to implement the method. Properties are unique to each object, and methods are common to all objects and only one copy. The class will only be loaded once in memory.
These are some of their own simple analysis of the object memory, just do a few very simple analysis, there is no place to look big God, thank you!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Simple Memory analysis for objects