memory allocation and partitioning in iOS development

Source: Internet
Author: User

memory allocation and partitioning in iOS development About Ram&rom

RAM and ROM are specific storage spaces, collectively known as memory.

RAM (Random access memory): Run memory, the CPU can be directly accessed, read and write very fast, but not power-down storage. It is also divided into:

    • Dynamic DRAM, a bit slower, requires regular refresh (charging), we often say that the memory bar refers to it, the price will be slightly lower, the mobile phone memory is also referred to it.

    • Static SRAM, fast, we often say the first level of cache, two-level cache refers to it, of course, higher prices.

Rom (read only memory): storage RAM, can be power-down storage, such as SD card, Flash (mechanical disk can also be simply understood as ROM). With a lot of: Nandflash, there are Norflash, now used is relatively small (the main difference is the former space, cheap, the latter can directly run the program, read faster).

Because the RAM type does not have the power-off storage capacity (that is, a stop power supply data is all gone, the new power is all garbled, so need to initialize), so the app is generally stored in ROM. RAM is accessed much more quickly than ROM, and the price is higher.

RAM and ROM work together
Since RAM does not power down the storage, our apps, brush packs, downloaded files, and so on, are stored in ROM.

The ROM used in the phone is basically nandflash,cpu is not directly accessible, but requires file system/driver (embedded in the EMC) read it into RAM, the CPU can be accessed. In addition, RAM is faster than Nandflash.

Memory partition: Can be divided into 5 zones

When it comes to memory partitioning, memory refers to RAM.

Stack: This is generally done by the compiler, or system management, there will be some local variables, function jump jump when the field protection (register values are saved in recovery), these systems will help us automatically, without our intervention. So a lot of local variables, deep recursion, function loop calls can run out of stack memory and cause program crashes.

Heap: Typically managed by programmers, such as alloc application memory, free frees memory. The objects we create are also placed here.

Global zone (static zone static): The storage of global variables and static variables is placed in a block, initialized global variables and static variables in an area, uninitialized global variables and uninitialized static variables in another area adjacent. The system is released after the program finishes. Note: In an embedded system, the global zone can also be divided into uninitialized global zones:. BSS segment and initialize Global zone: Data segment. Example: int A; uninitialized. int a = 10; initialized.

Constant: The constant string is placed here, as well as the const constant.

Code area: Store the code, the app will be copied here, the program is not stored in the ROM? Look at the following example

Each area in the diagram is not contiguous

Memzonepic01.png Program Run example (coordination between CPU RAM ROM)

First understand the following: virtual memory and physical memory.

All the programs on the phone depend on the operating system, running on virtual memory, and each app will assume that it has all the virtual memory. For example, a mobile phone, which is a 32-bit operating system (generally also 32-bit bus), real physical memory is 2G, then his address space is 4G (2 of 32), for the app, it feels that it has 4G of memory, although it is not possible (or the same time is not possible), but, The operating system as long as the app used to ensure that the address space has a real physical address corresponding to it, the app does not need to know the corresponding 2G real physical memory where the specific. It is not possible to require 4G of virtual memory to have real physical memory at the same time, which is impossible because there is only 2G of physical memory.

In the example below, only virtual memory is considered

When we click on the Phone Screen app icon to launch an app (for example), the operating system will open up 4G of virtual memory space (to open up real physical memory, corresponding to 4G of virtual memory), the operating system will be stored in the ROM part of the code (limited by space, not all copies), Copy the code area of the 4G memory space that was opened up to the previous step, for example, and then the CPU can access the RAM to run the program.

Assuming that we downloaded a 100M video, it will be downloaded from server 1.1 to ram and then from RAM to ROM storage. This will ensure that the video is still there when we turn it off and turn it on again. Assuming that we want to watch the video over time, the program will read it from ROM to ram and decode the playback.

Memzonepic002.png Programming Note

When an app launches, the code area, the constant area, and the global address are fixed, so pointers to these extents are not empty and cause a crash error. While the heap and stack areas are constantly changing (heap creation destruction, stack bounce pop-up), so when using a pointer to the memory in these two areas, it is important to note whether the memory has been released, otherwise it will cause program crashes (very common in programming).

memory allocation and partitioning in iOS development

Related Article

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: info-contact@alibabacloud.com 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.