C 中的 malloc() 和内存使用
我正在尝试使用 malloc 进行实验,看看是否可以分配所有可用的内存。
我使用了以下简单的程序,并有几个问题:
int main(void)
{
char * ptr;
int x = 100;
while(1)
{
ptr = (char *) malloc(x++ * sizeof(char) / 2);
printf("%p\n",ptr);
}
return 0;
}
1)为什么当使用较大的数据类型(int、unsigned long long int、long double)时,进程会使用较少的内存,但使用较小的数据类型(int、char)它会用更多吗?
2) 运行程序时,达到一定数量后会停止分配内存(Windows 7 64 位上约为 592mb,8GB RAM 交换文件设置为系统管理)。打印的输出如果显示 0,则表示 NULL。为什么达到这个阈值后就停止分配内存而不耗尽系统内存和交换区?
我在下面的帖子中发现有人尝试与我相同的事情,但不同之处在于他们没有看到内存使用情况有任何差异,但我是。 使用 malloc 发生内存泄漏失败
我已经在 Linux 内核 2.6.32 上尝试过代码- 5-686 具有相似的结果。
任何帮助和解释将不胜感激。
谢谢,
I was trying an experiment with malloc to see if I could allocate all the memory available.
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?
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). The output of the print if showed 0 which means 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
I've tried the code on Linux kernel 2.6.32-5-686 with similar results.
Any help and explanation would be appreciated.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
1)通常内存是按多个页面分配的,因此如果您要求的大小小于一页,malloc将分配至少一个页面。
2)这是有道理的,因为在多任务系统中,您不是唯一的用户,您的进程也不是唯一正在运行的进程,还有许多其他进程共享一组有限的资源,包括内存。如果操作系统允许一个进程不受任何限制地分配它所需的所有内存,那么它并不是一个好的操作系统,对吗?
最后,在 Linux 中,内核在您实际开始使用该内存之前不会分配任何物理内存页,因此仅调用 malloc 实际上不会消耗任何物理内存,除了跟踪分配本身所需的内存之外课程。不过我不确定 Windows 是否如此。
编辑:
以下示例分配 1GB 虚拟内存
如果运行 top 你会得到
如果你将 malloc 更改为 calloc,然后再次运行 top 你会得到
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.
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. 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. I'm not sure about Windows though.
Edit:
The following example allocates 1GB of virtual memory
If you run top you get
If you change malloc to calloc, and run top again you get
您如何读取内存使用情况?
1) 当使用
char
进行分配时,每次分配所分配的内存比使用long
分配的内存要少(通常是四分之一,但它取决于机器)由于程序本身外部的大多数内存使用工具不会显示已分配的内存,而是实际使用的内存,因此它只会显示 malloc() 本身使用的开销,而不是您 malloc 的未使用的内存。
更多分配,更多开销。
如果您用每次分配的数据填充 malloc 块,那么您应该得到非常不同的结果,以便实际使用内存。
2)我假设您正在从同一个工具中读取该内容?尝试计算实际分配的字节数,它应该显示正确的数量,而不仅仅是“malloc 开销”。
How are you reading your memory usage?
1) When allocating with
char
, you're allocating less memory per allocation than you do with for examplelong
(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.
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.
2) I assume you're reading that from the same tool? Try counting how many bytes you actually allocate instead and it should be showing the correct amount instead of just "malloc overhead".
1) 分配内存时,每次分配都会占用所请求内存的空间加上堆帧的大小。请参阅此处的相关问题
2) 的大小在 Windows 中,任何单个 malloc 都限制为 _HEAP_MAXREQ。请参阅此问题了解更多信息和一些解决方法。
1) When you allocate memory, each allocation takes the space of the requested memory plus the size of a heap frame. See a related question here
2) The size of any single malloc is limited in Windows to _HEAP_MAXREQ. See this question for more info and some workarounds.
1) 这可能是由于内存是分页的并且每个页面具有相同的大小。如果您的数据无法装入页面并落在两页之间,我认为它会移动到下一页的开头,从而在上一页的末尾造成空间损失。
2)阈值较小,因为我认为每个程序都受到一定数量的数据的限制,这不是您拥有的总最大内存。
1) This could come from the fact that memory is paged and that every page has the same size. 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.