What is the size of a process/thread in Linux? When a process/thread is created, along with task_struct and other data structure inside it, is there anything else?
Is the stack of a process/thread allocated upon process/thread initialization (fixed size)? Or is it allocated when necessary (like virtual memory)?
How can I know what size a standard process/thread when it is created in memory?
When a large block of memory (> pagesize = 4096 bytes) is first allocated on Linux it uses special “null” memory pages in the pagetable that aren’t backed by anything, so when a thread is started it will allocate ~1 MB of these zero pages for a thread stack. As the stack grows the pages are then converted into real memory backed pages. Because of this “null” page backing it is generally okay to have liberally large stacks.
Threads and processes are both created with the same underlying syscall called clone(2). It has lots of options and does lots of stuff. see
man clonefor a detailed explanation.http://linux.die.net/man/2/clone
Large blocks of memory are allocated with an anonymous mmap(2) call.
You may also be interested in doing a web search for “linux overcommit bit”
(If you want to refine your question, I can be more specific.)