C ++ allocator and some factors that affect Program Performance

Source: Internet
Author: User

Previously, we introduced allocator of c ++. Since the c ++ standard library already has a standard implementation, why should we implement allocator on our own? For performance considerations, many programs implement their own allocator to avoid using the standard library allocator. The standard library allocator will affect program performance in those aspects. The following is a one-to-one analysis:

1. Standard allocator involves system call, resulting in user-kernel-user Switching in the program. An additional cpu cycle is required for mode switching. Frequently use the standard allocator to allocate memory (for example, in a while loop), the cpu cycle consumed by mode switching is superimposed. Custom allocator uses a pre-allocated memory pool to avoid system call and improve memory allocation and recovery efficiency.

2. Standard allocator initiates system call. In addition to causing mode switching, it will also lead to another more serious problem: thread status switching-switching from execution to waiting (blocking ). First, system call is an entry point for the operating system to control thread status switching. As long as the thread initiates the system call, it tells the operating system that status switching can be performed, it depends on the scheduling policy of the operating system. Second, if the system call initiates a long operation, the thread will inevitably switch to the blocking state. Do not forget that memory allocation may be a long operation. For example, if the current physical memory is insufficient and you need to switch pages (causing disk IO ). When the code executed by a piece of key code (in the critical section) or a piece of interface thread is switched to a blocking state due to standard allocator, the consequences can be imagined.

3. Standard allocator may cause memory fragmentation. After fragmentation, some chain reactions may occur in extreme cases: first, frequent page changes may affect the memory allocation efficiency (blocking may occur, as shown at). Second, when the fragmentation continues to deteriorate, the swap-out memory may include the working set of the program in addition to the heap memory. When the working set of the program is switched out, a jmp of the thread may be switched to the blocking state (the target address of the jmp has been switched out), and the program execution efficiency is greatly reduced.

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: info-contact@alibabacloud.com 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.