简体   繁体   English

Malloc分配的内存多于RAM

[英]Malloc allocates memory more than RAM

I just executed a program that mallocs 13 MB in a 12 MB machine (QEMU Emulated!) . 我刚刚在一台12 MB的机器上执行了一个mallocs 13 MB的程序(QEMU Emulated!)。 Not just that, i even browsed through the memory and filled junk in it... 不仅如此,我甚至浏览了记忆并填充了垃圾......

void 
large_mem(void) 
{
  #define LONGMEM  13631488
  long long *ptr = (long long *)malloc(LONGMEM);
  long long i;
  if(!ptr) {
     printf("%s(): array allocation of size %lld failed.\n",__func__,LONGMEM);
     ASSERT(0);
  }
  for(i = 0 ; i < LONGMEM ; i++ ) { 
    *(ptr+i)=i;
  }
  free(ptr);
}

How is it possible ? 这怎么可能 ? I was expecting a segmentation fault. 我期待一个分段错误。

It's called virtual memory which is allocated for your program. 它被称为为您的程序分配的虚拟内存 It's not real memory which you call RAM. 它不是你称之为RAM的真实内存。

There is a max limit for virtual memory as well, but it's higher than RAM. 虚拟内存也有最大限制,但它高于RAM。 It's implemented (and defined) by your operating system. 它由您的操作系统实现(和定义)。

This is called as Lazy Allocation. 这称为惰性分配。

Most OS like Linux have an Lazy Allocation memory model wherein the returned memory address is a virtual address and the actual allocation only happens at access-time. 大多数类似Linux的操作系统都有一个Lazy Allocation内存模型,其中返回的内存地址是一个虚拟地址,实际的分配只在访问时发生。 The OS assumes that it will be able to provide this allocation at access-Time. 操作系统假定它能够在访问时提供此分配。

The memory allocated by malloc is not backed by real memory until the program actually touches it. 在程序实际触摸它之前, malloc分配的内存不支持实内存。

While, since calloc initializes the memory to 0 you can be assured that the OS has already backed the allocation with actual RAM (or swap). 同时,由于calloc将内存初始化为0,因此可以确保操作系统已经使用实际RAM(或交换)支持分配。

Try using calloc and most probably it will return you out of memory unless your swap file/partition is big enough to satisfy the request. 尝试使用calloc ,除非你的交换文件/分区足够大以满足请求,否则它很可能会让你失去内存。

Sounds like your operating system is swapping pages : 听起来你的操作系统正在交换页面

Paging is an important part of virtual memory implementation in most contemporary general-purpose operating systems, allowing them to use disk storage for data that does not fit into physical random-access memory (RAM). 在大多数现代通用操作系统中,分页是虚拟内存实现的重要部分,允许它们将磁盘存储用于不适合物理随机存取存储器(RAM)的数据。

In other words, the operating system is using some of your hard disk space to satisfy your 13 MB allocation request (at great expense of speed, since the hard disk is much, much slower than RAM). 换句话说,操作系统正在使用你的一些硬盘空间来满足你的13 MB分配请求(代价很高,因为硬盘比RAM 得多)。

Unless the virtualized OS has swap available, what you're encountering is called overcommit , and it basically exists because the easy way to manage resources in a system with virtual memory and demand/copy-on-write pages is not to manage them. 除非虚拟化操作系统具有交换可用性,否则您遇到的内容称为overcommit ,它基本上存在,因为在具有虚拟内存和请求/写入时复制页面的系统中管理资源的简单方法不是管理它们。 Overcommit is a lot like a bank loaning out more money than it actually has -- it seems to work for a while, then things come crashing down. 过度使用就像一家银行借出的钱多于实际拥有的东西 - 似乎工作了一段时间,然后事情就崩溃了。 The first thing you should do when setting up a Linux system is fix this with the command: 在设置Linux系统时,您应该做的第一件事是使用以下命令解决此问题:

echo "2" > /proc/sys/vm/overcommit_memory

That just affects the currently running kernel; 这只会影响当前运行的内核; you can make it permanent by adding a line to /etc/sysctl.conf : 您可以通过在/etc/sysctl.conf添加一行来使其永久化:

vm.overcommit_memory=2

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

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