php - How to understand the memory fragmentation mentioned in C language programming? Why should you avoid him as much as possible?
滿天的星座
滿天的星座 2017-06-24 09:42:23
0
3
818

Isn’t the memory completely random read and write? It is a different physical medium than the mechanical hard disk. So why does it affect the performance if there are memory fragments in the memory? What exactly does this memory fragmentation refer to?

滿天的星座
滿天的星座

reply all (3)
女神的闺蜜爱上我

The memory allocator usually applies for a large segment of memory from the OS in advance, and then marks a small segment as allocated each timemalloc()is used, and then marks this small segment as unallocated each timefree().

The order of malloc/free is generally arbitrary, so allocated and unallocated will alternate after multiple malloc/free, such asallocated 1 -- unallocated 1 -- allocated 2 -- unallocated 2, that is, unallocated Memory is a "fragment".

Disadvantages of memory fragmentation:

  • Extra memory occupation: Even if the total amount of unallocated memory is sufficient, contiguous memory may still not be separated. At this time, you need to apply for more from the OS

  • Affects the cache: The cache is based on pages, and the unallocated part of a page also occupies the cache

    phpcn_u1582

    The best way is not to use the system’s default process heap. Instead, apply for a new memory heap yourself. Data of one type is placed in a heap. This is suitable for small pieces of data that are frequently requested and released.
    You can also malloc a large piece of data yourself first, and then use your own memory pool to manage it.
    For the former, Windows’ file manager Explore is useful. For example, the traversed directory tree structure is placed in a separate heap. HeapFree releases the entire heap at one time, without the need to release leaf nodes again and again.
    Second, for example, libjpeg itself has a dedicated memory pool.

      为情所困
      • You can learn about buddy algorithm

      • There is also this PDF

      • If you can’t open it, go over the wall

      • If you want to know the implementation ofmalloc, just take a look at the source code ofdlmalloc, it’s not complicated

        Latest Downloads
        More>
        Web Effects
        Website Source Code
        Website Materials
        Front End Template
        About us Disclaimer Sitemap
        php.cn:Public welfare online PHP training,Help PHP learners grow quickly!