C 中的 malloc 可以有多大?

发布于 2024-09-13 22:45:50 字数 262 浏览 9 评论 0原文

我在 C 中有一个 malloc,它是 26901^2*sizeof(double)

这让我思考这里的最大值是多少?

另外,定义宏来访问这个二维数组时会遇到任何问题吗?

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

因为这似乎对我不起作用——或者至少我不确定它是否有效。我不知道如何在宏上进行 TotalView 潜水来告诉我 A[DN(indx,jndx)] 实际在看什么。

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)

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

破晓 2024-09-20 22:45:50

观察

假设一个典型的分配器,例如 glibc 使用的分配器,有一些观察:

  1. 无论内存是否实际使用,该区域都必须在虚拟内存中连续保留。
  2. 最大的空闲连续区域取决于现有内存区域的内存使用情况,以及这些区域对 < 的可用性代码>malloc
  3. 映射实践取决于体系结构和操作系统。此外,获取内存区域的底层系统调用也受到这些做法的影响(例如 malloc 调用 mmap 获取页面)。

实验

这是一个简单程序,用于分配最大的可能的块(使用gcclargest_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 stanley 2.6.32-上运行上述程序(./a.out) 24-generic-pae #39-Ubuntu SMP Wed Jul 28 07:39:26 UTC 2010 i686 GNU/Linux 机器获得此结果:

<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/[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]

结论

看来堆已经被在程序数据和代码之间的区域以及共享库映射之间进行了扩展,这些映射紧贴用户/内核内存空间边界(在此系统上显然为 3G/1G)

此结果表明使用 malloc 的最大可分配空间大致等于:

  1. 用户空间区域(示例中为 3GB)
  2. 减去到堆开头的偏移量(程序代码和数据)
  3. 减去为主线程堆栈保留的空间
  4. 减去共享库中所有映射所占用的空间
  5. 最后,底层系统调用在堆可用区域内可以找到的最大连续区域(可能是被其他映射分割)

注释

关于 glibc 和 Linux 实现,以下手册片段非常有趣:

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.

后记

该测试是在 x86 内核上完成的。我预计 x86_64 内核会得到类似的结果,尽管返回的内存区域要大得多。其他操作系统在映射的位置以及大型 malloc 的处理方面可能会有所不同,因此结果可能会有很大不同。

Observations

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

  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).

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;
    }
}

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

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]

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).

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

  1. The user space region (3GB in the example)
  2. Less the offset to the start of the heap (program code and data)
  3. Less the space reserved for the main thread stack
  4. Less the space occupied by all the mapped in shared libraries
  5. Finally, the largest contiguous region that can be found by the underlying system call within the region available to the heap (which may be fragmented by other mappings)

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.

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.

神经暖 2024-09-20 22:45:50

这取决于您的 malloc 实现!

根据维基百科,“自 v2.3 版本以来,GNU C 库 (glibc) 使用修改后的 ptmalloc2,它本身基于 dlmalloc v2.7.0。” dlmalloc 指的是 Doug Lea 的 malloc 实现。在此实现中需要注意的重要一点是,大型 malloc 是通过操作系统的内存映射文件功能完成的,因此这些块实际上可以非常大,而不会出现查找连续块的许多问题。

That depends on your malloc implementation!

According to Wikipedia, "Since the v2.3 release, the GNU C library (glibc) uses a modified ptmalloc2, which itself is based on dlmalloc v2.7.0." dlmalloc refers to Doug Lea's malloc implementation. The important thing to note in this implementation is that large mallocs are accomplished through the operating system's memory mapped file functionality, so these blocks can be quite large indeed without many problems of finding a contiguous block.

时光倒影 2024-09-20 22:45:50

malloc 问题已得到解答(取决于操作系统,您没有指定),因此关于定义:

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

不太安全,因为有人可能会执行 DN(a+b,c) ,它会扩展为

((int)a+b * ny + (int)c)

这可能不是你想要的。因此,请在其中添加很多括号:

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

要查看 DN(indx,jndx) 指向什么,只需 printf("%d\n",DN(indx,jndx));< /代码>

The malloc question is answered (depends on OS, which you don't specify), so about that define:

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

is not quite safe, for someone might do DN(a+b,c) which expands to

((int)a+b * ny + (int)c)

which is probably not what you wanted. So put a lot of parentheses in there:

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

to see what DN(indx,jndx) points to, just printf("%d\n",DN(indx,jndx));

欲拥i 2024-09-20 22:45:50

malloc 调用中的大小参数的类型为 size_t,该类型因实现而异。请参阅此问题了解更多的。

The size parameter in a call to malloc is of type size_t, which varies by implementation. See this question for more.

德意的啸 2024-09-20 22:45:50

这让我思考这里的最大值是多少?

26'901^2 = 723'663'801。如果你的 double 是 8 个字节,那么它就小于 8GB。我认为分配这么多内存完全没有问题,而且我的应用程序通常会分配更多内存(在 64 位系统上)。 (我见过的最大内存消耗是 420GB(在具有 640GB RAM 的 Solaris 10 numa 系统上),最大连续块约为 24GB。)

最大值很难识别,因为它依赖于平台:与 32 位系统类似,它取决于用户-空间/内核空间分割。就目前情况而言,我认为首先会达到实际物理 RAM 的限制 - 在达到 libc 可以分配的限制之前。 (内核并不关心,它只是经常扩展虚拟内存,甚至不考虑是否有足够的 RAM 来固定它。)

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

26'901^2 = 723'663'801. If your double is 8 bytes, then it is less than 8GB. I see totally no problem allocating that much of memory and my apps routinely allocate (on 64 bit systems) much more. (Biggest memory consumption I have ever seen was 420GB (on Solaris 10 numa system with 640GB RAM) with largest continuous block of ~24GB.)

Largest value is hard to identify since it is platform dependent: similar to the 32bit systems it depends on user-space / kernel-space split. As things stand at the moment, I think one would first come to the limit of the actual physical RAM - before reaching the limit of what libc can allocate. (And kernel doesn't care, it just expands virtual memory often without even considering whether there is sufficient RAM to pin it to.)

橘亓 2024-09-20 22:45:50

您可以要求 malloc() 获取的最大内存块是最大的 size_t 值 - 这是来自 SIZE_MAX代码>。您可以成功请求的最大数量显然取决于操作系统和单台计算机的配置。

你的宏不安全。它使用 int 变量执行索引计算,该变量的范围只需达到 32767。任何高于此值的值都可能导致有符号溢出,从而导致未定义的行为。您可能最好以 size_t 的形式进行计算,因为该类型必须能够保存任何有效的数组索引:(

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

尽管请注意,如果您为 i 提供负值或j,你会得到一个远远超出范围的索引)。

The largest memory block you can ask malloc() for is the largest size_t value - this is SIZE_MAX from <limits.h>. The largest amount you can sucessfully request is obviously dependent on the operating system and the configuration of the individual machine.

Your macro is not safe. It performs the index calculation with an int variable, which is only required to have a range up to 32767. Any value higher than this can cause signed overflow, which results in undefined behaviour. You are probably best off doing the calculation as a size_t, since that type must be able to hold any valid array index:

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

(Although note that if you supply negative values for i or j, you'll get an index far out of bounds).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文