tags:

views:

346

answers:

4

I need to know when the memory will be allocated for a particular program. How can i view where the memory is allocated.

+1  A: 

Install Process Explorer, locate your application/process in the list, right click, Properties, Performance tab.

Ash
That only tells you how much the program's allocated, not when. The Performance Graph tab is perhaps a bit better, but it just shows overall use, and generally just grows. Not enough to be much help.
Chris Charabaruk
That's where the quality wristwatch with second hand comes in handy :-)
Ben Hoffstein
@Chris, the Performance tab is dynamically updated, so you can see when memory is allocated. I realise there are more detailed profiler tools out there, however ProcExplorer is a good simple tool.
Ash
+2  A: 

I'll take a stab here and recommend dotTrace, the best profiler I've used. It'll tell you memory usage and a lot more.

Ben Hoffstein
+4  A: 

You'll need to be more specific with the OS, and perhaps language if it's interpreted or run time compiled (ie, PHP, JAVA, .NET, etc).

However, in general:

  • Static and global variables are allocated when the program is loaded into memory.
  • Local variables are allocated on the stack (sometimes heap, depending on compiler) when the function or block is run that instantiates them.
  • At other points in the program memory is allocated when objects are created, and released when they are destroyed (explicitly or through garbage collection)
  • The program may also explicitly allocate memory through malloc or similar memory allocation calls to the OS.

It should be noted that even if memory has been allocated with the OS, it may not yet actually be assigned - the OS waits until the memory is used before it gets a page for it. A memory profiler will help you learn where and when this occurs for a given process.

Where the memory is allocated is a much larger question. There are several points of view to consider:

  • The program's point of view (usually a flat virtual memory area that the program can run around in without colliding with other programs - about 4GB on a 32 bit machine)
  • The OS's point of view, with pages of memory swapped in an out as needed so the programs can pretend they have a nice, flat, unsegmented memory area to play in
  • The CPU's point of view where the memory is contiguous
  • The memory controller's point of view where it may have two 512 sticks and a 1GB stick with an empty slot inbetween

Which perspective are you curious about? Are you writing code that runs within the program of interest, shares memory with it, runs on the same OS, runs on the same CPU, or hooking a logic analyzer up to the memory bus?

Adam Davis
+1  A: 

Just as a cautionary story, even if you don't allocate much yourself, the libraries you use might be doing lots of allocations, so you need something that ties into the kernel or framework. as Ben Hoffstein says dotTrace would probably be a good solution for .Net application (something that I only realized after looking at the question's tags)

Robert Gould