13

How much virtual memory can a 32-bit process have on 64-bit Linux (i.e. how much memory can I allocate and use with malloc() before I start getting a NULL pointer)?

I tried it on my 32-bit Linux and reached about 3 GB limit. Will I be able to get more on 64-bit Linux?

unwind
  • 391,730
  • 64
  • 469
  • 606
leonidp
  • 131
  • 1
  • 1
  • 3

3 Answers3

26

In the standard 32-bit x86 smp kernel, each process can use 3GB of the 4GB address space and 1GB is used by the kernel (shared in the address space of each process).

With the 4G/4G split "hugemem" 32-bit x86 kernel, each process can use (almost) the entire 4GB of address space and the kernel has a separate 4GB of address space. This kernel was supported by Red Hat in RHEL 3 and 4, but they dropped it in RHEL 5 because the patch was not accepted into the mainline kernel and most people use 64-bit kernels now anyway.

With the 64-bit x86_64 kernel, a 32-bit process can use the entire 4GB address space, except for a couple pages (8KB) at the end of the 4GB address space which are managed by the kernel. The kernel itself uses a part of the address space that is beyond the 4GB accessible to 32-bit code, so it does not reduce the user address space. A 64-bit process can use much more address space (128TB in RHEL 6).

Note that some of the address space will be used by the program code, libraries, and stack space, so you won't be able to malloc() your entire address space. The size of these things varies by program. Take a look at /proc/<pid>/maps to see how the address space is being used in your process; the amount you can malloc() will be limited by the largest unused address range.

mark4o
  • 58,919
  • 18
  • 87
  • 102
  • So what if we have two or three 32-bit processes in a 64-bit OS/kernel? Let's say a server has 64Gb of memory running 64-bit kernel. Will then all of the 32-bit processes be limited to one chunk of 4Gb memory, or it is a separate 4Gb limit for each process? If the former, then three independent 32-bit processes will be all limited to the same 4Gb memory chunk; if the latter, then each 32-bit process will be limited to 4Gb giving total used memory by 32-bit processes up to 12Gb. Which one is right? – Tagar May 28 '15 at 19:28
  • 2
    With a 64-bit kernel, each 32-bit process can have its own separate 4GB of virtual memory. That doesn't necessarily correspond to the total amount of physical RAM that is needed; some of the virtual memory that is inactive may be paged out to disk. – mark4o May 28 '15 at 19:39
  • Thank you for the follow up. What about shared libs? Will each shared 32-bit lib then be duplicated in each 32-bit procesess'es "own" 4-Gb space? Or it would be one instance of a 32-bit shared lib used by different 32-bit processes each of which has its own 4-Gb space? – Tagar May 28 '15 at 19:46
  • 2
    If the same part of the same shared library (or other file) is mapped read-only into the virtual address space of different processes, they can all share one copy in physical memory, even if the file is mapped at different virtual addresses in each process. The initialized data sections that are written to will have a separate copy for each process. – mark4o May 28 '15 at 19:56
1

A 32-bit process will only be able to access 4GB of virtual memory regardless of the OS. This is due to the process only being able to map 32-bits for memory addresses. If you do the math you'll see that 32-bit addresses can only access a maximum of 4GB evenif your running on a 128-bit os.

ennuikiller
  • 46,381
  • 14
  • 112
  • 137
  • That is OK but does not answer my question. Will I be able to get the maximum 4GB on a 64-bit Linux? – leonidp Feb 22 '11 at 14:49
  • 1
    Note that on linux the kernel keeps about 1GB of memory for itself, so your userspace process will only be able to access 3GB. – Kristof Provost Feb 22 '11 at 14:50
  • 1
    @leonidp It does in fact answer the question. The short version is 'No'. – Kristof Provost Feb 22 '11 at 14:50
  • @Kristof OK so lets see if I understood correctly. On 64-bit Linux I will get the maximum of 4GB but because the kernel keeps 1GB for itself, I actually get only 3GB? So there is no difference for me if it is 32-bit or 64-bit Linux? – leonidp Feb 22 '11 at 14:57
  • 2
    @leonidp: use native 64 bits program to use more than 2GB / 3GBytes of virtual memory for your userspace program. – Yann Droneaud Feb 22 '11 at 15:02
0

On 64-bit linux, the maximum memory space for a single process is 2^48 bytes. (In theory, more is possible, but current chips do not allow the entire virtual address space of 2^64 bytes to be used.)

See Wikipedia for more information.

dogbane
  • 266,786
  • 75
  • 396
  • 414
  • 2
    Yes, I know that. But I have a 32-bit process so it is bounded to 4GB anyway no matter that the OS is 64-bit (is it?). So, will I really get this maximum value on a 64-bit Linux? (now, on a 32-bit Linux I get only 3GB) – leonidp Feb 22 '11 at 14:54
  • 1
    @leonidp yes, you are right. A 32-bit process can't use more than that even on 64-bit linux. You will have to rebuild your application for 64-bit. – dogbane Feb 22 '11 at 14:58