Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heap / Stack and multiple processes

Say I have two process p1,p2 runnning as a part of my application.

Say p1 is running initially executing function f1() and then f1() calls f2().With the invocation of f2() process p2 starts excuting

What I want to confirm is it that :- 1)Do we have seperate stack for different process?

2)Do we have seperate heap for different process? or do different process share same heap?

3)As we know that for a 32 bit OS do for every process the size of virtual memory is 4GB .So is it that for every process which has 4GB as virtual memory this 4GB is partitioned into heap,stack,text,data

Thanks.

like image 351
Kaizen Avatar asked Dec 23 '09 10:12

Kaizen


People also ask

Can two processes share heap?

Every process will get separate heap, stack independent of other process.

Does each process have its own stack and heap?

Each process has separate heap, data, stack, and grant regions. Processes are isolated from access the grant region in order to protect kernel state.

Is heap allocated per process?

Heap — This segment contains all memory dynamically allocated by a process.

What is heap and stack in a process?

Heap memory is used by all the parts of the application whereas stack memory is used only by one thread of execution. Whenever an object is created, it's always stored in the Heap space and stack memory contains the reference to it.


3 Answers

1) Yes, each process gets its own stack.

2) Yes, each process gets its own heap.

3) I don't think you get the whole 4GB. Some of it is reserved for kernel stuff.

like image 129
rui Avatar answered Sep 30 '22 09:09

rui


  • The virtual memory for a process will be different from other process.
  • Every process will get 4GB of virtual address space ( in 32 bit windows machine) and out of which you can use 2GB of user space ( remaining is for kernel). For stack, heap, static data storage and even loading the DLLs. (This is 3GB if you use large address space)
  • Every process will get separate heap, stack independent of other process.
like image 41
aJ. Avatar answered Sep 30 '22 10:09

aJ.


There are other limitations to consider in Java too, such as only being able to address arrays using Integer.MAX_VALUE at most. This limits you to about 2GB in a lot of areas relating to memory.

like image 20
bobjandal Avatar answered Sep 30 '22 11:09

bobjandal