JVM Learning, heap and stack--what and where is the stack and heap?

Source: Internet
Author: User

The stack is the memory set aside as scratch space for a thread of execution. When a function was called, a block is reserved on the top of the stack for local variables and some bookkeeping data. When this function returns, the block becomes unused and can be used the next time a function is called. The stack is all reserved in a LIFO (last on first out) order; The most recently reserved block was always the next block to be freed. This makes it really-keep track of the stack; Freeing a block from the stack was nothing more than adjusting one pointer.

The heap is memory set aside for dynamic allocation. Unlike the stack, there ' s no enforced pattern to the allocation and deallocation of blocks from the heap; You can allocate a block at any time and free it at any time. This makes it much more complex to keep track of which parts of the heap is allocated or free at any given time; There is many custom heap allocators available to tune heap performance for different usage patterns.

Each thread gets a stack and while there's typically only one heap for the application (although it isn ' t uncommon to has MU Ltiple heaps for different types of allocation).

Understanding: Stack space is allocated as a thread. When a function is called, the top of the stack allocates a block of memory for the variable and data. When the function returns, the block of memory is freed and will be used by the function that is called next. Stacks are usually last-in-first-out, so the tracking stack is simple, freeing up the memory in the stack to simply move the next pointer. The heap is used to dynamically allocate memory. Without the stack, there is no fixed way of allocating memory in the heap, and you can allocate and free a piece of memory at any time (you do not have to keep the order of LIFO).

To answer your questions directly:

To what extent is they controlled by the OS or language runtime?

The OS allocates the stack for each system-level thread when the thread is created. Typically the OS is called by the language runtime to allocate the heap for the application.

What is their scope?

The stack is attached to a thread, and so at the thread exits the stack is reclaimed. The heap is typically allocated at application startup by the runtime, and was reclaimed when the application (technically Process) exits.

What is determines the size of each of the them?

The size of the stack is set when a thread is created. The size of the heap is set in application startup, but can grow as space are needed (the allocator requests more memory fr OM the operating system).

Understanding: The size of the stack is determined and cannot be changed when the thread is created. The heap size is determined when the program is created, but it can be expanded as the program needs to run.

What makes one faster?

The stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it (a Pointer/integ Er is simply incremented or decremented) with the while the heap have much more complex bookkeeping involved in an allocation or FR Ee. Also, each of the byte in the stack tends to being reused very frequently which means it tends to being mapped to the processor ' s cache , making it very fast. Another performance hits for the heap are that the heap, being mostly a global resource, typically have to be multi-threading Safe, i.e. allocation and deallocation needs to be-typically-synchronized with ' all ' other heap accesses in the Program.

A Clear Demonstration:


Stack:

    • Stored in computer RAM just like the heap.
    • Variables created on the stack would go out of scope and automatically deallocate.
    • Much faster to allocate in comparison to variables on the heap.
    • Implemented with a actual stack data structure.
    • Stores local data, return addresses, used for parameter passing
    • Can has a stack overflow when too much of the stack is used. (mostly from infinite (or too much) recursion, very large allocations)
    • Data created on the stack can used without pointers.
    • Would use the stack if you know exactly how much data are need to allocate before compile time and it's not too big.
    • Usually have a maximum size already determined when your program starts

Heap:

    • Stored in computer RAM just like the stack.
    • Variables on the heap must is destroyed manually and never fall out of scope. The data is freed with delete, delete[] or free
    • Slower to allocate in comparison to variables on the stack.
    • Used on demand to allocate a block of data for use by the program.
    • Can has fragmentation when there is a lot of allocations and deallocations
    • In C + + data created on the heap would be pointed to by pointers and allocated with new or malloc
    • Can has allocation failures if too big of a buffer is requested to be allocated.
    • You would use the heap if you don ' t know exactly how much data you'll need at runtime or if you need to allocate a lot O F data.
    • Responsible for memory leaks

JVM Learning, heap and stack--what and where is the stack and heap?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.