gogoWebsite

【C++】【Memory】Series 3: Memory Optimization

Updated to 2 days ago

There are two assessment indicators for program performance: one is time and the other is space. This article optimizes the program's running memory as much as possible (may increase the running time).

Stack memory optimization

Since the stack is managed by the operating system, there is no need to actively release memory, and do not apply for too large stack memory to prevent stack overflow.

a. The function cannot be too large (it is recommended not to exceed 40~50 lines), otherwise it will cause too much stack memory occupied by the thread.
b. Use references or pointers as much as possible for function parameters with large memory values.
c. The function body defines variables and uses variables as compact as possible. The operating system will recycle the stack memory as soon as possible to reduce the amount of stack memory usage.

Heap memory optimization

Memory calculation

Generally, it calls the memory consumption interface provided by the operating system, periodically outputs memory consumption data, and makes it into a curve chart, so that the memory consumption situation can be clearly seen. Be careful not to run other programs at this time to avoid interference.

Memory optimization

Compiler optimization
a. In embedded type, you can specify the CPU model and perform specific compilation.
b, enable -O optimization option.


Algorithm and data structure optimization
a. If the data length is determined, it is mainly done with query work. It is recommended to use an array to complete it.
b. If the number of insertions and deletions is mainly the number of times, you need to use a linked list or hash table.
c, use efficient sorting and search algorithms.
d. The data members in the structure are aligned according to the CPU bit length.

Function optimization
a, small functions use inline functions
b. Reduce the number of incoming parameters of the function, and use reference transfer or move semantics as much as possible.

Underlying library or API optimization
a. Use a library or API with less memory consumption. Mebdtls
b. Reduce the introduction of library files. For example, you can define your own macro functions if you find absolute values.
c, delete printf multi-strategy function, use puts better

kind
a. When designing a class, don’t have too many inheritance layers
b. Define the moving constructor and moving assignment operator to avoid unnecessary copying operations

Operation
a, divide by a power of 2 or 2 to move right;
b, use composite assignment operators instead of ordinary operators, such as a += b; a = a + b;
c. Convert floating-point operation into integer operation, try to use floating-point operation as little as possible
d, simplified expression: y = a*x*x*x + b*x*x + c*x + d; => y = (((a*x + b)*x) + c)*x + d;
e, Switch instead of if-else

For loop
a, the unchanged code (strlen) is removed from the loop body
b. Expand the loop, read as 1:8, write as 1:16, and of course debug according to the specific processor.
c. Use statements involving construction destruction in for with caution, and it is recommended to put them outside for. For example, string, vector
d, avoid dynamically creating variables in loops
e, avoid calling hidden functions in loops
(1) • Declare a class instance (call the constructor)
(2) • Initialize a class instance (call the constructor)
(3) • Assign to a class instance (call the assignment operator)
(4) • Computational expressions involving class instances (call operator member functions)
(5) • Exit scope (call the destructor of the class instance declared in scope)
(6) • Function parameters (each parameter expression will be copied and constructed into its formal parameters)
(7) • Function returns an instance of a class (called the copy constructor, probably twice)
f, it is recommended to use move assignment instead of copy assignment operator in for