简体   繁体   English

分配内存时内存使用不会增加

[英]Memory usage doesn't increase when allocating memory

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. 我想通过检查操作系统的内存信息来找出C ++程序中某个进程使用的字节数。 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: 我想这样做的原因是在分配内存时发现内存分配中可能的开销(由于空闲列表中的内存控制块/节点等。)目前我在Mac上并且正在使用以下代码:

#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. 运行此代码时,我曾希望看到分配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. 当我运行代码并检查活动监视器时,我发现使用了304 kb的内存,但是该数量与通过编程获得的虚拟/驻留内存使用量不同。

  1. My end goal is to be able to find the memory overhead when assigning data, so is there a way to do this (ie 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 感谢您的阅读

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 . C ++运行时通常在程序启动时分配一个内存块,然后在使用诸如new东西时将其打包到您的代码中,并在调用delete时将其重新添加到该块中。 Hence, the operating system doesn't know anything about individual new or delete calls. 因此,操作系统对单个new呼叫或delete呼叫一无所知。 This is also true for malloc and free in C (or C++) 这也是真正mallocfree在C(或C ++)

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. 如果您想观察某些东西,则分配的不仅仅是一个int。 Try allocating several thousands and you will observe some changes. 尝试分配几千个,您将观察到一些变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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