Memory structure hierarchical, I think everyone is clear, in the storage of computer, there are a variety of memory, which directly affects our program efficiency, in general, can be divided into 5 Level: Register, first-level cache, secondary cache, main memory, disk memory. Let's take a generally: 1. Register, it is the shortest latency in all memory, the bandwidth, the least overhead, there is no doubt, this is the fastest speed, but the cost is more expensive, so The number of registers is limited, we should try to make full use of registers, there is a keyword in C / C : register? For variables that are frequently used, it is possible to become a performance bottleneck, which may increase in efficiency (after all, the bottleneck of the program rarely appears in a variable, generally in algorithm), but for individual Function, performance may have an increase in order. However, as in Inline is that register is just a prompt, prompting the compiler to allocate memory for variables, but decision is in the compiler, some compiler (of course, those relatively poor) will completely ignore Register indicator. 2, first-level cache, also known as the chip cache, speed is very fast, its delay is also in the same order with the register, but the capacity is also limited. At present, many CPUs have a level 2 chip cache, we can regard them as a chip cache, do not affect our discussion. According to 1999, we know that the register is delayed 2ns, and the first-level cache is also 2ns, and it seems that it seems to have the same performance, and it is not possible. Typically, a register group can read two operands within a clock cycle and write an operand, a total of 3 operands, and 2 clock cycles can be processed for the primary cache to handle one operand, compared to Below, the bandwidth of the register group is approximately 6 times the first level. Of course, for some special hardware may not meet this law, in any case, the bandwidth of the register is larger than the first level. 3, secondary cache, also known as the chip outside cache, speed is 1-2 times slower than the first level. 4, the main memory, that is, the usual memory is a semiconductor dynamic random access memory. Common DRAM, SDRAM, RAMBUS, etc., the capacity of memory has become great, common It is 256M, 512M. The main memory access of the computer has a very regular feature, which is what people say "6-1-1-1-2-1-1-1", what do you mean? That is, the initial access to memory requires 6 bus cycles, and then 3 times, only one bus cycle is required, the next 1 visit requires 2 bus cycles, then 3 visits, it only needs a bus cycle. This is the so-called "6-1-1-1-1-1-1-1-1" 5, disk memory, the most commonly used, the hard disk, speed and the above, the difference is a few orders, The action of accessing the hard disk belongs to I / O Access, which is very affected by performance, so try to avoid it. Of course, sometimes it is not accessible, then some effective strategies, such as: open a buffer pool. For large data access, its performance will be closely related to the system's virtual memory mechanism and is closely related to the file structure.
The secret memory of virtual memory is a very nice mechanism. On the surface, he seems to convert limited memory into unlimited memory space, especially the modern operating system, often has virtual storage that maps permanent data to the system. To access these permanent data, the system is enhanced, but also exacerbates the programs we have written in a large number of virtual memory mechanisms. Although, when we accesses the hard disk file, the data is resident in memory, and the system opens up a space as a virtual memory on the hard disk, and the virtual memory is used to dynamically manage the file. However, don't forget, due to the hard disk access, make it have to use the memory management code, and the result is expensive as the system's virtual change system. Depending on the data provided by the hard disk manufacturer, the disk access requires 12-20ms, typical disk access at least 2 context transitions and the execution of the low-level device interface, which requires thousands of instructions, millions of clock cycles. Delay. So if our procedure is relatively high for performance requirements, consider it when using virtual memory. Memory Allocation Policy For memory allocation, there are usually two comparison commonly used allocation policies: variable size allocation policies, fixed size assignment strategies.
Variable size distribution strategy, the key is on the use of their versatility, by them, users can apply for any size memory space to the system, clear, such allocation is very flexible, the application is very broad, but they also have their own fatal Defects, however, for us, the most affected approximately 2 aspects: 1. In order to meet the requirements of user requirements and system, there will be some additional work, and the efficiency will naturally decline; 2, run in the program During the period, there may be frequent memory allocation and release actions, using our existing data structure and operating system knowledge, this will form a large amount, discontinuous, not able to use memory fragments in memory, there are many In case, this is fatal for our procedures. If we can restart the system every other period of time without problems, but some procedures are unable to interrupt, even if it is interrupted, it is not realistic to restart the system every other time (who dares with you. Dongdong?)
The key to fixed size distribution strategy is that the key is fixed, that is, when we apply for memory, the system always returns a fixed size (usually 2 index) memory space, regardless of our actual memory the size of. Compared with the general-purpose distribution strategy above, it seems to be more dull, but the speed is faster and does not produce too many fine fragments.
In general, the variable size allocation strategy and fixed-size assignment strategy often cooperates together, for example, the dispenser will have a divided line M. When the application is applied, the fixed size assignment is used, and when the application is larger than the size of the application When M is used, the allocation of variable size is used. In fact, this mixing strategy used in SGI STL is 128B, and if the memory size of the application exceeds 128b, it is handed over to the first level of configurator processing. If it is less than 128b, the memory pool policy is employed. Maintain a small number of free-Lists.
Memory service hierarchical memory allocation has many strategies, then how do we know who is responsible for memory allocation? The memory allocation service and storage structure are also a hierarchical: first layer, the operating system kernel provides the most basic services, which is the basis of memory allocation strategy, but also the hardware system to the most close, so these services are said The characteristics are also different. The second floor, the default runtulent run library of the compiler provides its own allocation service, and new / malloc is provided by local allocation services, and the specific service method relies on different compilers, the compiler's service can only assign local allocation. The service is a simple layer of packaging, not considering any efficiency, for example, New is a layer of shallow packaging to Malloc. The service of the compiler can also be overloaded for the local service, and the memory is used in a reasonable way. Like the memory allocation service provided by the standard container, like the default runtime providing services, he can use the compiler's service, such as the standard configurable device in SGI, Although in order to comply with the STL standard, SGI defines this configuration device, but in actual applications, SGI abandoned it. It is also possible to overload, implement your own strategy and optimization measures. For example, an SGI empty sword configuration device having this configuration capability is used in SGI. The outermost layer, user-defined container and distributor provides services, we can implement your favorite solutions for the container's distributor, or to reload new / delete, let him do what we like.