0

I have a simple program and set a breakpoint to see how to monitor memory allocation within a debugging interface (instead of, for example, with valgrind):

enter image description here

The above program should allocate a lot of memory. It seems like the "Memory" icon on the left is unrelated to dynamic memory allocation (perhaps it's related to the amount of memory Xcode itself is using). Is there a command to see how much memory has been allocated within lldb, for example, similar to (gdb) call malloc_stats()? I know there is the Instruments application (which I've used), but I'm more interested in being able to step through the total memory usage while debugging it. Or, is there some GUI command/panel that will show this?

  • you probably need to dirty the pages -- write something to them -- to get your memory use to go up. Try writing something every 16k in your allocations. – Jason Molenda Oct 06 '19 at 20:35
  • @JasonMolenda -- what do you mean by "every 16k in your allocations"? Would memset across all work? Or would I need to chunk the writes up. –  Oct 06 '19 at 21:47
  • I think he means in your "alloc" function just write to each block (s[0] = 'z') as it is returned. The kernel is probably being clever and not committing memory allocations you haven't yet written to. – Jim Ingham Oct 07 '19 at 18:56

1 Answers1

1

The memory gauge does reflect the memory your program allocates. If you do what Jason suggests, you will see that rise as you allocate more memory.

There are a couple of commands: "memory region" and "malloc_info", that will tell you the allocation history or state of memory containing a particular address. But lldb doesn't have a general "show me all allocated memory regions" command, like malloc_stats.

You can also use the Xcode "Debug Memory Graph" button to view the memory allocations - including all the Malloc blocks - in your program.

Jim Ingham
  • 25,260
  • 2
  • 55
  • 63
  • Thanks -- can you show where the "Debug Memory Graph" button is or how to access/use that command/button? –  Oct 08 '19 at 16:08
  • It is the button with three circles connected in a open triangle on the debug control bar - in the section after the stepping controls. If you want to see stacks for each allocation, you need to turn on Malloc Stack in the Diagnostics tab of the Run scheme of the Xcode Target you are debugging. The docs for it are here: https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/debugging_with_xcode/chapters/special_debugging_workflows.html#//apple_ref/doc/uid/TP40015022-CH9-DontLinkElementID_1. – Jim Ingham Oct 08 '19 at 22:34