简体   繁体   中英

Information on the virtual memory my program is using?

Is there a way I can get information on the virtual memory my application is using?

As far as i'm aware there are several segments of memory that a program uses, such as code segment, data segments and the heap, stack etc.

Is there a way I can get information on the addresses where these segments start and end?

To give some context i'm trying to draw an image which contains a visual representation of the virtual memory of my program (a kind of graph). So i need the start and end addresses of the different segments, and if possible a way to determine if an address is memory is currently being used.

i'm using c++ on windows btw.

You can query memory layout of a process by calling VirtualQueryEx . It returns the memory map layout you wanted.

The returned MEMORY_BASIC_INFORMATION table list of sections of memory with their protection attributes.

However, the machine works on protection attributes not by its nature(code, data, rodata, bss, heap, etc), you can only guess on them:

  • PAGE_EXECUTE: code
  • PAGE_READONLY: rodata
  • PAGE_READWRITE: data, bss, heap, stack
  • PAGE_WRITECOPY: data, bss, heap, stack

However, the above guess would not be reliable if the process did it's own customized VirtualAllocate or file mapping, to increase the accurancy you may also query the executable name with GetModuleFileName and parse the file, then compare with the table (note the address space randomization)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM