Skip to content

Instantly share code, notes, and snippets.

@micjabbour
Last active March 21, 2021 22:31
Show Gist options
  • Save micjabbour/167d49192d489e64f62faebd9743869b to your computer and use it in GitHub Desktop.
Save micjabbour/167d49192d489e64f62faebd9743869b to your computer and use it in GitHub Desktop.
C++ healp-allocated memory usage measurement example
//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