3

I am confused over what is meant by virtual address space. In a 32 bit machine a process can address 2^32 memory locations. Does that mean the virtual address space of every process is 2^32 (4GB) ?

The following is a snapshot of the virtual address space of a process. Can this grow upto 4GB? Is there any limit on the number of processes in such a system?

enter image description here

Nemo
  • 4,601
  • 11
  • 43
  • 46

4 Answers4

3

Yes, the virtual address space of every process is 4GB on 32-bit systems (232 bytes). In reality, the small amount of virtual memory that is actually used corresponds to locations in the processor cache(s), physical memory, or the disk (or wherever else the computer decides to put stuff).

Theoretically (and this behavior is pretty common among the common operating systems), a process could actually use all its virtual memory if the OS decided to put everything it couldn't fit in physical memory onto the disk, but this would make the program extremely slow because every time it tried to access some memory location that wasn't cached, it would have to go fetch it from the disk.

You asked if the picture you gave could grow up to 4GB. Actually, the picture you gave takes up all 4GB already. It is a way of partitioning a process's 4GB of virtual memory into different sections. Also if you're thinking of the heap and the stack "growing", they don't really grow; they have a set amount of memory allocated for them in that partitioning layout, and they just utilise that memory however they want to (a stack moves a pointer around, a heap maintains a data structure of used and non-used memory, etc).

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • "if the OS decided to put everything it couldn't fit in physical memory onto the disk"... Forgive me, but isn't that exactly what all modern desktop/server OSs do (Windows, OS X, Linux, BSD)? (That is, unless you specifically disable the feature.) – Dietrich Epp Mar 01 '12 at 07:14
  • @DietrichEpp yes, but there is emphasis on _everything_, and I didn't want to say something is for sure when really the computer can do what it wants. – Seth Carnegie Mar 01 '12 at 07:16
  • What else would it do with data that doesn't fit in memory, besides put it on the disk? (Assuming it wasn't already on the disk.) – Dietrich Epp Mar 01 '12 at 07:22
  • @DietrichEpp it could just stop trying and give you a memory allocation error. – Seth Carnegie Mar 01 '12 at 14:55
  • Linux does not do this. If you ask for more memory than you have, it will kill a process to get it for you, but it won't return an error. – Dietrich Epp Mar 01 '12 at 15:11
  • @DietrichEpp ok, but my answer isn't about linux. – Seth Carnegie Mar 01 '12 at 15:19
  • Sorry to get off topic, I was trying to say that the "theoretically" section is actually quite common, except on embedded devices. – Dietrich Epp Mar 01 '12 at 15:47
  • @DietrichEpp Ok, I added a parenthetical saying that it was pretty common. – Seth Carnegie Mar 01 '12 at 15:50
3

Can this grow upto 4GB?

The size of the address space is capped by the number of unique pointer values. For a 32-bit processor, a 32-bit value can represent 2 ^ 32 distinct values. If you allow each such value to address a different byte of memory, you get 2 ^ 32 bytes, which equals four gigabytes.

So, yes, the virtual address space of a process can theoretically grow to 4 GB. However in reality, this may also depend on the system and processor. As can be seen:

This theoretical maximum cannot be achieved on the Pentium class of processors, however. One reason is that the lower bits of the segment value encode information about the type of selector. As a result, of the 65536 possible selector values, only 8191 of them are usable to access user-mode data. This drops you to 32TB.

Note that there are two ways to allocate memory from the system, you can, of course, allocate memory for your process implicitly using C's malloc ( your question is tagged ), but explicitly map file bytes.

Is there any limit on the number of processes in such a system?

a process includes one or more threads that actually execute the code in the process (technically, processes don’t run, threads do) and that are represented with kernel thread objects.

According to some tests carried out here, A 32-bit Windows XP system with 2GB of default address space can create approximately 2025 threads:

Thread Limit of 2025

However a 32-bit test limit running on 64 bit Windows XP with 4GB allocated address space created close to 3204 threads:

32bit test limit on 64 bit XP created 3204 threads

However the exact thread and process limit is extremely variable, it depends on a lot of factors. The way the threads specify their stack size, the way processes specify their minimum working set, the amount of physical memory available and the system commit limit. In any case, you don't usually have to worry about this on modern systems, since if your application really exceeds the thread limit you should rethink your design, as there are almost always alternate ways to accomplish the same goals with a reasonable number.

Community
  • 1
  • 1
ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
  • 32-bit app on Vista 64 - my current record, 4000 threads in one app, (over 5000 in all), no problem, (maximum stack size set to 128K). – Martin James Mar 01 '12 at 12:11
  • Just for fun, I set the stack at 65536 and could create 5000 threads on one app. Trying for 6000 resulted in 'Thread creation error' exception box :(. – Martin James Mar 01 '12 at 12:36
  • Has anyone counting the max threads on a non-Windows NT machine? I can't find anything about this anywhere. – ApprenticeHacker Mar 01 '12 at 13:21
2

Did you read wikipedia's virtual memory, process, address space pages?

What book did you read about advanced unix programming? or on advanced linux programming?

Usually, the address space is the set of segments which are valid (not in blue in your figure).

See also mmap(2) and execve(2) pages.

Try (on a Linux system)

cat /proc/self/maps

and

cat /proc/$$/maps

to understand a bit more.

See also this question and this. Read Operating Systems: Three Easy Pieces

Of course, the kernel is able to set some limits (see also setrlimit(2) syscall). And they are resource constraints (swap space, RAM, ...).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
-1

Answering the neglected part...

There's a limit on how many processes there can be. All per-process data structures that the kernel keeps in its portion of the virtual address space (which is shared, otherwise you wouldn't be able to access the kernel in every process) take some space. So, for example, if there's 1GB available for this data and only a 4KB page is needed per process in the kernel, then you arrive at about 250 thousand of processes max. In practice, this number is usually much smaller because things more are complex and there's physical memory reserved for various things for every process. See, for example, Mark Russinovich's article on process and thread limits in Windows for more details.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • *which is shared, otherwise you wouldn't be able to access the kernel in every process*: I don't understand what you mean __it is shared__. What is shared among whom? – Kindred Dec 10 '18 at 02:49
  • *for example, if __there__'s 1GB available for __this__ data*: Where? Which data? – Kindred Dec 10 '18 at 02:53
  • @ptr_user7813604 kernel's portion of the virtual address space is shared. It's present in each process' virtual address space. This portion is limited in size (to 1 or 2 GB out of 4 GBs). So, for example, if you describe each process state using a 4KB page of memory and you only have 1GB of the kernel's portion of the VAS, 250K processes is max. – Alexey Frunze Dec 15 '18 at 21:57