Introduction to the basic concept and classification of memory (2)

Source:   Editor: Jacquelyn Update Time :2019-03-11


  

Hierarchical structure of memory

According to the proximity degree to CPU, memory is divided into internal memory and external memory. Internal memory, also known as main memory, belongs to the components of the host, while external memory, also known as auxiliary memory, is external devices. CPU can't access external memory directly as it accesses memory. If transmitting data between external memory and CPU or I/O devices, internal memory must be required. High-end microcomputer like 80386 or above is equipped with cache. At this time, internal memory includes two parts: main memory and cache. However, for low-end microcomputers, internal memory is main memory.

The memory is divided into several levels for the following reasons:

First, resolving the contradiction between speed and cost reasonably can provide a higher performance-price ratio. Semiconductor memory is fast but expensive, and its capacity should not be done very large, so it is only used as an internal memory to exchange information frequently with CPU. Disk memory is cheaper, and it can be produced with a large capacity. But its access speed is slow, so it can be used as external memory stored a large number of programs, raw data (many programs and data are temporarily not involved in the operation) and the operation of results, with a small number of accesses. When a computer performs a certain task, it only loads the related programs and raw data from disk to internal memory with small capacity. After high-speed data processing with CPU and internal memory, final results are written from internal memory to disk. Such configuration is affordable with faster comprehensive access.

Cache can also be used to address the contradiction between high-speed CPU and relatively slow main memory. Cache uses fast and expensive semiconductor static memory with microprocessors to store the most frequently used instructions and data. When the CPU reads instructions and data from internal memory, both cache and main memory are accessed simultaneously. If the required content is in the cache, it can be retrieved immediately; if not, it can be read from main memory. The contents of cache are replaced in time according to the actual situation. In this way, a high speed can be achieved by adding a small amount of cost.

Using disk as external memory is not only cheap to make storage capacity large, but also possible to protect the information stored in it from being lost when the power is cut off. It can preserve information for a long time, and it is very convenient to copy and carry.

Memory management

The server allocates memory in the memory environment in such a way that the memory allocated in one environment can be released by the environment destructor without affecting the memory allocated in other environments. All memory allocations (through palloc, etc.) are treated as allocating memory in the area of the current environment. If you try to release (or redistribute) memory that is not in the current environment, you will get unpredictable results.

Creating and switching memory environments is the task of memory managers in SPI managers.

SPI processes handle two memory environments: upper executor memory environments and process memory environments (if connected).

Before a process is connected to the SPI manager, the current memory environment is the upper executor environment, so all the memory allocated by the process itself is in this environment before connecting to the SPI manager through palloc/repalloc or through the SPI application function.

After the SPI_connect call, the current environment is owned by the process itself. Memory allocated through palloc/repalloc or through SPI application functions (except SPI_copytuple, SPI_modifytuple, SPI_palloc and SPI_repalloc) is allocated in this environment.

When the process is disconnected from the SPI manager (by calling SPI_finish), the current environment is restored to the upper executor environment and all the memory allocated in the process memory environment is released and cannot continue to be used!

If you want to return something to the upper executor, you have to allocate a piece of memory for that in the upper environment!

SPI cannot automatically release the memory allocated in the upper executor environment!

SPI automatically releases memory allocated during query execution after query is completed!

Related Articles:


Don't be silly. Popular science: What's the difference between memory and storage?(2)


Introduction to the basic concept and classification of memory (1)


Introduction to the basic concept and classification of memory (3)