Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

memory-management - 什么是堆栈和堆?(What and where are the stack and heap?)

Programming language books explain that value types are created on the stack , and reference types are created on the heap , without explaining what these two things are.

(编程语言书籍解释了值类型是在堆栈上创建的,而引用类型是在堆上创建的,而没有说明这两个是什么。)

I haven't read a clear explanation of this.

(我还没有阅读清楚的解释。)

I understand what a stack is.

(我了解堆栈是什么。)

But,

(但,)

  • Where and what are they (physically in a real computer's memory)?

    (它们在哪里和在哪里(物理上在真实计算机的内存中)?)

  • To what extent are they controlled by the OS or language run-time?

    (它们在多大程度上受操作系统或语言运行时的控制?)

  • What is their scope?

    (他们的范围是什么?)

  • What determines the size of each of them?

    (什么决定了它们的大小?)

  • What makes one faster?

    (是什么使速度更快?)

  ask by mattshane translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The stack is the memory set aside as scratch space for a thread of execution.

(堆栈是为执行线程预留的暂存空间。)

When a function is called, a block is reserved on the top of the stack for local variables and some bookkeeping data.

(调用函数时,将在堆栈顶部保留一个块,用于存放局部变量和一些簿记数据。)

When that function returns, the block becomes unused and can be used the next time a function is called.

(当该函数返回时,该块将变为未使用状态,并且可以在下次调用该函数时使用。)

The stack is always reserved in a LIFO (last in first out) order;

(堆栈始终按LIFO(后进先出)顺序保留;)

the most recently reserved block is always the next block to be freed.

(最近保留的块始终是下一个要释放的块。)

This makes it really simple to keep track of the stack;

(这使得跟踪堆栈非常简单。)

freeing a block from the stack is 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 are allocated or free at any given time;

(这使得跟踪在任何给定时间分配或释放堆的哪些部分变得更加复杂。)

there are many custom heap allocators available to tune heap performance for different usage patterns.

(有许多自定义堆分配器可用于调整不同使用模式的堆性能。)

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

(每个线程都有一个堆栈,而应用程序通常只有一个堆(尽管对于不同类型的分配有多个堆并不少见)。)

To answer your questions directly:

(要直接回答您的问题:)

To what extent are 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.

(通常,语言运行库会调用OS来为应用程序分配堆。)

What is their scope?

(他们的范围是什么?)

The stack is attached to a thread, so when the thread exits the stack is reclaimed.

(堆栈连接到线程,因此当线程退出时,堆栈将被回收。)

The heap is typically allocated at application startup by the runtime, and is reclaimed when the application (technically process) exits.

(通常,堆是在运行时在应用程序启动时分配的,并在应用程序(技术上已退出)退出时被回收。)

What determines the size of each of them?

(什么决定了它们的大小?)

The size of the stack is set when a thread is created.

(创建线程时设置堆栈的大小。)

The size of the heap is set on application startup, but can grow as space is needed (the allocator requests more memory from the operating system).

(堆的大小是在应用程序启动时设置的,但是可以随需要的空间而增长(分配器从操作系统请求更多的内存)。)

What makes one faster?

(是什么使速度更快?)

The stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it (a pointer/integer is simply incremented or decremented), while the heap has much more complex bookkeeping involved in an allocation or deallocation.

(堆栈速度更快,因为访问模式使分配和释放内存变得很简单(指针/整数只是增加或减少),而堆的分配或释放则要复杂得多。)

Also, each byte in the stack tends to be reused very frequently which means it tends to be mapped to the processor's cache, making it very fast.

(同样,堆栈中的每个字节都倾向于被非常频繁地重用,这意味着它倾向于被映射到处理器的高速缓存中,从而使其非常快。)

Another performance hit for the heap is that the heap, being mostly a global resource, typically has to be multi-threading safe, ie each allocation and deallocation needs to be - typically - synchronized with "all" other heap accesses in the program.

(堆的另一个性能损失是,堆(通常是全局资源)通常必须是多线程安全的,即,每个分配和释放都必须(通常)与程序中的“所有”其他堆访问同步。)

A clear demonstration:

(清晰的演示:)
Image source: vikashazrati.wordpress.com

(图片来源: vikashazrati.wordpress.com)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...