4

I have to debug program that rapidly allocates memory sometimes (Not by design.) and when it happens my whole computer just stop responding because physical memory goes 100% (I have 4GB ram), then I have to press the restarting button everytime with no way to know why did it happen.

Is there a way to limit new's or malloc's heap's size? By limiting I mean that it will throw exception like C#'s OutOfMemoryException. NOTE: I can't just pick all the news and mallocs and replace it with customized allocator, it's a lot of work there.

I tried setting Project Properties -> Configuration Properties -> Linker -> System -> Heap Reserve\Commit Size to 256MB or 256000000 but nothing works.

LyingOnTheSky
  • 2,844
  • 1
  • 14
  • 33
  • If the program starts allocating memory and completely stop responding, it may be an infinite loop somewhere. If this is the case, why you don't try to debug it instead? – Vinícius Gobbo A. de Oliveira Nov 13 '14 at 11:57
  • /HEAP (Set Heap Size) http://msdn.microsoft.com/en-us/library/f90ybzkh.aspx – Alex F Nov 13 '14 at 11:57
  • @ViníciusGobboA.deOliveira You didn't understood, my whole computer stopping to respond. I can't pinpoint the line because I have to press the restart button to continue work. – LyingOnTheSky Nov 13 '14 at 11:58
  • @AlexFarber Tried it, it doesn't help somehow. – LyingOnTheSky Nov 13 '14 at 12:00
  • If you switch to Linux you could use [setrlimit(2)](http://man7.org/linux/man-pages/man2/setrlimit.2.html) thru the `ulimit` bash shell builtin. Yet another reason to try Linux! – Basile Starynkevitch Nov 13 '14 at 12:02
  • @BasileStarynkevitch Unfortuately I need to program on&to Windows. Off-topic: I tried Linux for half a year. – LyingOnTheSky Nov 13 '14 at 12:03
  • You probably could redefine `::operator new` in a system specific way ... – Basile Starynkevitch Nov 13 '14 at 12:05
  • See also this queston: The AppVerifier answer may be good for you. http://stackoverflow.com/questions/192876/set-windows-process-or-user-memory-limit – Ben Nov 13 '14 at 12:05
  • See answers here http://stackoverflow.com/questions/192876/set-windows-process-or-user-memory-limit on ideas on limiting memory usage – Roman R. Nov 13 '14 at 12:07
  • @Ben Is this a duplicate? – LyingOnTheSky Nov 13 '14 at 12:11
  • @LyingOnTheSky, not really, because he doesn't need to limit the memory usage of the app, he needs to diagnose the reason for it. – Ben Nov 13 '14 at 13:42
  • @Ben I need it too. The question is more about to set memory limit per user. Allow the user 90% so the system will always have 10%. (?) – LyingOnTheSky Nov 13 '14 at 13:45
  • @LyingOnTheSky sorry didn't notice it was your question. Your problem is that the application is using loads of memory and it shouldn't be. You need to find out why it is behaving badly and stop it. If you do, you won't have the other problem any more. – Ben Nov 13 '14 at 14:01
  • 1
    @Ben Yeah mine is for debugging purpose. (I didn't saw the difference somehow) – LyingOnTheSky Nov 13 '14 at 14:02

1 Answers1

8

Yes, use the Debug Heap hooks in the CRT.

You can hook malloc to breakpoint when you allocate a large block, using _CrtSetAllocHook and _CrtDbgBreak. Or if your problem is lots of small blocks, you can set a breakpoint on the 10,000th allocation (for example) using _CrtSetBreakAlloc.

Ben
  • 34,935
  • 6
  • 74
  • 113