是否MMAP或的malloc分配内存? [英] Does mmap or malloc allocate RAM?

查看:235
本文介绍了是否MMAP或的malloc分配内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是一个愚蠢的问题,但我一直在寻找了一段时间,并不能找到一个明确的答案。如果我使用 MMAP 的malloc (在C,在Linux机器上)不任何一个在内存中分配空间?举例来说,如果我有2GB的RAM,想使用所有可用的RAM可我只是用一个的malloc / memset的组合, MMAP ,还是有另一种选择,我不知道的?

I know this is probably a stupid question but i've been looking for awhile and can't find a definitive answer. If I use mmap or malloc (in C, on a linux machine) does either one allocate space in RAM? For example, if I have 2GB of RAM and wanted to use all available RAM could I just use a malloc/memset combo, mmap, or is there another option I don't know of?

我想写一系列可以同时运行,并保持在这个过程中用于强制交换所使用的所有RAM简单的程序,以及输入/输出交换频繁的页面。我用下面的程序尝试这样做了,但它不正是我想要的。它分配内存(RAM),并使用武力交换(如果有足够的实例正在运行),但是当我打电话睡眠不只是从锁定内存正在使用的(所以没有什么实际上正在或换出其他进程?),还是我误解的东西。

I want to write a series of simple programs that can run simultaneously and keep all RAM used in the process to force swap to be used, and pages swapped in/out frequently. I tried this already with the program below, but it's not exactly what I want. It does allocate memory (RAM?), and force swap to be used (if enough instances are running), but when I call sleep doesn't that just lock the memory from being used (so nothing is actually being swapped in or out from other processes?), or am I misunderstanding something.

例如,如果我跑这3次我会使用2GB的内存(全部)前两个实例,然后第三个实例将交换previous一两个实例出来(内存)和当前实例到RAM?或将例如#3只使用磁盘或虚拟内存中运行?

For example, if I ran this 3 times would I be using 2GB (all) of RAM from the first two instances, and the third instance would then swap one of the previous two instances out (of RAM) and the current instance into RAM? Or would instance #3 just run using disk or virtual memory?

这带来了另一个问题,我需要分配足够的内存来使用所有可用的虚拟内存,以及用于交换分区中使用?

This brings up another point, would I need to allocate enough memory to use all available virtual memory as well for the swap partition to be used?

最后,将 MMAP (或任何其他的C函数。见鬼,即使其他语言如适用)是这样做的更好呢?

Lastly, would mmap (or any other C function. Hell, even another language if applicable) be better for doing this?

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 

#define MB(size) ( (size) * 1024 * 1024)
#define GB(size) ( (size) * 1024 * 1024 * 1024)


int main(){
    char *p;
    p = (char *)malloc(MB(512));
    memset(p, 'T', MB(512));
    printf(".5 GB allocated...\n");

    char *q;
    q = (char *)malloc(MB(512));
    memset(q, 'T', MB(512));
    printf("1 GB allocated...\n");
    printf("Sleeping...\n");

    sleep(300);
}

**编辑:我使用CentOS的6.4(带3.6.0内核)为我的操作系统,是否可以帮助任何

** I am using CentOS 6.4 (with 3.6.0 kernel) for my OS, if that helps any.

推荐答案

这是很OS /机器相关的。

This is very OS/machine dependent.

在大多数操作系统没有分配内存。他们都分配VM空间。他们让你的流程的虚拟内存在一定范围内有效使用。 RAM通常是由第一次写操作系统以后分配。在此之前的分配不会(从页表,列出了它们作为有效的VM空间除外)使用RAM。

In most OSes neither allocates RAM. They both allocate VM space. They make a certain range of your processes virtual memory valid for use. RAM is normally allocated later by the OS on first write. Until then those allocations do not use RAM (aside from the page table that lists them as valid VM space).

如果要分配的物理内存,那么你必须使每个页面(的sysconf(_SC_PAGESIZE)为您提供了系统PAGESIZE)脏了。

If you want to allocate physical RAM then you have to make each page (sysconf(_SC_PAGESIZE) gives you the system pagesize) dirty.

在Linux上,你可以看到你的VM映射与的/ proc /自/ smaps所有细节 RSS 是居民设置映射(多少是驻留在RAM),其他一切是脏的将被换出。所有非脏内存将可用,但不会存在,直到然后。

In Linux you can see your VM mappings with all details in /proc/self/smaps. Rss is your resident set of that mapping (how much is resident in RAM), everything else that is dirty will have been swapped out. All non-dirty memory will be available for use, but won't exist until then.

您可以使所有网页脏的东西,如

You can make all pages dirty with something like

size_t mem_length;
char (*my_memory)[sysconf(_SC_PAGESIZE)] = mmap(
      NULL
    , mem_length
    , PROT_READ | PROT_WRITE
    , MAP_PRIVATE | MAP_ANONYMOUS
    , -1
    , 0
    );

int i;
for (i = 0; i * sizeof(*my_memory) < mem_length; i++) {
    my_memory[i][0] = 1;
}

在一些实现这也可以通过将 MAP_POPULATE 标志 MMAP ,但实现(具体取决于您的系统),它可能只是失败 MMAP ENOMEM 如果你尝试过,你有可用的RAM映射等等。

On some Implementations this can also be achieved by passing the MAP_POPULATE flag to mmap, but (depending on your system) it may just fail mmap with ENOMEM if you try to map more then you have RAM available.

这篇关于是否MMAP或的malloc分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