xtofl
xtofl

Reputation: 41509

Conditional breakpoint when heap > some limit

Is it possible to break into debugger when the allocated memory of attached-to process becomes bigger than a certain value?

Preferrably using Visual Studio 2005, but other IDE's/debuggers are an option.

Upvotes: 0

Views: 178

Answers (3)

ScaledLizard
ScaledLizard

Reputation: 181

The question may be old, but it is still relevant.

You can check available memory using these functions:

https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-globalmemorystatusex

https://learn.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getprocessmemoryinfo

Then, either call that function from your main thread occasionally or run a separate thread to monitor the main thread. You can have a breakpoint in the monitoring code to break execution if memory use exceeds a defined threshold.

Upvotes: 0

Mitch Wheat
Mitch Wheat

Reputation: 300489

I don't know of any direct way in Visual Studio, but you could use ProcDump to create a crash dump when the Memory commit threshold reaches a certain value (-m option).

You would then need to use WinDbg (part of the Windows debugging tools) to inspect the heap.

Upvotes: 1

Dialecticus
Dialecticus

Reputation: 16761

There is no direct way to do it. Alternative is to set ordinary breakpoint somewhere inside CRT allocation code, and set it to break when the hit count is multiple of say 2000. You'll get to wanted state quickly enough.

Upvotes: 2

Related Questions