Q: 스택의 사이즈는 왜이렇게 작나요?
https://stackoverflow.com/questions/10482974/why-is-stack-memory-size-so-limited
Q: When you allocate memory on the heap, the only limit is free RAM (or virtual memory).
힙에 메모리를 할당할 때는 램이나 가상메모리의 크기 내에서는 자유롭게 사이즈를 줄 수 있죠.
It makes Gb of memory.
그래서 보통 기가바이트 단위도 쓸 수가 있어요.
So why is stack size so limited (around 1 Mb) ?
그런데 스택은 왜이렇게 작은거죠? 보통 1메가정도밖에 안되더라고요.
What technical reason prevents you to create really big objects on the stack ?
기술적인 이유 때문에 스택에 큰 객체를 두지 못하는건가요?
...
A: My intuition is the following.
내 생각엔 이래요.
The stack is not as easy to manage as the heap.
스택은 힙처럼 관리하기가 쉽지 않아요.
The stack need to be stored in continuous memory locations.
스택은 연속적인 메모리 위치에 저장될 필요가 있거든요.
This means that you cannot randomly allocate the stack as needed, but you need to at least reserve virtual addresses for that purpose.
스택에는 필요에 따라 랜덤으로 할당을 할수 없단거죠.
하지만 최소한 가상 주소는 예약을 해놔야 합니다.
The larger the size of the reserved virtual address space, the fewer threads you can create.
예약된 가상 주소 공간이 커질수록 만들수 있는 스레드 수는 적어져요.
For example, a 32-bit application generally has a virtual address space of 2GB.
예를 들어 32비트 앱은 보통 2기가의 가상주소 공간을 가집니다.
This means that if the stack size is 2MB (as default in pthreads), then you can create a maximum of 1024 threads.
그러니까 스택의 크기가 2메가면 최대 1024개의 스레드를 생성할 수가 있는거죠.
This can be small for applications such as web servers.
웹서버 같은 앱들은 이게 적어도 되는데요.
Increasing the stack size to, say, 100MB (i.e., you reserve 100MB, but do not necessarily allocated 100MB to the stack immediately), would limit the number of threads to about 20, which can be limiting even for simple GUI applications.
스택의 크기를 100으로 늘리면(100메가를 예약해도 바로 100메가를 할당하진 않음), 스레드 수를 20개 정도로 제한할 수 있죠. 이건 간단한 gui 앱에서도 제한이 될수 있어요.
A interesting question is, why do we still have this limit on 64-bit platforms.
흥미로운 문제는, 64비트 플랫폼에서도 여전히 이 제한이 존재한다는 거에요.
I do not know the answer, but I assume that people are already used to some "stack best practices":
이 문제에 대해서는 답을 드리진 못하겠지만, 아마 사람들이 스택의 그 "무난한 관행"에 익숙해졌기 때문인것 같네요.
be careful to allocate huge objects on the heap and, if needed, manually increase the stack size.
큰 객체는 힙에 놓고 필요할 때만 스택의 크기를 늘리는게 좋다는 겁니다.
Therefore, nobody found it useful to add "huge" stack support on 64-bit platforms.
그래서 64비트 플랫폼에서 어떤 환경도 기본으로 "큰" 스택을 지원하지 않는거에요.