user_vs
user_vs

Reputation: 1033

Can an object be stored on the stack instead of the heap in java?

Can an object be stored on the stack instead of the heap?

I recently gone through this blog http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/

Can an object be stored on the stack instead of the heap?

Yes, an object can be stored on the stack. If you create an object inside a function without using the “new” operator then this will create and store the object on the stack, and not on the heap. Suppose we have a C++ class called Member, for which we want to create an object. We also have a function called somefunction( ). Here is what the code would look like:

they suggesting that Objects can stored in Heap. and it was confusing.

I thought that,

  1. All objects in Java are stored on the heap.
    whether it is created by
    a. new Keyword
    b. Using Class.forName().
    c. Using clone().
    d. Using newInstance() method
    e. Using Object Deserialization.

  2. Methods,threads and variables are in stack.

  3. Class variables(Static variables) are stored as part of the Class object associated with that class. This Class object can only be created by JVM and is stored in permanent generation.

Please do correct me if i am wrong.

Now my doubt is whether objects can reside or stored in stack in any form.

Thank you.

Upvotes: 14

Views: 10539

Answers (4)

outdev
outdev

Reputation: 5492

All class instances are stored in the heap.

Here are some credible sources:
From JVM spec:
https://docs.oracle.com/javase/specs/jvms/se8/jvms8.pdf

2.5.3 Heap

The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. The heap is the run-time data area from which memory for all class instances and arrays is allocated.

From Java Language Spec:
https://docs.oracle.com/javase/specs/jls/se8/jls8.pdf

17.4.1 Shared Variables

Memory that can be shared between threads is called shared memory or heap memory. All instance fields, static fields, and array elements are stored in heap memory.

Upvotes: 8

Dariusz
Dariusz

Reputation: 22241

All Java objects have to be allocated at the heap:

JVMS 8 2.5.3 :

[...] The heap is the run-time data area from which memory for all class instances and arrays is allocated

However, due to escape analysis, some objects may be optimized to primitive types and stored on the stack. They never actually become objects in such case, thus not really violating the rule.

Upvotes: 2

RealSkeptic
RealSkeptic

Reputation: 34628

Your point (1): All objects are allocated on Heap.

This is pretty much true. Objects are always allocated on the heap. However, an object may be allocated on the stack if its escape analysis allows it to (it is only visible to the calling method, and doesn't escape outside it), as discussed in this article on the IBM web site.

However, note that Oracle specifically says that it does not replace heap allocation with stack allocation as a result of its escape analysis in this technical note.

So yes, objects are always on the heap in (Oracle) Java.


Your point (2): Methods, threads and variables are on the stack.

No, methods and threads are not on the stack. Local variables and arguments are placed on the stack. On each method invocation, a stack frame is prepared and space is allocated in it for the arguments, return value and local variables.

The methods themselves are part of the class. Thread objects are allocated like any other object, but the threads themselves are not data and are not allocated, but rather stacks are allocated for them.


Your point (3): Static variables are allocated on the PermGen.

This information was true up until Java 8. According to JEP 122, they are now allocated on the heap, as the PermGen has been eliminated.

Note that such implementation details are different between different implementations of the JVM. Other JVMs have gotten rid of the PermGen even before Java 8.

Upvotes: 8

rghome
rghome

Reputation: 8819

The comment you have quoted refers to C++, which can store objects on the stack. In Java, you cannot.

Upvotes: 2

Related Questions