能有多大一个malloc在C? [英] How big can a malloc be in C?

查看:235
本文介绍了能有多大一个malloc在C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个malloc使用C就是26901 ^ 2 * sizeof的(双)

I have a malloc in C that is 26901^2*sizeof(double)

这让我想什么最大的价值会在这里?

This got me thinking what the largest value can be here?

另外,我会定义一个宏来访问此二维数组什么问题?

Also, would I have any problems defining a macro to access this 2D array?

 #define DN(i,j) ((int)i * ny + (int)j)

由于这似乎不是为我工作 - 或者我至少不能确定它是。我无法弄清楚如何使宏TotalView的潜水告诉我有什么[DN(INDX,jndx)实际上是在寻找。

Because this seems to not be working for me - or I am at least unsure it is. I can't figure out how to make totalview dive on a macro to tell me what A[DN(indx,jndx)] is actually looking at.

推荐答案

假设一个典型的分配器,如一个glibc的使用,也有一些意见:

Observations

Assuming a typical allocator, such as the one glibc uses, there are some observations:


  1. 是否存储实际使用,该区域必须连续在虚拟存储器保留

  2. 的最大的自由毗邻地区依赖于现有的内存区域的内存使用情况,以及这些地区的可用性 的malloc

  3. 的映射的做法取决于体系结构和操作系统。此外底层系统调用来获得内存区域由这些做法(如的malloc 通过调用到的 MMAP 收购页)。

  1. Whether or not the memory is actually used, the region must be reserved contiguously in virtual memory.
  2. The largest free contiguous regions depends on the memory usage of existing memory regions, and the availability of those regions to malloc.
  3. The mapping practices depend on the architecture and OS. Furthermore underlying system calls to obtain memory regions are affected by these practices (such as malloc calling through to mmap to acquire pages).

下面是一个<一个href=\"http://$c$c.google.com/p/anacrolix/source/browse/public/stackoverflow/largest_malloc_size.c\">simple计划分配可能的最大块(编译 GCC largest_malloc_size.c -Wall -O2

Experiment

Here's a simple program to allocate the largest possible block (compile with gcc largest_malloc_size.c -Wall -O2:

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

static void *malloc_wrap(size_t size)
{
    void *p = malloc(size);
    if (p) {
        printf("Allocated %zu bytes from %p to %p\n", size, p, p + size);
    }
    else {
        printf("Failed to allocated %zu bytes\n", size);
    }
    return p;
}

int main()
{
    size_t step = 0x1000000;
    size_t size = step;
    size_t best = 0;
    while (step > 0)
    {
        void *p = malloc_wrap(size);
        if (p) {
            free(p);
            best = size;
        }
        else {
            step /= 0x10;
        }
        size += step;
    }
    void *p = malloc_wrap(best);
    if (p) {
        pause();
        return 0;
    }
    else {
        return 1;
    }
}

在我的的Linux士丹利2.6.32-24-仿制PAE#39 Ubuntu上运行上面的程序( ./ a.out的) SMP周三07月28日7点39分26秒UTC 2010 i686的GNU / Linux的机器获得这样的结果:

Running the above program (./a.out) on my Linux stanley 2.6.32-24-generic-pae #39-Ubuntu SMP Wed Jul 28 07:39:26 UTC 2010 i686 GNU/Linux machine obtains this result:

<snip>
Allocated 2919235584 bytes from 0x9763008 to 0xb7763008
Allocated 2936012800 bytes from 0x8763008 to 0xb7763008
Failed to allocated 2952790016 bytes
Failed to allocated 2953838592 bytes
Failed to allocated 2953904128 bytes
Failed to allocated 2953908224 bytes
Allocated 2936012800 bytes from 0x85ff008 to 0xb75ff008

这正是2800MiB的分配。观察从 的/ proc / [数字] /地图相关的映射

This is an allocation of exactly 2800MiB. Observing the relevant mapping from /proc/[number]/maps:

<snip>
0804a000-0804b000 rw-p 00001000 08:07 3413394    /home/matt/anacrolix/public/stackoverflow/a.out
085ff000-b7600000 rw-p 00000000 00:00 0          [heap]
b7600000-b7621000 rw-p 00000000 00:00 0 
b7621000-b7700000 ---p 00000000 00:00 0 
b7764000-b7765000 rw-p 00000000 00:00 0 
b7765000-b78b8000 r-xp 00000000 08:08 916041     /lib/tls/i686/cmov/libc-2.11.1.so
<snip>
bfc07000-bfc1c000 rw-p 00000000 00:00 0          [stack]

结论

看起来堆已经在程序数据和code之间的区域被扩大,共享库映射,它舒舒服服地盘踞反对的用户/内核内存空间的边界(显然3G / 1G这个系统上)。

Conclusion

It appears the heap has been expanded in the area between the program data and code, and the shared library mappings, which sit snug against the user/kernel memory space boundary (obviously 3G/1G on this system).

这个结果表明,用malloc最大可分配的空间是大致等于

This result suggests that the maximum allocatable space using malloc is roughly equal to:


  1. 用户空间区域(3GB中的例子)

  2. 少的偏移量堆的开始(程序code和数据)

  3. 少主线程堆栈保留的空间

  4. 少由所有共享库映射
  5. 占用的空间
  6. 最后,可以通过在区域内提供给堆的底层系统调用中发现的最大的连续区域(其可通过其他映射片段化)

对于glibc的和Linux的实现,下面的手册片段是极大的兴趣:

Notes

With respect to glibc and Linux implementations, the following manual snippets are of great interest:

的malloc

   Normally, malloc() allocates memory from the heap, and adjusts the size
   of the heap as required, using sbrk(2).  When allocating blocks of mem‐
   ory larger than MMAP_THRESHOLD bytes, the glibc malloc() implementation
   allocates the memory as a  private  anonymous  mapping  using  mmap(2).
   MMAP_THRESHOLD  is  128  kB  by  default,  but is adjustable using mal‐
   lopt(3).

MMAP

   MAP_ANONYMOUS
          The mapping is not backed by any file; its contents are initial‐
          ized to zero.

后记

此测试是在一个86内核完成。我期望类似的结果从x86_64的内核,尽管返回大得多的内存区域。其它操作系统可能会在他们的映射位置不同而不同,大型的malloc的处理 S,那么结果可能是相当相当大的不同。

Afterword

This test was done on a x86 kernel. I'd expect similar results from a x86_64 kernel, albeit with vastly larger memory regions returned. Other operating systems may vary in their placement of mappings, and the handling of large mallocs, so results could be quite considerably different.

这篇关于能有多大一个malloc在C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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