Last active
March 21, 2021 22:31
-
-
Save micjabbour/167d49192d489e64f62faebd9743869b to your computer and use it in GitHub Desktop.
C++ healp-allocated memory usage measurement example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//from Howard Hinnant's answer https://stackoverflow.com/a/28003328 | |
#include <cstdlib> | |
#include <cstddef> | |
#include <iostream> | |
std::size_t allocated = 0; | |
void* operator new(std::size_t sz) { | |
void* p = std::malloc(sz); | |
allocated += sz; | |
return p; | |
} | |
void operator delete(void* p) noexcept { | |
std::free(p); | |
} | |
int main() { | |
allocated = 0; //exclude memory heap-allocated memory for static objects | |
//TODO: do whatever you want to profile here | |
std::cout << "total amount of heap-allocated memory: " << allocated << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment