user2815465
user2815465

Reputation: 91

Process memory limit and address space for UNIX and Linux and Windows

what is the maximum amount of memory for a single process in UNIX and Linux and windows? how to calculate that? How much user address space and kernel address space for 4 GB of RAM?

Upvotes: 4

Views: 19605

Answers (3)

Jenny T-Type
Jenny T-Type

Reputation: 199

In Linux there's a way to find out the limit of address space you can have. Using the rlimit structure.

struct rlimit {
    rlim_t cur;    //current limit
    rlim_t max;    //ceiling for cur.
}

rlim_t is a unsigned long type.

and you can have something like:

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

I ran this on my computer and... prrrrrrrrrrrrrrrrr tsk!

Output: Current address_space: 18446744073709551615 Bytes, or 17179869183 GB, or 16.000000 EB

I have 16 ExaBytes of max address space available on my Linux x86_64.

Here's getrlimit()'s definition it also lists the other constants you can pass to getrlimits() and introduces getrlimit()s sister setrlimit(). There is when the max member of rlimit becomes really important, you should always check you don't exceed this value so the kernel don't punch your face, drink your coffee and steal your papers.

PD: please excuse my sorry excuse of a drum roll ^_^

Upvotes: 2

Sunil Bojanapally
Sunil Bojanapally

Reputation: 12688

How much user address space and kernel address space for 4 GB of RAM?

The address space of a process is divided into two parts,

User space: On standard 32 bit x86_64 architecture,the maximum addressable memory is 4GB, out of which addresses from 0x00000000 to 0xbfffffff = (3GB) meant for code, data segments. This region can be addressed when user process executing either in user or kernel mode.

Kernel space: Similarly, addresses from 0xc0000000 to 0xffffffff = (1GB) are meant for virtual address space of the kernel and can only addressed when the process executes in kernel mode.

This particular address space split on x86 is determined by the value of PAGE_OFFSET. Referring to Linux 3.11.1v page_32_types.h and page_64_types.h, page offset is defined as below,

#define __PAGE_OFFSET _AC(CONFIG_PAGE_OFFSET, UL)

Where Kconfig defines a default value of default 0xC0000000 also with other address split options available.

Similarly for 64 bit,

#define __PAGE_OFFSET _AC(0xffff880000000000, UL).

On 64 bit architecture 3G/1G split doesn't hold anymore due to huge address space. As per the source latest Linux version has given above offset as offset.

When I see my 64 bit x86_64 architecture, a 32 bit process can have entire 4GB of user address space and kernel will hold address range above 4GB. Interestingly on modern 64 bit x86_64 CPU's not all address lines are enabled(or the address bus is not large enough) to provide us 2^64 = 16 exabytes of virtual address space. Perhaps AMD64/x86 architectures has 48/42 lower bits enabled respectively resulting to 2^48 = 256TB / 2^42= 4TB of address space. Now this definitely improves performance with large amount of RAM, at the same time question arises how it is efficiently managed with the OS limitations.

Upvotes: 8

ern0
ern0

Reputation: 3172

On Linux systems, see man ulimit

(UPDATED)

It says:

The ulimit builtin is used to set the resource usage limits of the shell and any processes spawned by it. If a new limit value is omitted, the current value of the limit of the resource is printed.

ulimit -a prints out all current values with switch options, other switches, e.g. ulimit -n prints out no. of max. open files.

Unfortunatelly, "max memory size" tells "unlimited", which means that it is not limited by system administrator.

You can view the memory size by

cat /proc/meminfo

Which results something like:

MemTotal:        4048744 kB
MemFree:          465504 kB
Buffers:          316192 kB
Cached:          1306740 kB
SwapCached:          508 kB
Active:          1744884 kB
(...)

So, if ulimit says "unlimited", the MemFree is all yours. Almost.

Don't forget that malloc() (and new operator, which calls malloc()) is a STDLIB function, so if you call malloc(100) 10 times, there will be lot of "slack", follow link to learn why.

Upvotes: 1

Related Questions