0

I would like to find out the amount of bytes used by a process from within a C++ program by inspecting the operating system's memory information. The reason I would like to do this is to find a possible overhead in memory allocation when allocating memory (due to memory control blocks/nodes in free lists etc.) Currently I am on mac and am using this code:

#include <mach/mach.h>
#include <iostream>

int getResidentMemoryUsage() {
    task_basic_info t_info;
    mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;

    if (task_info(mach_task_self(), TASK_BASIC_INFO,
reinterpret_cast<task_info_t>(&t_info),
                  &t_info_count) == KERN_SUCCESS) {

        return t_info.resident_size;
    }
    return -1;
}


int getVirtualMemoryUsage() {
    task_basic_info t_info;
    mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;

    if (task_info(mach_task_self(), TASK_BASIC_INFO,
reinterpret_cast<task_info_t>(&t_info),
                  &t_info_count) == KERN_SUCCESS) {

        return t_info.virtual_size;
    }
    return -1;
}

int main(void) {
  int virtualMemoryBefore = getVirtualMemoryUsage();
  int residentMemoryBefore = getResidentMemoryUsage();

  int* a = new int(5);

  int virtualMemoryAfter = getVirtualMemoryUsage();
  int residentMemoryAfter = getResidentMemoryUsage();

  std::cout << virtualMemoryBefore << " " << virtualMemoryAfter << std::endl;
  std::cout << residentMemoryBefore << " " << residentMemoryAfter << std::endl;

  return 0;
}

When running this code I would have expected to see that the memory usage has increased after allocating an int. However when I run the above code I get the following output:

75190272 75190272
819200 819200

I have several questions because this output does not make any sense.

  1. Why hasn't either the virtual/resident memory changed after an integer has been allocated?

  2. How come the operating system is allocating such large amounts of memory to a running process.

When I do run the code and check activity monitor I find that 304 kb of memory is used but that number differs from the virtual/resident memory usage obtained programmatically.

  1. My end goal is to be able to find the memory overhead when assigning data, so is there a way to do this (i.e. determine the bytes used by the OS and compare with the bytes allocated to find the difference is what I am currently thinking of)

Thank you for reading

Raees Rajwani
  • 487
  • 5
  • 18
  • 3
    The memory that your OS allocates to a process is **not** a good tool for measuring the memory requested by your program. – Drew Dormann Mar 12 '18 at 18:04
  • 4
    That `int` can probably fit on a page that's already been allocated. Try a larger allocation. – François Andrieux Mar 12 '18 at 18:04
  • Notice that those numbers are both exact multiples of 1024. – François Andrieux Mar 12 '18 at 18:09
  • @FrançoisAndrieux If the int is only 4 bytes and a page is 4kb would there be a way to query the amount of data used by the current process in a single page? – Raees Rajwani Mar 12 '18 at 18:16
  • If you never write to your allocated memory, chances are the OS will never bother backing the allocation with physical pages (since it doesn't need to until you write). – Jesper Juhl Mar 12 '18 at 19:01
  • @JesperJuhl That's true for many Linux distributions ([optimistic allocation](https://stackoverflow.com/questions/1655650/linux-optimistic-malloc-will-new-always-throw-when-out-of-memory)), but it's not the norm. c++ expects that writing to memory that was successfully allocated should succeed. – François Andrieux Mar 12 '18 at 19:09

2 Answers2

1

The C++ runtime typically allocates a block of memory when a program starts up, and then parcels this out to your code when you use things like new, and adds it back to the block when you call delete. Hence, the operating system doesn't know anything about individual new or delete calls. This is also true for malloc and free in C (or C++)

  • If the C++ runtime allocates a large block of memory and parcels it out like a pool allocator, would there be any way to determine the bytes lost to storing metadata about the allocations? – Raees Rajwani Mar 12 '18 at 18:13
  • @Raees Only if your implementation supplies such a thing, which it almost certainly won't. Or if you have the source code for the allocator. –  Mar 12 '18 at 18:16
0

First you measure number of pages, not really memory allocated. Second the runtime pre allocates few pages at startup. If you want to observe something allocate more than a single int. Try allocating several thousands and you will observe some changes.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69