在UNIX上分配给C ++程序的初始堆大小 [英] Initial Heap size allocated to a C++ program on UNIX

查看:190
本文介绍了在UNIX上分配给C ++程序的初始堆大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常对于在基于UNIX的操作系统上运行的C ++程序分配的初始堆大小是多少?

What is the initial heap size alloted typically to a C++ program running on UNIX based OS ?

它是如何由g ++编译器决定的,如果它有一个角色扮演在这方面?

How is it decided by the g++ compiler if at all it has a role to play in this regard ?

推荐答案

对于C ++,无论什么平台,堆几乎总是动态地扩展, 。在一些嵌入式平台或一些非常老的平台上,这可能不是真的,但是你可能有一个很好的想法,因为环境的性质,你有多少堆。

For C++, no matter what the platform, the heap is almost always extended dynamically by asking the OS for more memory as needed. On some embedded platforms, or some very old platforms this may not be true, but then you probably have a really good idea how much heap you have because of the nature of the environment.

在Unix平台上,这是双重的真实。即使大多数Unix嵌入式平台也是这样工作的。

On Unix platforms this is doubly true. Even most Unix embedded platforms work this way.

在这样的平台上,库通常没有任何内部限制,而是依赖于操作系统告诉它它不能有任何更多的内存。

On platforms that work like this, the library usually doesn't have any kind of internal limit, but instead relies on the OS to tell it that it can't have any more memory. This may happen well after you have actually asked for more memory than is available though for a variety of reasons.

在大多数Unix系统上,有多少一个进程可以拥有的总内存。可以使用 getrlimit 系统调用查询此限制。相关常数为RLIMIT_AS。此限制管理可分配给进程的内存页的最大数量,并直接限制可用的堆空间量。

On most Unix systems, there is a hard limit on how much total memory a process can have. This limit can be queried with the getrlimit system call. The relevant constant is RLIMIT_AS. This limit governs the maximum number of memory pages that can be assigned to a process and directly limits the amount of heap space available.

不幸的是,该限制不直接说明多堆可以使用。

Unfortunately that limit doesn't directly say how much heap you can use. Memory pages are assigned to a process as a result of mmap calls, to hold the program code itself, and for the process' stack.

另外,这个限制是频繁设置的,这是因为mmap调用的结果是保存程序代码本身,远远超过整个系统可用的总内存,如果您将物理内存和交换空间添加在一起。因此,实际上,在达到此限制之前,您的程序将频繁耗尽内存。

Additionally, this limit is frequently set well in excess of the total memory available to the whole system if you add together physical memory and swap space. So in reality your program will frequently run out of memory before this limit is reached.

最后,某些版本的Unix会重新分配页面。它们允许您分配大量的页面,但只有在写入这些页面时,才会实际找到这些页面的内存。这意味着即使所有内存分配调用成功,您的程序也可能因为内存不足而死亡。这样做的理由是能够分配只能被部分使用的大型数组。

Lastly, some versions of Unix over-assign pages. They allow you to allocate a massive number of pages, but only actually find memory for those pages when you write to them. This means your program can be killed for running out of memory even if all the memory allocation calls succeed. The rationale for this is the ability to allocate huge arrays which will only ever be partially used.

简而言之,没有一个典型的大小,方式来找出真正的大小。

So, in short, there isn't a typical size, and no good way to find out what the size really is.

这篇关于在UNIX上分配给C ++程序的初始堆大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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