简体   繁体   中英

Is there a way to monitor heap usage in C++/MacOS?

I fear that some of my code is causing memory leaks, and I'm not sure about how to check it. Is there a tool or something for MacOS X?

Thank you

Yes - there's an application called MallocDebug which is installed as part of the Xcode package.

You can find it in the /Developer/Applications/Performance Tools folder.

Apple has a good description of how to use MallocDebug on OS X on their developer pages.

Of course UNIX provides a quick and dirty way of detecting memory leaks... top.

Launch your app and watch the system memory allocated to your process over time. If it continually grows when it shouldn't then there is likely a memory leak. At which point you break out Valgrind or use MallocDebug, etc.

Of course if you use smart pointers and/or RAII, then you shouldn't have memory leaks in your code, right? ;)))

THE BEST tool PERIOD for memory errors, leaks, etc. is Valgrind. Get started here . You dont need to do anything special in your code and this will report where the memory was allocated (with a full stack trace, even in C). Also, it'll detect writes to freed memory, uninitialized memory usage, and much more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM