-1

Would the data in the following statement be stored as automatic memory allocation, or dynamic memory allocation or both

myFunction(new MyClass());

Thank You!

Holger
  • 285,553
  • 42
  • 434
  • 765
Anonymous Dodo
  • 261
  • 3
  • 17

4 Answers4

4

The terms “automatic memory allocation” and “dynamic memory allocation” make no sense in the context of Java. In Java, all memory is managed by the execution environment.

In other programming languages, the terms “automatic storage” and “dynamic storage” are used to distinguish between storage, which is automatically deallocated when going out of scope, and storage, which requires an explicit deallocation action performed by the application. In Java, there are no explicit deallocations at all. You will find that people and literature continue to distinguish between stack and heap, where only the latter contains objects, whose lifetime may exceed the execution of the method in which they were created. This, however, is only a logical separation, which might not reflect how a particular JVM implementation actually works.

The Java® Language Specification doesn’t mandate much details about the workings of this. There are only two spots at all:

15.12.4.5. Create Frame, Synchronize, Transfer Control

A method m in some class S has been identified as the one to be invoked.

Now a new activation frame is created, containing the target reference (if any) and the argument values (if any), as well as enough space for the local variables and stack for the method to be invoked and any other bookkeeping information that may be required by the implementation (stack pointer, program counter, reference to previous activation frame, and the like). If there is not sufficient memory available to create such an activation frame, a StackOverflowError is thrown.

 

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. In this chapter, we use the term variable to refer to both fields and array elements.

Local variables (§14.4), formal method parameters (§8.4.1), and exception handler parameters (§14.20) are never shared between threads and are unaffected by the memory model.

Note that this is the only place where the term “heap” is used as memory type and how it’s actually defined here…

The sections §15.9.4. Run-Time Evaluation of Class Instance Creation Expressions and §15.10.2. Run-Time Evaluation of Array Creation Expressions are much more vague, saying that “space is allocated” and an OutOfMemoryError is thrown if not enough space is available, and nothing more.

So if you go the route of distinguishing between stack and heap, you can say that your code myFunction(new MyClass()); will cause a heap allocation for an instance of MyClass, followed by a stack allocation for the activation frame of the actual implementation of the myFunction method. Not that it matters for any practical purpose.


If you want to dig more into the way, JVMs may implement it, you may refer to the Java® Virtual Machine Specification instead:

2.5.2. Java Virtual Machine Stacks

Each Java Virtual Machine thread has a private Java Virtual Machine stack, created at the same time as the thread. A Java Virtual Machine stack stores frames (§2.6). A Java Virtual Machine stack is analogous to the stack of a conventional language such as C: it holds local variables and partial results, and plays a part in method invocation and return. Because the Java Virtual Machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated. The memory for a Java Virtual Machine stack does not need to be contiguous.

 

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.

The heap is created on virtual machine start-up. Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector); objects are never explicitly deallocated. The Java Virtual Machine assumes no particular type of automatic storage management system, and the storage management technique may be chosen according to the implementor's system requirements. The heap may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger heap becomes unnecessary. The memory for the heap does not need to be contiguous

Note, how these definitions only differ in their purpose while not having significant difference in their constraints, i.e. might be fixed-size or resizable and might be contiguous or not, not to speak of the explicit mentioning of the possibility to allocate stack frames on the heap.

Community
  • 1
  • 1
Holger
  • 285,553
  • 42
  • 434
  • 765
1

There are three groups of memory in Java

  1. Heap - this is where Objects are created and located when you call new MyClass();
  2. Stack - The stack contains stack frames, and stack frames have space allocated for primitives and pointers to Objects in the Heap. Stack frames are allocated on method call and deallocated on method return
  3. String constants: String literals defined at compile time will be added to a String constant pool. At runtime, it is possible to add strings to the pool using String.intern()

That's it

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
1

There are two things being allocated in your example;

  1. The expression, new MyClass() allocates an instance of MyClass on the object heap.

  2. The result of the new expression is an object reference. The object reference is saved in the activation record for a call to myFunction(). Activation records are allocated on the calling thread's call stack.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
1

When you create a new object, it often allocated on the heap however with escape analysis, it can be unpacked onto the stack as if it was a local variable.

The only allocation is via new or a capturing lambda (or some native method in rare cases) There isn't any explicit distinction between different ways of allocating an object.

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