Some understanding of memory allocation and memory management

Source: Internet
Author: User

Memory allocation method and memory allocation algorithm

There are two ways of memory allocation, the way of continuous memory allocation and the allocation of discrete memory. Different allocation methods have different allocation algorithms.

Memory allocation algorithm, in fact, is: a large chunk of free resources, How to allocate resources rationally? the idea of memory allocation can be used in many other areas.

① Continuous Memory allocation method

1) Fixed partition allocation

Divides the memory into several fixed-size blocks. Load the program into a block. After the memory is divided into blocks, the block size no longer changes. Of course, blocks are divided in the same way: all blocks are equal in size, and block sizes are unequal.

This way, before the actual memory allocation, you already know all the memory block size.

2) Dynamic Partition allocation

A free table or idle chain is required to record the area of memory in the current system. In memory allocation, you need to find a spatial table or a free chain to find a chunk of memory allocated to the current process.

Dynamic Partition allocation algorithm:

A) First adaptation method

b) Cyclic first adaptation method

c) Best Fit method

D) worst-fit approach

e) Rapid Adaptation method

3) can relocate partition allocation

To be blunt, it is to increase the function of memory movement. Due to several memory allocations and recoveries, each free memory block is not contiguous. With "relocation", the allocated memory is "compact" in one piece (similar to the replication algorithm in JVM garbage collection), thereby emptying a chunk of free memory.

"Compact" is cost-intensive, such as the need to recalculate addresses, which is why JVM garbage collection causes STW.

The discrete distribution method-whether it is pagination or fragmentation, is to put the program directly into each discrete page. Thus there is no "compact" one said.

Continuous memory allocation involves two operations: memory allocation operations and memory reclamation operations

② Discrete Memory allocation method

The memory resource is limited and the program is running and must be loaded into memory. What if the memory is full and now there are new programs to run? ---SWAP

The current unused program (data) to swap out memory, so that there is space to load the program currently needed to run part of the data into memory, which greatly improves the memory utilization.

Because of the involved swapping in and swapping out, the previous way of continuous memory allocation is a bit inappropriate. Because, the most obvious question: for the continuous memory allocation method, what part of the data is swapped out?

And this kind of loading only partial " data" can make the program run mechanism, is the nature of virtual memory.

1) Paging Storage Management

Divides the logical address space of a process into several pages of equal size, and also divides physical memory into pages of equal size (called blocks or frames). When allocating memory for a process, several pages of the process can be mounted in blocks into multiple non-contiguous physical blocks in memory.

As can be seen from the above: "Discrete" is reflected in: the process in memory allocated space (physical block) is discontinuous. in the case of continuous allocation, the space in which the process is allocated in memory is continuous.

Now consider a 32-bit system with a size of 4KB per physical block. How do I convert a logical address to a physical address?

For each process, it has its own page table. The essence of a page table is the mapping of logical addresses to physical addresses.

The logical address in the paging store is structured as follows:

1) because the logical page size of the process is the same size as the physical block (page frame), the ancient capital is 4K, so 12 bits are required to represent the 4K size (2^12=4k), which is "0-11" in the figure

2) "12-31" represents the page number. A total of 20 bits represents the page number, which is: for a process, there can be 1M (2^20=1m) pages altogether.

3) The logical address space for each process is 0-2^32-1 because each page is 4K in size and has a total of 1M pages. Therefore, the logical space available for the process is 2^32b

The conversion of the logical address to the physical address is required for the page table. The specific details are an "address changer", which has a register to save the page table at the start address of the memory and the length of the page table .

As mentioned above, a process can have a maximum of 1M pages, so the page table has 1M page table entries. Assuming that each page table entry is only 1 B, the page table size also has 1MB, so: In general, the page table is also very large, not all in the register, so the page table is also stored in memory . (Some machines have "quick tables", and a fast table is a register that holds some of the table items in the page table); Second, you can use a multilevel page table to solve the problem of a single page table being too large.

Now given a logical address, how to know its physical address?

① compares the page number of the "12-31" bit to the length of the page table. The page number cannot be greater than the page table length, otherwise out of bounds.

② finds the page table entry that contains the page number based on the page number, that is, which page table item the page number corresponds to. Because the physical address is stored in the page table entry.

So how do you find page table entries? Multiply the page number by the length of the page table item (the mapping information for each page table item, which is actually a logical page to the physical page), and you know which page table item the logical page corresponds to (the page table entry is typically hardware-based)

Then, as mentioned earlier, the page table is also stored in memory, it is necessary to the page table memory address (This is why the address transformation mechanism to save the page table in the memory of the starting address of the reason), the page table from the beginning of the product together with the above, the logical page corresponding to the page table entry physical addresses . Reading the contents of the physical address of the page table entry , you know the physical block address (physical address) corresponding to the logical page. Thus, the conversion of the logical address to the physical address is completed.

As can be seen from the above,every time the CPU accesses one data, it needs to access the main memory again. Once the physical address of the page table entry is accessed, the physical block address of the data is obtained. Take the physical block address for the second time to fetch the data.

In distributed storage Management mode: Due to a data, need two times, CPU processing speed is reduced by half, for this reason: the introduction of "Fast Table" (also known as TLB (translation lookaside Buffer)), the Fast table is a register, Used to save the page table entries that are currently being accessed. Thus, when the page table entry is read, it does not need to be revisited, but is read directly from the register.

Virtual memory

When it comes to virtual storage, it's always said that it logically expands the capacity of memory, why?

The memory is limited and the job is initially saved on disk, and if you want to run it, you must load the appropriate program (data) into memory. What if there are so many jobs to run that can't be loaded into memory all at once?

One way is to add a memory bar, which is the capacity to physically expand the memory.

The other way is: first part of the operation of the program (data) into the memory, let it run, the process found that: gee, I also need other data, and this data has not been loaded into memory, so there is an interruption (fault pages) and then load the data memory.

In this way, the system can load many jobs into memory at once. At this point, the physical memory is still the original size, but it can run a lot of jobs, so that the logical expansion of memory.

The idea of virtual storage, combined with paging storage management, only loads part of the job's pages into memory at a time, creating a powerful memory allocation and management system. With the introduction of virtual memory, it is also necessary to have a page table that records the mapping of the logical address to the physical address, except that the page table is more complex, because some pages may still be on disk. Also need to have a fault processing mechanism, because after all, only a portion of the data into memory, will cause a missing pages, you need to deal with interrupts, you also need an address transformation mechanism, where the address of the transformation mechanism more functions, because the need to handle the address transformation in case of interruption.

2) Segmented storage management

Some understanding of memory allocation and memory management

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: 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.