6

When I say something like:

Thread t1 = new Thread();

does it create it on a heap or a stack?

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Chiran
  • 183
  • 1
  • 5
  • 14
  • The "visible" part is in the JVM heap. There is also, for a running thread, at least, an OS-specific part that is allocated elsewhere, but it's all handled automatically. – Hot Licks Oct 17 '13 at 17:42

3 Answers3

11

There is no way to allocate objects on the stack in Java.
The stack can only hold references and primitives, and only for local variables.

Note that starting a thread will create a new stack for that thread.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    +1 To be more specific, the `Thread` object and its fields are allocated on the heap. The stack memory associated with the new thread is allocated by the JVM/OS but that is _not_ on the heap. It is allocated in a different section of memory. – Gray Oct 17 '13 at 17:45
  • You can't allocate objects on stack explicitelly but the JVM can allocate objects on stack as an optimization if they are small enough and are not escaping the current thread. See this article at section "Stack allocation": http://www.ibm.com/developerworks/java/library/j-jtp09275/index.html – dcernahoschi Oct 17 '13 at 18:08
  • Note: Java 7/8 does allow objects to be placed on the stack. – Peter Lawrey Jun 21 '16 at 07:29
  • @PeterLawrey: No, it does not. – SLaks Jun 21 '16 at 15:06
  • @SLaks http://docs.oracle.com/javase/7/docs/technotes/guides/vm/performance-enhancements-7.html Ok, more specifically "the server compiler eliminates scalar replaceable object allocations and associated locks from generated code. " and it does this by using the stack instead of creating an object on the heap. – Peter Lawrey Jun 21 '16 at 15:28
10
Thread t1 = new Thread();

tl;dr This allocates object i.e. t1 in heap.

As each new thread comes into existence, it gets its own pc register (program counter) and Java stack. If the thread is executing a Java method (not a native method), the value of the pc register indicates the next instruction to execute. A thread's Java stack stores the state of Java (not native) method invocations for the thread. The state of a Java method invocation includes its local variables, the parameters with which it was invoked, its return value (if any), and intermediate calculations. The state of native method invocations is stored in an implementation-dependent way in native method stacks, as well as possibly in registers or other implementation-dependent memory areas.

The Java stack is composed of stack frames (or frames). A stack frame contains the state of one Java method invocation. When a thread invokes a method, the Java virtual machine pushes a new frame onto that thread's Java stack. When the method completes, the virtual machine pops and discards the frame for that method.

The Java virtual machine has no registers to hold intermediate data values. The instruction set uses the Java stack for storage of intermediate data values.

Figure shows a snapshot of a virtual machine instance in which three threads are executing. At the instant of the snapshot, threads one and two are executing Java methods. Thread three is executing a native method. It also shows of the memory areas the Java virtual machine creates for each thread, these areas are private to the owning thread. No thread can access the pc register or Java stack of another thread.

enter image description here

Trying
  • 14,004
  • 9
  • 70
  • 110
  • thank you @Trying! But could explain me what is a native method of the thread? and this satement - "The state of native method invocations is stored in an implementation-dependent way in native method stacks, as well as possibly in registers or other implementation-dependent memory areas." – Chiran Oct 17 '13 at 21:00
  • 1
    if a thread does some native operation, jni stuff. And for the other question it means diff os has different implementation in terms of thread and space allocation. – Trying Oct 17 '13 at 22:43
  • Hi, Please clarify if native includes OS Level API call ? Like for invocation of the read() , write() methods in Java I/O. – Rahul Saini Sep 16 '15 at 17:14
  • Note: Java 7/8 does allow objects to be placed on the stack. – Peter Lawrey Jun 21 '16 at 07:29
2

In Java 8, using Escape Analysis objects can be created on the stack. This occurs when an object is detected as not escaping the current method (after inlining has been performed) Note: this optimisation is available in Java 7, but I don't think it worked as well.

However, as soon as you call start() it will escape the current method so it must be placed on the heap.

When I say something like:

Thread t1 = new Thread();

does it create it on a heap or a stack?

It could place it on the stack, provided you don't use it to create a real thread. i.e. if you so

Thread t1 = new Thread(runnable);
t1.start();

It has to place it on the heap.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130