search

Home  >  Q&A  >  body text

php - How to understand the memory fragmentation mentioned in C language programming? Why should you avoid him as much as possible?

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?

滿天的星座滿天的星座2717 days ago923

reply all(3)I'll reply

  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-24 09:44:23

    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 time malloc() is used, and then marks this small segment as unallocated each time free().

    The order of malloc/free is generally arbitrary, so allocated and unallocated will alternate after multiple malloc/free, such as allocated 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

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-06-24 09:44:23

    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.

    reply
    0
  • 为情所困

    为情所困2017-06-24 09:44:23

    • 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 of malloc, just take a look at the source code of dlmalloc, it’s not complicated

    reply
    0
  • Cancelreply