Thumbs up comments, feel useful friends can pay attention to the author public number iOS growth refers to north, continue to update

Small file, big content —.memgraph

We briefly introduced the use of.memgraph in the article from OOM to Memory Management.

Learning to parse and interpret.memgraph files gives us a deeper understanding of memory as a file that keeps track of all memory allocations over the life of an application.

Use memory graphs to further understand and reduce memory footprint

To obtain.memgraphfile

After our application has been running for a while, we can get a.memgraph file with a fair amount of data in the following way.

  • While running the APP, open The Memory Graph and select View Memory Graph Hierarchy

  • After the Memory Graph is generated, clickFile -> Export Memory graphExport a.memgraphfile

Once the.memgraph file is exported. We can explore it further using command-line tools such as VMMap, Leaks, Heap, malloc History.

We need the application to do some of the work, or at least some of the memory. A good guess is that when we break the main.m file, we can find out about the memory allocation before main.

vmmap

In the article from OOM to Memory Management, we provided a brief overview of how to get the.memgraph file using the VMmap command line tool.

vmmap -summary a.memgraph

Use -summary to get an overview of the current A.mgraph file.

Note the following types of packets:

  • The __TEXT code segment, read-only, contains functions, and read-only strings
  • __DATA data segment, read and write, including read and write global variables, etc
  • __LINKEDIT contains metadata for methods and variables (locations, offsets), as well as information such as the code signature
  • __OBJC This area contains Objective-C runtime library code if the application contains Objective-C code
  • Shared Memory System libraries that are Shared with other applications (such as Cocoa and OpenGL)
  • This area contains the contents of frequently accessed files and is Mapped to virtual memory to support faster access
  • The Stack contains the Stack storage, including the parameters for each function call

__TEXT, __DATA, and __LINKEDIT all correspond to the corresponding fields of mach-O File. For more analysis of Mach-O files, see my in-depth Understanding of iOS Startup Processes and optimization tips – Mach-O for more information on Mach-O

vmmap --verbose

We only see stacks in the.memgraph file overview, but what memory does the Heap store refer to in memory management allocation?

Let’s briefly explain the basis of OS X/iOS allocation here.

There is a scalable mechanism for OS X Heap memory allocation. The memory allocated through this mechanism is divided into three types (Tiny, Small, and Large) based on size. We may elaborate on this mechanism later to illustrate how we allocate one large memory or more contiguous or discontiguous memory in memory.

In the.memgraph file we can look at MALLOC_LARGE, MALLOC_SMALL, and MALLOC_TINY to get Heap allocation information.

You can use this command to view information about a specific memory module

vmmap --verbose test.memgraph | grep "MALLOC_TINY"
Copy the code

To obtainDirty MemoryNumber of pages

From OOM to Memory management, OOM is mainly caused by too much Dirty Memory.

We can analyze the number of pages in Dirty Memory to find out in advance when the Memory might be OOM. After all, the size of memory pages is fixed.

vmmap -pages test.memgraph | grep '.dylib' | awk '{ sum += $6} END { print "Total Dirty Pages:" sum } '
Copy the code

LEAKS

Displays objects that have been allocated but are no longer referenced, and displays the reference loop to which the leak belongs.

leaks a.memgraph

Use Leaks test.memgraph to get the results of Leaks analysis

Open the malloc log stack

It is recommended to enable the malloc log stack to get a traceback to the root node.

Edit Scheme -> Run -> Diagnostics before running the application

Use Leaks test.memgraph to get the corresponding description information

How to query

The amount of information printed by command can be easily intercepted by command-line tools, and it can take a long time to delve into some memory.

Leaks-helper Gets the methods supported by the Leaks tool

Then the –trace or –traceTree commands help

Heap

Displays the objects allocated on the process heap that identify large objects in memory and the objects allocated.

heap -sortBySize a.memgraph

Use heap-sortbySize A.emgraph to get all the objects assigned to the heap in reverse order of size

Using the heap command allows you to see what objects are allocated at a glance.

Practical value

If our project uses a large number of class names with custom names, we can retrieve the classes created by the current classification that exceed a code limit by using the heap command

You can use the following command to get the number of current object addresses over 100 bytes.

heap --addresses=UIImage[100-] test.memgraph
Copy the code

A large memory creation means there may be some problems. If our custom classes take up too much memory and are created too many times, it could mean something is wrong

In addition to memory allocation during runtime, would it help if we exported our.memgraph file as soon as the first screen was loaded to help us analyze which classes were created unnecessarily?

MALLOC_HISTORY

We can do something interesting with.memgraph files that have been generated by turning on the malloc log stack by using the malloc_history command.

As mentioned earlier, opening the Malloc log stack helps us trace back. When we are in doubt about an address or want to know when it was created, use this command to provide a traceback for a given address in memory. From this traceback, we can identify and better manage functions/classes that cause large memory allocations.

conclusion

How do we choose this approach in real projects?

Memory Creation Reference: Specific Size
malloc_history leaks vmmap, heap

When we need to learn more about how to introduce memory footprint, we can try using the above tools

Thank you for reading this! 🚀

Reference documentation

IOS — Advanced Memory Debugging to the Masses

iOS Memory Deep Dive