我想跟踪一个大型应用程序当前分配了多少内存。
我发现我可以在 malloc/free/realloc 周围安装钩子(Hook)以拦截内存分配调用:
http://man7.org/linux/man-pages/man3/malloc_hook.3.html
所以我想跟踪的是分配的总字节数 - 释放的总字节数。
现在的问题是 free 只需要一个指针而不是一个大小。
可以在我的 malloc Hook 中创建我自己的映射或 HashMap ,跟踪为该指针分配了多少内存,但这会导致相当多的开销。
在调用 free 时(使用默认的 g++ malloc)在 Linux(64 位)上有什么方法(即使有点 hack)获得 ptr 的大小?
最佳答案
linux man malloc_usable_size
malloc_usable_size() returns the number of bytes available in the dynamically allocated buffer ptr, which may be greater than the requested size (but is guaranteed to be at least as large, if the request was successful). Typically, you should store the requested allocation size rather than use this function.
关于c - 有没有办法知道在 Linux 中传递给 __free_hook 的指针的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15368093/