Fundamental of Virtural Machine
I spend some time to read this article fundamental_of_virtual_memory in the day.
Here I records some points that I don’t know before , along with some derived idea base on the article.
- All the variables whose size can be determined presiely at compile time will be allocated to the stack.
- All the variable allocated on the stack can be reclaimed efficiently by simply moving the stack pointer down.
- From 1,2 allocate or reclaimed variables on the stack is very fast.
- However, in some cases, variables must be allocated on the heap instead of stack: 4.1 When the variable type or size cannot be determined at compile time. 4.2 When the method A is called by multiple methods (e.g B and C). In such cases, the variables of method A cannot remain on the stack, because they would be lost once A’s stack frame is popped. Therefore,they must be stored on the heap.
- A process can create multiple threads. Each thread will have it’s own stack, but these threads share the heap.
- Each thread’s stack has size limit, which is defined by the operating system, but can also be adjusted by the virtural machine.
- The default stack size per thread is typically 2MB. we use this to estimate the maximum number of threads a VM can support, max thread count = (available memory / thread memory size). However, it’s not clear whether the actual memory usage is always affected by this allocation, since OS may reserve 2 MB per thread regardless of real consumption.
- In addition to 7, we also need to consider heap usage when estimating the maximum supported thread count. Is there a straighforward way to determin this factor, so that we can better understand the pratical maximum concurrency ?