jvm - Memory Allocation in Java for Parameters -
would data in following statement stored automatic memory allocation, or dynamic memory allocation or both
myfunction(new myclass());
thank you!
the terms “automatic memory allocation” , “dynamic memory allocation” make no sense in context of java. in java, memory managed execution environment.
in other programming languages, terms “automatic storage” , “dynamic storage” used distinguish between storage, automatically deallocated when going out of scope, , storage, requires explicit deallocation action performed application. in java, there no explicit deallocations @ all. find people , literature continue distinguish between stack , heap, latter contains objects, lifetime may exceed execution of method in created. this, however, logical separation, might not reflect how particular jvm implementation works.
the java® language specification doesn’t mandate details workings of this. there 2 spots @ all:
15.12.4.5. create frame, synchronize, transfer control
a method
m
in classs
has been identified 1 invoked.now new activation frame created, containing target reference (if any) , argument values (if any), enough space local variables , stack method invoked , other bookkeeping information may required implementation (stack pointer, program counter, reference previous activation frame, , like). if there not sufficient memory available create such activation frame,
stackoverflowerror
thrown.
17.4.1. shared variables
memory can shared between threads called shared memory or heap memory.
all instance fields,
static
fields, , array elements stored in heap memory. in chapter, use term variable refer both fields , array elements.local variables (§14.4), formal method parameters (§8.4.1), , exception handler parameters (§14.20) never shared between threads , unaffected memory model.
note place term “heap” used memory type , how it’s actually defined here…
the sections §15.9.4. run-time evaluation of class instance creation expressions , §15.10.2. run-time evaluation of array creation expressions more vague, saying “space allocated” , outofmemoryerror
thrown if not enough space available, , nothing more.
so if go route of distinguishing between stack , heap, can code myfunction(new myclass());
cause heap allocation instance of myclass
, followed stack allocation activation frame of actual implementation of myfunction
method. not matters practical purpose.
if want dig more way, jvms may implement it, may refer java® virtual machine specification instead:
2.5.2. java virtual machine stacks
each java virtual machine thread has private java virtual machine stack, created @ same time thread. java virtual machine stack stores frames (§2.6). java virtual machine stack analogous stack of conventional language such c: holds local variables , partial results, , plays part in method invocation , return. because java virtual machine stack never manipulated directly except push , pop frames, frames may heap allocated. memory java virtual machine stack not need contiguous.
2.5.3. heap
the java virtual machine has heap shared among java virtual machine threads. heap run-time data area memory class instances , arrays allocated.
the heap created on virtual machine start-up. heap storage objects reclaimed automatic storage management system (known garbage collector); objects never explicitly deallocated. java virtual machine assumes no particular type of automatic storage management system, , storage management technique may chosen according implementor's system requirements. heap may of fixed size or may expanded required computation , may contracted if larger heap becomes unnecessary. memory heap not need contiguous
note, how these definitions differ in purpose while not having significant difference in constraints, i.e. might fixed-size or resizable , might contiguous or not, not speak of explicit mentioning of possibility allocate stack frames on heap.
Comments
Post a Comment