Memory UNIX、Linux和Windows的进程内存限制和地址空间

Memory UNIX、Linux和Windows的进程内存限制和地址空间,memory,memory-management,process,operating-system,Memory,Memory Management,Process,Operating System,UNIX、Linux和windows中单个进程的最大内存量是多少?怎么计算呢?4 GB RAM的用户地址空间和内核地址空间是多少?在Linux系统上,请参阅 (更新) 它说: ulimit内置代码用于设置 shell及其派生的任何进程。如果新的极限值为 省略,将打印资源限制的当前值 ulimit-a使用开关选项打印出所有当前值,其他开关,例如ulimit-n打印出最大打开文件数 不幸的是,“最大内存大小”告诉“无限”,这意味着它不受系统管理员的限制 您可以通过以下方式查看内存大小: cat /p

UNIX、Linux和windows中单个进程的最大内存量是多少?怎么计算呢?4 GB RAM的用户地址空间和内核地址空间是多少?

在Linux系统上,请参阅

(更新)

它说:

ulimit内置代码用于设置 shell及其派生的任何进程。如果新的极限值为 省略,将打印资源限制的当前值

ulimit-a使用开关选项打印出所有当前值,其他开关,例如ulimit-n打印出最大打开文件数

不幸的是,“最大内存大小”告诉“无限”,这意味着它不受系统管理员的限制

您可以通过以下方式查看内存大小:

cat /proc/meminfo
结果是:

MemTotal:        4048744 kB
MemFree:          465504 kB
Buffers:          316192 kB
Cached:          1306740 kB
SwapCached:          508 kB
Active:          1744884 kB
(...)
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>

//Bytes To GigaBytes
static inline unsigned long btogb(unsigned long bytes) {
    return bytes / (1024 * 1024 * 1024);
}

//Bytes To ExaBytes
static inline double btoeb(double bytes) {
    return bytes / (1024.00 * 1024.00 * 1024.00 * 1024.00 * 1024.00 * 1024.00);
}

int main() {

    printf("\n");

    struct rlimit rlim_addr_space;
    rlim_t addr_space;

    /*
    * Here we call to getrlimit(), with RLIMIT_AS (Address Space) and
    * a pointer to our instance of rlimit struct.
    */
    int retval = getrlimit(RLIMIT_AS, &rlim_addr_space);
    // Get limit returns 0 if succeded, let's check that.
    if(!retval) {
        addr_space = rlim_addr_space.rlim_cur;
        fprintf(stdout, "Current address_space: %lu Bytes, or %lu GB, or %f EB\n", addr_space, btogb(addr_space), btoeb((double)addr_space));
    } else {
        fprintf(stderr, "Coundn\'t get address space current limit.");
        return 1;
    }

    return 0;
}
因此,如果ulimit说“无限”,MemFree就是你的了。差不多


不要忘记malloc()(以及调用malloc()的新操作符)是一个STDLIB函数,所以如果您调用malloc(100)10次,请按照链接了解原因。

4 GB RAM有多少用户地址空间和内核地址空间?

进程的地址空间分为两部分

用户空间:在标准32位x86_64体系结构上,最大可寻址内存为
4GB
,其中从
0x00000000
0xBFFFFF
=
(3GB)
的地址用于代码、数据段。当用户进程以用户模式或内核模式执行时,可以对该区域进行寻址

内核空间:类似地,从
0xc000000
0xffffff
=
(1GB)
的地址用于内核的虚拟地址空间,仅当进程在内核模式下执行时才能寻址

x86
上的特定地址空间分割由
页偏移量的值决定。参考Linux
3.11.1v
,页偏移定义如下:

#定义"页面"偏移量"AC(配置"页面"偏移量,UL)

其中定义了默认值
default 0xc000000
,还提供了其他地址拆分选项

同样,对于64位

定义页面偏移量(0xFFFF88000000000,UL)

