Heap and stack

Java objects are all on the heap, so it can sometimes be confusing. For example:

Why heap and stack?

What’s the difference between a heap and a stack?

Why is the heap thread shared and the stack not?

Many people who know a little bit about Java or even a little bit about programming know that memory is generally divided into stacks and stacks, stacks are related by the system, and stacks are managed by programmers themselves… Any book on the basics of language will mention these traits, but have you ever wondered why?

Why heap and stack

The distinction between heap and stack is determined by the similarity between the two types of memory required by the program. In C++, there are two ways to create an object, statically and dynamically.

Statically allocated

In general, static allocation is used when initializing a known object size, such as int a[10]; If we can determine that the array is 10, we can use this approach. In this way, the memory required is determined at compile time, so the operating system can pre-determine the specified size of memory for a variable, and it can be automatically freed at the end of the variable’s life cycle.

Dynamic allocation

Int * a =new int[count]; int* a =new int[count]; The variable count may come from a configuration file or a value entered manually by the user. In this case, the operating system can no longer determine the memory size in advance, and it cannot know the memory size to be allocated until the new int[count] line is executed. Therefore, the operating system allocates a chunk of memory for dynamic allocation. In addition, dynamically allocated memory needs to be managed by clients.

In the Java heap

As stipulated in the JVM specification, all objects in the JVM are stored on the heap (except memory escape). As a result, there is no static allocation of objects in Java, so where the heap and stack come from seems like a no-brainer. But understand why heaps and stacks exist in operating systems.

The difference between heap and stack

Now that you know why you want to distinguish between heap and stack, let’s look at the difference between heap and stack.

  • The heap determines memory size at runtime, whereas the stack determines memory size at compile time

    The reason is the one mentioned in the first section, and it is the original reason for the difference between heap and stack

  • Heap memory is managed by the user (or by the JVM in Java) and stack memory is freed automatically

  • The stack implementation adopts the stack implementation in the data structure, with the sequential characteristics of (LIFO), and the heap is a piece of memory

  • The stack, because of the way it is implemented, is much faster in allocation than the heap. Allocating stack memory is simply moving a pointer

  • In the JVM, the stack is not used directly by the programmer, who generally works with the heap.

  • The stack is thread private and the heap is thread shared

Although there are many differences between heap and stack, these differences are all determined by the operating system, and in terms of hardware, they are all essentially RAM

Why is the heap thread shared and the stack not?

The last point mentioned above is that the stack is thread private and the heap is thread shared. Why??

In fact, it is very simple to solve a problem: communication between threads.

There are two ways to implement communication between threads:

  • The messaging
  • The Shared memory

Shared memory is what we mean by making the heap shared between threads, so that we can share data through objects in the heap, so that other threads can know that a thread has modified some data. There may be thread safety issues, but the advantage is that it is faster and saves memory. Java, C#, etc use this approach

Messaging is each thread private own data space, when need to thread communication, it needs a thread according to send another thread specific message, doing so can make multi-threaded more secure, but private thread space and messaging is brought about by the need to copy of the same object for each thread, variables, etc., And it may cause efficiency problems. Erlang and JoCaml use message-passing threads to communicate.

Respect the success of labor, reproduced with original notes

Reference article:

Why do threads share the heap space?

What’s the difference between a stack and a heap?

What and where are the stack and heap?


If you think it is written well, welcome to pay attention to wechat public number: Yiyou Java, every day from time to time to release some articles about Java advanced, thank you for your attention