IOS: memory consumption for data distribution

This may be a basic question, but just curious to know the answer. How much memory will be occupied when we create each variable of type int, NSString, NSDictionary, NSData, etc. In our iOS program, Obj C. I hope it looks like a C program based on the size of a data type (or) differently?

Thanks!

+4
source share
5 answers

In short, you cannot. At least this is not useful.

Almost every class will have any number of distributions that are not directly credited to the instance distribution. In addition, many classes - NSDictionary , NSArray , NSString - are actually part of the cluster of classes, and thus, what is actually allocated is a subclass. Finally, for different collection classes and data, the size of the associated distributions will vary greatly depending on their contents. Some classes are UIImage , NSData , etc. - may contain MB of data that is not actually represented by the distribution of the heap in that they display data from the file system.

Or to summarize, class_getInstanceSize() useless.

Instead, you need to focus on using the memory of your application as a system characteristic of its behavior. The Allocations Instrument can measure your memory usage well and, more importantly, help you determine what is responsible for its consumption.

+3
source

You must consider the size of Objective-C objects, implementation details that you cannot rely on. They may be consistent from one medium to another, but they do not guarantee this. In addition, there is no permanent and complete way to estimate their size, as they can be implemented by all kinds of smart methods.

If you need precise control over memory allocations, just use the C types, which in themselves are fully valid Objective-C.

+2
source

You can get the size of any class by calling it, for example:

 #import "objc/runtime.h" int size = class_getInstanceSize([NSDictionary class]); 
+1
source

It depends on the content that you would like to put in this class (NSString, NSDictionary, NSData). You can say that NSMutableString etc. They change their sizes, but they simply redistribute themselves.

+1
source

Source: https://habr.com/ru/post/1493504/


All Articles