최대 메모리 사용량을 측정하는 방법은 무엇입니까?

최대 메모리 사용량을 측정하는 방법은 무엇입니까?

C++ 프로그램이 있습니다. 이 프로그램의 최대 메모리 사용량을 알고 싶습니다. GNU C 라이브러리를 사용해 보았 memusage으나 결과가 Windows에서와 동일하지 않습니다. 왜 그런 겁니까?

이것은 내 테스트 프로그램입니다.

#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <list>

int main()
{
return 0;
}

memusage다음은 사용된 72,704바이트를 보여주는 출력 입니다 .

Memory usage summary: heap total: 72704, heap peak: 72704, stack peak: 0
         total calls   total memory   failed calls
 malloc|          1          72704              0
realloc|          0              0              0  (nomove:0, dec:0, free:0)
 calloc|          0              0              0
   free|          0              0

Windows에서는 출력이 0B로 표시됩니다.

답변1

GNU C 라이브러리와 GCC의 표준 C++ 라이브러리는 다음과 같은 목적으로 malloc프로그램 시작 중(실행 전 ) 힙 메모리를 할당하고 호출합니다.main

  • 동적 링크
  • vDSO 설정(GNU C 라이브러리를 사용하는 Linux의 모든 프로그램이 이 작업을 수행합니다)
  • 긴급 예외 처리 사전 할당(더 이상 할당할 메모리가 없어도 항상 예외를 처리할 수 있도록 메모리를 할당하는 것)

처음 두 개는 힙 사용 통계에 나타나지 않으며 memusage72,704바이트 할당은 C++ 예외 처리 할당에 해당합니다. Valgrind의 출력도 참조하세요.

$ valgrind ./memtest
==822129== Memcheck, a memory error detector
==822129== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==822129== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==822129== Command: ./memtest
==822129== 
==822129== 
==822129== HEAP SUMMARY:
==822129==     in use at exit: 0 bytes in 0 blocks
==822129==   total heap usage: 1 allocs, 1 frees, 72,704 bytes allocated
==822129== 
==822129== All heap blocks were freed -- no leaks are possible
==822129== 
==822129== For lists of detected and suppressed errors, rerun with: -s
==822129== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

모든 메모리 시간에 대한 전체 메모리 사용량을 보려면 다음과 같은 간단한 도구를 사용하세요 /usr/bin/time.

$ /usr/bin/time ./memtest
0.00user 0.00system 0:00.00elapsed 100%CPU (0avgtext+0avgdata 3148maxresident)k
0inputs+0outputs (0major+117minor)pagefaults 0swaps

이는 프로그램이 최대 3,148K의 메모리를 사용했음을 나타냅니다. ( /usr/bin/time대신 사용하십시오 time- 후자는 이 정보를 표시하지 않을 수 있는 쉘의 내장 기능을 사용합니다.)

C++ 프로그램이 Windows에서 비슷한 작업을 수행한다고 생각하지만 메모리 공간이 다를 수 있고 시작 힙 사용량이 포함되지 않을 수도 있습니다.

관련 정보