DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Analyzing “java.lang.OutOfMemoryError: Failed to create a thread” Error
  • Mastering Ownership and Borrowing in Rust
  • Fixing OutOfMemoryErrors in Java Applications
  • Using Heap Dumps to Find Memory Leaks

Trending

  • AI Meets Vector Databases: Redefining Data Retrieval in the Age of Intelligence
  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Fixing Common Oracle Database Problems

Native Memory Leak Diagnostics with Visual Studio 2015

Leak diagnostics is a nasty business in native applications. There have been many attempts at solving this problem automatically.

By 
Sasha Goldshtein user avatar
Sasha Goldshtein
·
Apr. 09, 15 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
41.8K Views

Join the DZone community and get the full member experience.

Join For Free

leak diagnostics is a nasty business in native applications. there have been many attempts at solving this problem automatically. to name a few:

  • the  crt debug heap  (  which is no longer used by default in visual studio 2015!   - see update below.  ) can help identify memory leaks by associating each allocation with additional data on the allocating source file and line number. at program exit (or whenever a special crt function is called), all blocks that haven’t been freed are printed out. this has been around forever. the problem is that you need to compile with the crt debug heap enabled, so you can’t pick an arbitrary application and start analyzing it for memory leaks. it’s still a useful technique, and one used by boost test. if you’re interested, here’s a great walkthrough .
      update  : where would i be without you, dear readers! ofek shilon points out that it’s the windows debug heap that’s being phased out by default in visual studio 2015, and not the crt debug heap, which is still alive and kicking. here’s  his post on the subject  . 
  •  umdh  (from the debugging tools for windows) can diagnose memory leaks by using built-in windows support for capturing allocation stack traces. when properly configured, the windows heap manager will keep track of all outstanding allocations and their stack traces, and umdh.exe can compare allocation snapshots as the program is executing and determine which blocks haven’t been freed. unlike the crt debug heap, you get a call stack for each leaking allocation, and you get aggregation so if you leaked 1,000 blocks from a single source location, you only see it once in the report. moreover, unlike the crt debug heap, you don’t need any ahead-of-time preparation. you can use umdh to diagnose any application without recompiling.
  • the windows heap manager is instrumented with  etw events  (heapalloc/heaprealloc/heapfree), and wpa can aggregate them to show you blocks that were allocated but not freed during a particular trace you record. if you enable stack walking, you can get allocation call stacks as well. again, just like umdh, this doesn’t require ahead-of-time preparation and works with optimizations enabled.

i think the key problem with all the approaches above is the amount of configuration they require. most c++ developers want leak analysis to be available in their ide, and get the information at their fingertips without running an additional tool and subjecting themselves to complex trace analysis. (umdh reports are known for being user-unfriendly, and wpa has its quirks as well.)

native memory leak diagnostics in visual studio 2015

visual studio 2015 makes native leak analysis a lot easier for most developers. you will still need umdh or etw for production debugging and leak detection, but in the comfort of your development workstation, visual studio should be able to suit your needs. basically, you recompile with the visual c++ 2015 compiler, and then run your app with or without the debugger with the new diagnostic tools mode enabled (you can get there using the alt+f2 keyboard shortcut). to get this working while debugging, you will need to configure a special registry key. this requirement will hopefully go away at rtm, but for now you can find the instructions  here  .

while the tools are running, you get a nice graph of memory utilization and can capture heap snapshots. you can see right away how many bytes you’re leaking and how many individual allocations they are coming from. in this case, over a span of just a few seconds we leaked over 2.5mb of memory.

diagnostic tools showing memory accumulation and heap snapshot details

if you pick a specific snapshot, you can see details on what types are in that snapshot and how much memory is taken by objects of each type.

individual heap snapshot listing types, counts, and sizes

heap snapshots can be compared, producing easy to read information on which types of allocations are likely leaking. looks like we have arrays of  cpu_info_internal  and objects of  battery_info_internal  taking up lots of memory. all other types line up with no count delta.

diffing heap snapshots shows which types are likely leaking

and finally, you can dive in and see a list of all objects of a particular type, including their contents and where each individual object was allocated. this is probably the killer feature — you can look at the actual object contents if you like. that’s not something you get out of etw or umdh. you can also double click frames on the allocation call stack to jump to the source code.

object details with allocating call stack and memory contents

it’s worth reiterating that this works with optimizations enabled. the screenshots above were produced using an optimized version. it does have an effect on call stack accuracy if frames are being inlined. in the preceding screenshot, the call stack appears as though  temperatureandbatteryupdaterthread  called  operator new  directly. in fact, it called a constructor for a type called  batteryinformation  , which isn’t visible in the call stack because it was inlined. (you might expect the  /zo  switch to make a difference, but presently it doesn’t. i’ll be sure to check future versions for better inline debugging support.)

wrap up

visual studio 2015 has really nice native memory debugging support. you can capture heap snapshots, compare them, and get to the bottom of memory leaks by looking at diffs and inspecting individual objects and their allocating call stacks. this experience is much more friendly than anything previously available (crt debug heap, umdh, etw) and will make leak analysis more streamlined for all native developers.

Memory (storage engine)

Opinions expressed by DZone contributors are their own.

Related

  • Analyzing “java.lang.OutOfMemoryError: Failed to create a thread” Error
  • Mastering Ownership and Borrowing in Rust
  • Fixing OutOfMemoryErrors in Java Applications
  • Using Heap Dumps to Find Memory Leaks

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!