Memory stability of a C++ application in Linux

前端 未结 6 1148
余生分开走
余生分开走 2021-01-03 05:14

I want to verify the memory stability of a C++ application I wrote and compiled for Linux. It is a network application that responds to remote clients connectings in a rate

6条回答
  •  鱼传尺愫
    2021-01-03 05:28

    I would suggest using valgrind with memcheck tool or any other profiling tool for memory leaks

    from Valgrind's page:

    Memcheck

    detects memory-management problems, and is aimed primarily at C and C++ programs. When a program is run under Memcheck's supervision, all reads and writes of memory are checked, and calls to malloc/new/free/delete are intercepted. As a result, Memcheck can detect if your program:

    • Accesses memory it shouldn't (areas not yet allocated, areas that have been freed, areas past the end of heap blocks, inaccessible areas of the stack).
    • Uses uninitialised values in dangerous ways.
    • Leaks memory.
    • Does bad frees of heap blocks (double frees, mismatched frees).
    • Passes overlapping source and destination memory blocks to memcpy() and related functions.

    Memcheck reports these errors as soon as they occur, giving the source line number at which it occurred, and also a stack trace of the functions called to reach that line. Memcheck tracks addressability at the byte-level, and initialisation of values at the bit-level. As a result, it can detect the use of single uninitialised bits, and does not report spurious errors on bitfield operations. Memcheck runs programs about 10--30x slower than normal. Cachegrind

    Massif

    Massif is a heap profiler. It performs detailed heap profiling by taking regular snapshots of a program's heap. It produces a graph showing heap usage over time, including information about which parts of the program are responsible for the most memory allocations. The graph is supplemented by a text or HTML file that includes more information for determining where the most memory is being allocated. Massif runs programs about 20x slower than normal.

    Using valgrind is as simple as running application with desired switches and give it as an input of valgrind:

    valgrind --tool=memcheck ./myapplication -f foo -b bar
    

提交回复
热议问题