Why use heap memory in Java

Why do we use heap memory, can we use stack memory for the same?


EDITED

Another question came to my mind after reading the answers 1) is there any other kind of memory that we can think of an alternative to heap and stack?


Edited

I came across a string pool, is it memory associated with a heap or a stack?

+6
java heap
source share
7 answers

Well, you need to use a bunch if you want to use objects. Objects are essentially pointers to the stack (or inside other objects) to pieces of memory on the heap.

Unlike C ++, where you can put objects on a stack or a heap, Java does things differently.

Even in C ++, it is recommended to use a bunch for objects that must survive the function in which they were created. You can probably avoid this, but you may run into a performance issue with all copy constructors.


As for your editing:

Is there any other kind of memory that we can think of an alternative to heap and stack?

Of course, there are: static data elements, those where there is only one for each class, and not one instance of the object should go somewhere. They cannot be on the stack, as they can disappear when the function exits. And they cannot belong to a specific object.

These (at least in the Sun / Oracle JVM) fall into the Method area.

In addition, you should not think about having one stack. Each thread gets its own stack when created.

There is also a pool of runtime constants and stacks for native (non-Java) calls.

There is a lot of information about the internal components of the JVM here regarding this aspect, but keep in mind that there may be a difference between the logical machine and the implementation.

+11
source share

Heap memory is central to Java. All objects are allocated on the heap, and local variables contain only references (essentially pointers) to them. C # allows you to have objects (value types) that live on the stack. Java chose not to take this in C ++, in part to simplify the language.

In different languages, heap memory is a way of providing long-lived objects of arbitrary size.

+2
source share

Is it possible to use stack memory for the same?

In principle, no.

In Java, stack memory is used exclusively for parameters and local variable methods (and some hidden accounting data that are not relevant to this discussion). And for these variables, only primitive types use only the memory stack. Any object or array is represented as a reference to something on the heap, and Java does not provide the ability to allocate an object or array on the stack.

[In addition, it is theoretically possible to perform any computation in Java using only primitive types and recursive static methods. However, that would be terribly dirty. Even writing a simple message on the console will cause your application to dump objects. Therefore, we can dismiss this as completely impractical.]

+2
source share

In the JVM, instead of the local thread stack for objects, it uses the Local Stream Distribution Buffer or (TLAB). This provides most of the performance benefits of the stack, without the developer worrying about whether the object is on the stack or not.

+1
source share

Heap memory is used to store objects in Java. It doesn't matter where the object is created in the code. for example, as a member variable, local variable, or class variable, they are always created inside heap space in Java. If there is no memory on the stack to hold the function call or local variable, the JVM will call java.lang.StackOverFlowError, although if there is no more heap space to create the object, the JVM will throw java.lang.OutOfMemoryError Java Heap Space.

+1
source share

Another important factor to consider is the review. If all objects were to be allocated on the stack, they go out of context when the stack frame is deflated. In non-professional terms, think of the stack as a dump, which stores all the values โ€‹โ€‹of the variables and references to objects that are local to the routine / method that is currently in scope. As soon as it completes execution (or the method goes beyond scope), then everything in it will be lost.

0
source share

This simplifies the compiler for managing large and ampere / dynamic sizes of variables - they still occupy a small permanent storage in the call stack - 4 bytes (heap pointer).

0
source share

All Articles