heap_profiler.cc 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //
  2. // Copyright (c) 2013-2021 Winlin
  3. //
  4. // SPDX-License-Identifier: MIT
  5. //
  6. /**
  7. @see: https://gperftools.github.io/gperftools/heapprofile.html
  8. config srs with gperf(to make gperftools):
  9. ./configure --with-gperf --jobs=3
  10. set the pprof path if not set:
  11. export PPROF_PATH=`pwd`/../../../objs/pprof
  12. to do mem profile:
  13. make && rm -f srs.*.heap && env HEAPPROFILE=./srs ./heap_profiler
  14. $PPROF_PATH --text heap_profiler ./*.heap
  15. */
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <signal.h>
  19. #include <stdlib.h>
  20. #include <gperftools/heap-profiler.h>
  21. void memory_alloc_profile_imp() {
  22. for (int i = 0; i < 2; ++i) {
  23. char* p = new char[110 * 1024 * 1024];
  24. for (int j = 0; j < 110 * 1024 * 1024; ++j) {
  25. p[j] = j;
  26. }
  27. printf("mem profile, increase 110MB\n");
  28. printf("press CTRL+C if you want to abort the program.\n");
  29. sleep(5);
  30. }
  31. }
  32. void memory_alloc_profile() {
  33. memory_alloc_profile_imp();
  34. }
  35. int main(int argc, char** argv) {
  36. // must start profiler manually.
  37. HeapProfilerStart(NULL);
  38. memory_alloc_profile();
  39. // not neccessary to call stop.
  40. //HeapProfilerStop();
  41. return 0;
  42. }