Linux malloc 返回内存或虚拟地址空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5728077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
malloc returns memory or virtual address space
提问by Lukasz Madon
Does malloc allocate a block of memory on the heap or should it be called Virtual Address Space?
malloc 是在堆上分配一块内存还是应该将其称为虚拟地址空间?
Am I being picky calling it Virtual Address Space or this just the legacy of DOS? How about Linux?
我是挑剔称它为虚拟地址空间还是这只是 DOS 的遗产?Linux呢?
EDIT:
编辑:
many answers with great details, none of them answer my question, though.
许多答案都很详细,但没有一个回答我的问题。
采纳答案by Lukasz Madon
To answer this question, we need to know what kind of Operating System and architecture we are dealing with. As pmg mention the Standard and articles refers to “storage” or “space”. These are the most general terms and the only valid ones, unless we make a bunch of assumptions.
要回答这个问题,我们需要知道我们正在处理什么样的操作系统和架构。正如 pmg 提到的标准和文章是指“存储”或“空间”。这些是最通用的术语,也是唯一有效的术语,除非我们做出一系列假设。
For example:
例如:
malloc allocates a block of Virtual Address Space on the heap
malloc 在堆上分配一块虚拟地址空间
This is not correct for many embedded systems. Many of them do not use Virtual Memory, because there is no need (no multitasking etc.) or for performance reasons. What is more, it is possible that some exotic device does not have the idea of heap – doubt that malloc would be used, but fairly that is one of the reasons why the Standard refers to “storage” - it is implementation-specific.
这对于许多嵌入式系统来说是不正确的。他们中的许多人不使用虚拟内存,因为没有必要(没有多任务处理等)或出于性能原因。更重要的是,一些奇特的设备可能没有堆的概念——怀疑是否会使用 malloc,但公平地说,这就是标准提到“存储”的原因之一——它是特定于实现的。
On the other hand, the example is correct for Window and Linux in our PCs. Let's analyze it to answer the question.
另一方面,该示例适用于我们 PC 中的 Window 和 Linux。让我们分析它来回答这个问题。
First off, we need to define what is Virtual Address Space.
首先,我们需要定义什么是虚拟地址空间。
Virtual Address Space (VAS) is a memory mapping mechanism that helps with managing multiple processes.
虚拟地址空间 (VAS) 是一种内存映射机制,有助于管理多个进程。
- isolate processes – each of them has his own address space
- allow to allocate memory that 32-bit architecture is limited to.
- provides one concise model of memory
- 隔离进程——每个进程都有自己的地址空间
- 允许分配 32 位架构受限的内存。
- 提供一种简洁的记忆模型
Back to question, ”Does malloc allocate a block of memory on the heap or should it be called Virtual Adress Space ?”
回到问题,“malloc 是在堆上分配一块内存还是应该叫它虚拟地址空间?”
Both statements are correct. I would rather say VAP instead of memory – it is more explicit. There is a common myth that malloc = RAM memory. Back in old day, DOS memory allocation was very simple. Whenever we ask for memory it was always RAM, but in modern Oses it may vary.
这两种说法都是正确的。我宁愿说 VAP 而不是记忆——它更明确。有一个普遍的误解,认为 malloc = RAM 内存。回到过去,DOS 内存分配非常简单。每当我们要求内存时,它总是 RAM,但在现代 Oses 中它可能会有所不同。
回答by NPE
malloc
allocates a block on the heap. For each memory page that the allocated block spans, there may or may not be physical memory committed to it at the outset. The entire block is usable though, since the OS will take care of handling page faults and managing physical/virtual memory that's needed to support the allocation.
malloc
在堆上分配一个块。对于分配块跨越的每个内存页,一开始可能有也可能没有物理内存提交给它。整个块虽然可用,因为操作系统将负责处理页面错误和管理支持分配所需的物理/虚拟内存。
回答by Sadique
Kernel and user space work with virtual addresses (also called linear addresses) that are mapped to physical addresses by the memory management hardware. This mapping is defined by page tables, set up by the operating system.
内核和用户空间使用由内存管理硬件映射到物理地址的虚拟地址(也称为线性地址)。 此映射由页表定义,由操作系统设置。
Go through this link on Memory Allocation.
通过这个关于内存分配的链接。
回答by Sadique
The answer depends on the underlying OS, libc implementation, and hardware architecture. With most modern OS'es (like Linux or Windows) running on the x86 architecture you will get a pointer within the linear address space, but generally this is implementation dependent. I doubt that when, for example, programming some small device (like a microcontroller) in C, malloc() would return a pointer to virtual memory, because there is no virtual memory as such.
答案取决于底层操作系统、libc 实现和硬件架构。对于在 x86 架构上运行的大多数现代操作系统(如 Linux 或 Windows),您将在线性地址空间内获得一个指针,但通常这取决于实现。我怀疑,例如,当用 C 编程一些小设备(如微控制器)时, malloc() 会返回一个指向虚拟内存的指针,因为没有虚拟内存。
回答by doron
All processes run within its own virtual address space. Each access to memory is mediated by the memory management unit. If memory is mapped, the data is either loaded or stored from the corresponding physical address. If no memory is mapped to the specified address, the (Memory Management Unit (MMU) will trigger an exception.
所有进程都在其自己的虚拟地址空间内运行。每次对内存的访问都由内存管理单元进行调解。如果内存被映射,数据要么从相应的物理地址加载要么存储。如果没有内存映射到指定地址,(Memory Management Unit (MMU)) 将触发异常。
Malloc manages a bunch (or perhaps even just a fraction) of mapped memory pages. These pages are known as the heap. When one requests a number of bytes from malloc, malloc will either find that memory within the pages that it already manages or it will ask the operating system (using either brk or mmap on linux). This is totally transparent to the user of malloc.
Malloc 管理一堆(甚至可能只是一小部分)映射的内存页面。这些页面称为堆。当从 malloc 请求多个字节时,malloc 将在它已经管理的页面中找到该内存,或者它会询问操作系统(在 linux 上使用 brk 或 mmap)。这对 malloc 的用户是完全透明的。
So the two concepts are totally orthogonal. Processes access virtual memory which the MMU may translate into a physical address and the heap is the block of memory managed by malloc.
所以这两个概念是完全正交的。进程访问虚拟内存,MMU 可以将其转换为物理地址,而堆是由 malloc 管理的内存块。
回答by R.. GitHub STOP HELPING ICE
There are at least 3 ways of measuring memory consumption:
至少有 3 种方法可以测量内存消耗:
- virtual address space - the amount of your process's address space consumed by the allocation. this also affects fragmentation and the maximum contiguous future allocations you can make.
- commit charge - this is the operating system's accounting of the maximum possible physical storage required to maintain all of the writable, non-file/device-backed memory allocated to your process. if the OS allows it to exceed the total physical memory + swap, very bad things could happen the first time the excess is written to.
- physical memory - the amount of physical resources (potentially including swap, depending on your interpretation) your process is currently occupying. This could be less than commit charge due to virgin zero pages and virgin private writable maps of files, or more than commit charge due to non-writable or shared mappings the process is using (but these are usually swappable/discardable).
- 虚拟地址空间 - 分配消耗的进程地址空间量。这也会影响碎片化和您可以进行的最大连续未来分配。
- 提交费用 - 这是操作系统对维护分配给您的进程的所有可写、非文件/设备支持的内存所需的最大可能物理存储的计算。如果操作系统允许它超过总物理内存 + 交换,那么第一次写入超出的部分时可能会发生非常糟糕的事情。
- 物理内存 - 您的进程当前占用的物理资源量(可能包括交换,取决于您的解释)。由于原始零页和原始私有可写文件映射,这可能低于提交费用,或者由于进程使用的不可写或共享映射(但这些通常是可交换/可丢弃的)而高于提交费用。
malloc
generally affects them all.
malloc
一般都会影响到他们。
Edit:So, the best way I can think to answer your question is to say:
编辑:所以,我认为回答您问题的最佳方式是说:
malloc
allocates virtual memory.
malloc
分配虚拟内存。
And virtual memory consumes:
并且虚拟内存消耗:
- virtual address space,
- commit charge, and
- physical resources, if it's been written to.
- 虚拟地址空间,
- 承担费用,和
- 物理资源(如果已写入)。
回答by hawk
Does malloc allocate a block of memory on the heap or should it be called virtual adress space?
malloc 是在堆上分配一块内存还是应该称为虚拟地址空间?
short answer: malloc allocates memory on the heap.
简短回答: malloc 在堆上分配内存。
it's not precise enoughto say that malloc allocates memory in the virtual adress[sic] space, since your call stack itself is part of that same space.
它不是足够精确的说在虚拟ADRESS [原文]空间的malloc分配内存,因为调用栈本身是同一空间的一部分。
回答by Gautam
Malloc allocates on the heap, which is a part of the virtual address space.
Malloc 在堆上分配,堆是虚拟地址空间的一部分。
You are not being picky by calling it virtual address space, you are just being too general. It can be compared to saying, "You puke in the bathroom." Strictly speaking that is true but "You puke in the pot" is more precise because the former statement implies that you can puke in the sink or tub too.
称其为虚拟地址空间并不是很挑剔,只是太笼统了。这可以比作说,“你在浴室里吐了。” 严格来说这是真的,但“你在锅里呕吐”更准确,因为前一句话暗示你也可以在水槽或浴缸里呕吐。
Conceptually, malloc, the heapand virtual memoryare supported by most operating systems including Dos and Linux.
回答by karlphillip
malloc()
does allocate a block of memory on the HEAP.Should it be called virtual address space? Hold that thought for a second. VAS(virtual address space) is a memory mapping mechanism that comprises the entire memory space of an application. In other words, VAS is not restricted to the memory area of the HEAP. The HEAP is actually just another part of it.
malloc()
确实在 HEAP 上分配了一块内存。它应该被称为虚拟地址空间吗?保持这个想法一秒钟。VAS(虚拟地址空间)是一种内存映射机制,包含应用程序的整个内存空间。换句话说,VAS 不限于 HEAP 的内存区域。HEAP 实际上只是其中的一部分。
Each time a new application is run, the OS creates a new process and allocates a new VAS for the application. Memory allocated through malloc()
is reserved on the HEAP, which is a special memory region within the VAS, as you know, and memory allocated through standard means ends up in the stack, which is another region of memory located inside the VAS of the application.
每次运行新应用程序时,操作系统都会创建一个新进程并为该应用程序分配一个新的 VAS。通过分配的内存malloc()
保留在 HEAP 上,这是 VAS 中的一个特殊内存区域,如您所知,通过标准方式分配的内存最终在堆栈中,这是位于应用程序 VAS 内部的另一个内存区域。
回答by Ciaran McHale
You could have answered this question yourself if you had bothered to RTFM :-)
如果您对 RTFM 感到困扰,您本可以自己回答这个问题 :-)
In particular, typing man malloc
on a Linux machine and searching (one-at-a-time) for "heap" and "virtual" will let you see unambiguously that malloc()
is defined in terms of heapmemory, rather than virtualmemory.
特别是,man malloc
在 Linux 机器上键入并搜索(一次一个)“堆”和“虚拟”将使您清楚地看到malloc()
根据堆内存而不是虚拟内存定义的。
The Wikipedia articlefor malloc()
agrees with the Linux man page. It states (emphasis is mine):
在维基百科文章的malloc()
同意Linux手册页。它指出(重点是我的):
In C, the library function
malloc
is used to allocate a block of memory on the heap. [...] Some platforms provide library calls which allow run-time dynamic allocation from the C stack rather than the heap (e.g. Unixalloca()
, Microsoft Windows CRTL'smalloca()
). This memory is automatically freed when the calling function ends. The need for this is lessened by changes in the C99 standard, which added support for variable-length arrays of block scope having sizes determined at runtime.
在 C 中,库函数
malloc
用于在堆上分配一块内存。[...] 一些平台提供库调用,允许从 C 堆栈而不是堆(例如 Unixalloca()
、Microsoft Windows CRTL'smalloca()
)进行运行时动态分配 。当调用函数结束时,该内存会自动释放。C99 标准中的更改减少了对此的需求,该标准增加了对块范围的可变长度数组的支持,该数组的大小在运行时确定。
If you are confused about the meaning of terminology, then the Wikipedia articles on heap memoryand virtual memorymay help you.
回答by Hans Lub
malloc
allocates memory on the heap, period.
malloc
在堆上分配内存,期间。
Your C library typically keeps a list (or some more intricate data structure) of available memory chunks, finding a suitable chunk to satisfy a malloc
(possibly splitting a larger chunk into a number of smaller ones) and returning free
'd memory to the list (possibly merging a few smaller chunks into a bigger one)
您的 C 库通常保留可用内存块的列表(或一些更复杂的数据结构),找到合适的块来满足malloc
(可能将较大的块拆分为许多较小的块)并将free
“d 内存”返回到列表(可能将几个较小的块合并成一个更大的块)
Onlywhen the list doesn't contain a large enough chunk to satisfy your malloc
, the library will ask the OS for more memory, e.g. using the sbrksyscall. The address returned by this syscall may be a virtual address, or a real one, depending on your hardware, but as a programmer you cannot (and don't need to) know this.
只有当列表不包含足够大的块来满足您的 . 时malloc
,库才会要求操作系统提供更多内存,例如使用sbrk系统调用。此系统调用返回的地址可能是虚拟地址,也可能是真实地址,具体取决于您的硬件,但作为程序员,您不能(也不需要)知道这一点。
Saying that malloc
allocates virtual adress space rather than a block on the heap is like saying that read
reads from your hard disk rather than from a file: it is irrelevant from the caller's perspective, and not always true.
说malloc
分配虚拟地址空间而不是堆上的块就像说read
从硬盘读取而不是从文件读取:从调用者的角度来看,这无关紧要,并不总是正确的。