3

Sorry new to linux/ubuntu. I have read through a bunch of htop posts here, but I just wanted to clarify what the values mean. I run a 32-bit program, and my understanding is that roughly 4.2 is the max amount of memory this application can use. If I go over that, I will get a calloc error.

When looking at htop, am I looking at the virt or the res amount to see when it's getting close to that 4gb limit?

htop image

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
Jeff
  • 131

1 Answers1

2

As stated in the htop man page:

M_RESIDENT (RES)

The resident set size (text + data + stack) of the process (i.e. the size of the process's used physical memory).

M_SIZE (VIRT)

The size of the virtual memory of the process.

However, RES just shows that - the physical memory, it doesn't mean in any way that the 32-bit binary process will be limited to 4 GB in total, and in fact if you have Physical Address Extension enabled the process could use more than that. Physically, yes, 232 addresses means 4GB of physical RAM. As per Gilles's answer (with my emphasis):

A 32-bit Linux kernel can only execute 32-bit processes. Depending on the kernel compilation options, each process can only allocate 1GB, 2GB or 3GB of memory (the rest is reserved for the kernel when it's processing system calls). This is an amount of virtual memory, unrelated to any breakdown between RAM, swap, and mmapped files.

Also according to an answer by Ramesh on Unix & Linux site, a process can have large quantities of memory allocated:

The most that the process can address is 4GB. You are potentially confusing memory with address space. A process can have more memory than address space. That is perfectly legal and quite common in video processing and other memory intensive applications. A process can be allocated dozens of GB of memory and swap it into and out of the address space at will. Only 2 GB can go into the user address space at a time.

If you have a four-car garage at your house, you can still own fifty cars. You just can't keep them all in your garage. You have to have auxiliary storage somewhere else to store at least 46 of them; which cars you keep in your garage and which ones you keep in the parking lot down the street is up to you.

Corroborated with Breakthrough's answer:

In terms of an address being "valid", each process has it's own unique address space (thus implementing a virtual memory scheme), so any address is technically valid. Remember, a process can allocate more memory than is physically available.

...

A virtual address for a given process is mapped to some physical storage hardware (RAM, disk, etc...), but the mapping is done at run-time by the operating system & MMU.

Of course, address space being 4GB implies pointers still being 32-bit.


Additionally Linux kernel has Out Of Memory Killer/Manager, which will clean up and free up memory. In other words, don't stress out about the 4GB limit, if you're worried about the process memory overall.

If by contrast we're talking about allocating 4 GB in your code, then according to a related post malloc() syscall will be in fact limited to 4 GB of allocations. But you can do more than one malloc() requesting more than 4GB in total (though I'm not entirely sure how that works out with the physical limit).

Other interesting readings:

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • Thanks, the 4GB is definitely a real thing for me, and we had a calloc error last night. Maybe it was due to something else. Will look into it more. Thanks – Jeff May 12 '19 at 21:17
  • https://en.wikipedia.org/wiki/Physical_Address_Extension#Design suggests that your statement is wrong and the 4GB limit still applies to each individual process on PAE systems. The OS can just address more physical memory in total, but software still only has 32 bit wide address pointers, resulting in 2^32 = 4GB maximum memory usage per process. – Byte Commander May 12 '19 at 22:53
  • @ByteCommander that's what I thought it was, so if RES is under 4GB, then I am good, if it goes over, I'm done? – Jeff May 12 '19 at 23:15
  • Not sure about that, but I don't think either value can go over 4GB. – Byte Commander May 12 '19 at 23:17
  • @ByteCommander see that's the biggest question I have because I'm sitting around 3gb VIRT right now but i did get to 4gb and it crashed. Working on porting to 64bit now but until then. – Jeff May 12 '19 at 23:34
  • 1
    @ByteCommander I've addressed that. Physically, yes - we're not going to get above 4 GB physical memory. Software-wise, it's quite possible to request/allocate more than that. Links provided as well. – Sergiy Kolodyazhnyy May 13 '19 at 00:03
  • @Jeff OK, let's back up a little bit and maybe clarify what you're asking in this question ( and maybe edit that into the question itself). Can application use more than 4GB memory ? Yes. Can more than 4 GB of RAM be used if you only have 4 GB RAM installed ? No, physically not possible and some will be reserved by kernel. Can a process use more memory in total if you have only 4GB of RAM ? Yes, with PAE. What causes the crash ? Lots of possible options, it's not limited to just RES memory growing large, although it's likely. – Sergiy Kolodyazhnyy May 13 '19 at 00:21
  • Also this https://stackoverflow.com/a/1440020 and https://stackoverflow.com/a/2798357 – Sergiy Kolodyazhnyy May 13 '19 at 00:27
  • @SergiyKolodyazhnyyI have 32GB of ram on this server. I'm running a chroot 32 bit kernel so I can run a 32 bit app. It's currently sitting at 3292M Virt and 2049M Res. 6.4% of memory. – Jeff May 13 '19 at 00:37