简体   繁体   English

C和内存使用情况中的malloc()

[英]malloc() in C And Memory Usage

I was trying an experiment with malloc to see if I could allocate all the memory available. 我正在尝试使用malloc进行实验,以查看是否可以分配所有可用内存。

I used the following simple program and have a few questions: 我使用了以下简单程序,并有几个问题:

int main(void)
{
    char * ptr;
    int x = 100;

    while(1)
    {
        ptr = (char *) malloc(x++ * sizeof(char) / 2);
        printf("%p\n",ptr);
    }

    return 0;
}

1) Why is it that when using larger data types(int, unsigned long long int, long double) the process would use less memory but with smaller data types (int, char) it would use more? 1)为什么当使用较大的数据类型(int,unsigned long long int,long double)时,该进程将使用较少的内存,而使用较小的数据类型(int,char)将使用更多的内存?

2) When running the program, it would stop allocating memory after it reached a certain amount (~592mb on Windows 7 64-bit with 8GB RAM swap file set to system managed). 2)运行该程序时,它将在达到一定数量后停止分配内存(在Windows 7 64位系统上约为592mb,并将8GB RAM交换文件设置为系统托管)。 The output of the print if showed 0 which means NULL. 如果输出为0,则表示NULL。 Why does it stop allocating memory after a reaching this threshold and not exhaust the system memory and swap? 为什么在达到此阈值后停止分配内存,而没有耗尽系统内存并进行交换?

I found someone in the following post trying the same thing as me, but the difference they were not seeing any difference in memory usage, but I am. 我在以下文章中发现有人尝试与我做相同的事情,但是区别是他们没有看到内存使用方面的任何区别,但是我同意。 Memory Leak Using malloc fails 使用malloc的内存泄漏失败

I've tried the code on Linux kernel 2.6.32-5-686 with similar results. 我已经在Linux内核2.6.32-5-686上尝试过该代码,但结果相似。

Any help and explanation would be appreciated. 任何帮助和解释将不胜感激。

Thanks, 谢谢,

1)Usually memory is allocated in multiples of pages, so if the size you asked for is less than a page malloc will allocate at least one page. 1)通常,内存是在多个页面中分配的,因此,如果您要求的大小小于一个页面,则malloc将分配至少一个页面。

2)This makes sense, because in a multitasking system, you're not the only user and your process is not the only process running, there are many other processes that share a limited set of resources, including memory. 2)这是有道理的,因为在多任务系统中,您不是唯一的用户,并且您的进程不是唯一正在运行的进程,还有许多其他进程共享有限的资源集,包括内存。 If the OS allowed one process to allocate all the memory it needs without any limitation, then it's not really a good OS, right ? 如果操作系统允许一个进程无限制地分配其所需的所有内存,那么它并不是一个好的操作系统,对吗?

Finally, in Linux, the kernel doesn't allocate any physical memory pages until after you actually start using this memory, so just calling malloc doesn't actually consume any physical memory, other than what is required to keep track of the allocation itself of course. 最后,在Linux中,内核直到您真正开始使用该内存后才分配任何物理内存页面,因此仅调用malloc并不会实际上消耗任何物理内存,除了跟踪其自身分配所需的内容外课程。 I'm not sure about Windows though. 我不确定Windows。

Edit: The following example allocates 1GB of virtual memory 编辑:下面的示例分配1GB的虚拟内存

#include <stdio.h>
int main(int agrc, char **argv)
{
    void *p = malloc(1024*1024*1024);
    getc(stdin);
}

If you run top you get 如果你跑得最好,你会得到

 top -p `pgrep test` PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 20 0 1027m 328 252 S 0 0.0 0:00.00 test 

If you change malloc to calloc, and run top again you get 如果将malloc更改为calloc,然后再次运行top,则得到

 top -p `pgrep test` PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 20 0 1027m 1.0g 328 S 0 1.3 0:00.08 test 

How are you reading your memory usage? 您如何阅读内存使用情况?

1) When allocating with char , you're allocating less memory per allocation than you do with for example long (one quarter as much, usually, but it's machine dependent) Since most memory usage tools external to the program itself don't show allocated memory but actually used memory, it will only show the overhead malloc() itself uses instead of the unused memory you malloc'd. 1)使用char分配时,您为每个分配分配的内存要少于例如分配的long (通常是四分之一,但是取决于计算机),这是因为程序本身外部的大多数内存使用工具都不显示已分配内存,但实际使用了内存,它将仅显示开销malloc()本身使用的内存,而不是您已分配的未使用的内存。

More allocations, more overhead. 更多分配,更多开销。

You should get a very different result if you fill the malloc'd block with data for each allocation so the memory is actually used. 如果为每个分配用数据填充malloc'd块,那么实际使用的内存应该得到非常不同的结果。

2) I assume you're reading that from the same tool? 2)我假设您正在从同一工具中读取内容? Try counting how many bytes you actually allocate instead and it should be showing the correct amount instead of just "malloc overhead". 尝试计算实际分配的字节数,它应该显示正确的数量,而不仅仅是“ malloc开销”。

1) When you allocate memory, each allocation takes the space of the requested memory plus the size of a heap frame. 1)分配内存时,每次分配都占用请求内存的空间加上堆帧的大小。 See a related question here 在这里查看相关问题

2) The size of any single malloc is limited in Windows to _HEAP_MAXREQ. 2)在Windows中,任何单个malloc的大小都被限制为_HEAP_MAXREQ。 See this question for more info and some workarounds. 有关更多信息和一些解决方法,请参见此问题

1) This could come from the fact that memory is paged and that every page has the same size. 1)这可能是由于内存已分页并且每个页面都具有相同的大小。 If your data fails to fit in a page and falls 'in-between' two pages, I think it is move to the beginning of the next page, thus creating a loss of space at the end of previous page. 如果您的数据无法放入一个页面并位于两个页面之间,我认为它已移至下一页的开始,从而在前一页的末尾造成了空间的损失。

2) The threshold is smaller because I think every program is restricted to a certain amount of data that is not the total maximum memory you have. 2)阈值较小,因为我认为每个程序仅限于一定数量的数据,而不是您拥有的总最大内存。

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

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