Where is Heap and Stack on Physical Memory?

I read by some googling about Heap & Stack, but most answer says just its concept description, differences.

I am curious other things.

  • as title says, Where is Heap and Stack on Physical Memory?

  • How is their size? For example, I use 12 giga byte memory at my desktop PC, then how much is Heap? and how much is Stack size?

  • Who made these 2 different type concept?

  • Can I manipulate Heap & Stack's allocation? if they take 50% of memory each, (if Heap take 6 giga byte memory, Stack take 6 giga byte too in my case), can I resize them?


  • as title says, Where is Heap and Stack on Physical Memory?
  • Ever since CPUs have had MMUs to add a layer of indirection between virtual memory and physical memory, heap and stack have been anywhere in physical memory. Ever since modern Operating Systems have implemented ASLR, heap and stack have been anywhere in virtual memory, too.

  • How is their size? For example, I use 12 giga byte memory at my desktop PC, then how much is Heap? and how much is Stack size?
  • Both start small and grow on demand. On Unix, the maximum stack size is set by ulimit -s and the heap size is limited by ulimit -d . You can see what limits are set by default on your Unix OS with ulimit -a .

  • Who made these 2 different type concept?
  • I would bet this goes back to at least the 1960s. Wikipedia has a reference from 1960.

  • Can I manipulate Heap & Stack's allocation? if they take 50% of memory each, (if Heap take 6 giga byte memory, Stack take 6 giga byte too in my case), can I resize them?
  • As already said, they resize themselves, or more accurately, they grow on demand, within limits set by the OS and the user. See the help for ulimit if you are using Unix and bash.


    1. It can be everywhere. Even outside the physical memory, because in terms of application there is no such thing. Everything in user land uses virtual memory, that can be mapped to RAM or swap area on HDD . No certain assumptions here, sorry.

    2. They both grow dynamically, difference lies in speed and size limits:

  • Heap is usually considered slower. It is allocated, depending on application requirements. It is as huge as the amount of RAM or even larger (paging).

  • Stack is much faster, because it "allocated" by simple move of stack pointer. It usually has size limit. For example, in C++, this limit is set at phase of compilation ( ulimit -s on GCC, /STACK:reserve , /STACK:reserve,commit on MSVC).

  • Stack is usually much smaller and can be easily overflowed (that's what we call stack overflow ). For example, in C++, you most likely won't be able to do this:

    int main()
    {
        int large_array[1000000];
        return 0;
    }
    

    Because:

    在这里输入图像描述

    While this is perfectly fine:

    int main()
    {
        int* large_array = new int[1000000]; //allocated from heap
        return 0;
    }
    

    3. Some really smart people.

    4. Read carefully points 1-3 and you will know the answer.

    链接地址: http://www.djcxy.com/p/2342.html

    上一篇: 在堆中堆叠逻辑内存区域?

    下一篇: 物理内存堆和堆栈在哪里?