Simple Memory analysis for objects

Source: Internet
Author: User

#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

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: 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.