在64位体系结构上,由于巨大的地址空间,拆分不再有效。根据来源,最新的Linux版本已经给出了上述偏移量作为偏移量


当我看到我的64位x86_64体系结构时,一个32位进程可以拥有整个
4GB
的用户地址空间,内核将保持高于
4GB
的地址范围。有趣的是,在现代64位x86_64 CPU上,并非所有地址线都已启用(或地址总线不够大),无法为我们提供
2^64
=
16 EB
的虚拟地址空间。也许
AMD64
/
x86
体系结构分别启用了
48
/
42
低位,从而导致地址空间的
2^48
=
256TB
/
2^42
=
4TB
。现在,这无疑提高了大量RAM的性能,同时出现了一个问题,即如何在操作系统限制下有效地管理它

在Linux中,有一种方法可以找出地址空间的限制。 使用
rlimit
结构

struct rlimit {
    rlim_t cur;    //current limit
    rlim_t max;    //ceiling for cur.
}
rlim\u t
是一种
无符号长
类型

你可以有这样的东西:

MemTotal:        4048744 kB
MemFree:          465504 kB
Buffers:          316192 kB
Cached:          1306740 kB
SwapCached:          508 kB
Active:          1744884 kB
(...)
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>

//Bytes To GigaBytes
static inline unsigned long btogb(unsigned long bytes) {
    return bytes / (1024 * 1024 * 1024);
}

//Bytes To ExaBytes
static inline double btoeb(double bytes) {
    return bytes / (1024.00 * 1024.00 * 1024.00 * 1024.00 * 1024.00 * 1024.00);
}

int main() {

    printf("\n");

    struct rlimit rlim_addr_space;
    rlim_t addr_space;

    /*
    * Here we call to getrlimit(), with RLIMIT_AS (Address Space) and
    * a pointer to our instance of rlimit struct.
    */
    int retval = getrlimit(RLIMIT_AS, &rlim_addr_space);
    // Get limit returns 0 if succeded, let's check that.
    if(!retval) {
        addr_space = rlim_addr_space.rlim_cur;
        fprintf(stdout, "Current address_space: %lu Bytes, or %lu GB, or %f EB\n", addr_space, btogb(addr_space), btoeb((double)addr_space));
    } else {
        fprintf(stderr, "Coundn\'t get address space current limit.");
        return 1;
    }

    return 0;
}
#包括
#包括
#包括
//字节到千兆字节
静态内联无符号长btogb(无符号长字节){
返回字节/(1024*1024*1024);
}
//字节到EB
静态内联双字节btoeb(双字节){
返回字节/(1024.00*1024.00*1024.00*1024.00*1024.00*1024.00*1024.00);
}
int main(){
printf(“\n”);
结构rlimit rlim_addr_空间;
rlim_t addr_空间;
/*
*这里我们调用getrlimit(),其中RLIMIT_作为(地址空间)和
*指向我们的rlimit结构实例的指针。
*/
int retval=getrlimit(RLIMIT_AS和rlim_addr_空间);
//Get limit返回0如果成功,让我们检查一下。
如果(!retval){
addr_space=rlim_addr_space.rlim_cur;
fprintf(标准输出,“当前地址空间:%lu字节,或%lu GB,或%f EB\n”,地址空间,btogb(地址空间),btoeb((双)地址空间));
}否则{
fprintf(stderr,“无法获取地址空间电流限制”);
返回1;
}
返回0;
}
我在我的电脑上运行了这个。。。PRRRRRRRRRRRTSK

输出:
当前地址空间:18446744073709551615字节,或17179869183 GB,或16.000000EB

我的Linux x86_64上有16 EB的最大可用地址空间

这是 它还列出了可以传递给
getrlimit()
的其他常量,并介绍了
getrlimit()
s姐妹
setrlimit()
。当
rlimit
max
成员变得非常重要时,你应该始终检查你没有超过这个值,这样内核就不会打你的脸,喝你的咖啡,偷你的文件

警察局长:请原谅我抱歉的打鼓