dtech
dtech

Reputation: 49289

How does Java execution from heap differ from execution from c / c++ stack?

In c / c++ local objects are created on the stack, and data is fed from the stack to the cpu registers.

In Java there is no stack, all objects are allocated on the heap, now for pre-written code the size needed for objects can be calculated and instead of having an overkill c c++ style per object heap allocation entire code blocks are laid down at once. This way Java's heap performance is almost as good and virtually comparable to that of the stack in c c++.

My question is how does the program flow from the heap to end up being executed?

Lets assume I run a function that copies the program code into memory, after the program is in the heap memory, and returns the program entry point address, how do I initiate its execution?

Upvotes: 1

Views: 660

Answers (3)

Cratylus
Cratylus

Reputation: 54074

In Java there is no stack

Of course there is a stack.
When you do new of course the object is allocated to the heap.
But the reference variable e.g. if it is a local variable it is located on the stack.
Also the stack is used for the function parameters (function frame).

Upvotes: 4

If you speak of performance of heap, it means for Java the performance of its garbage collector (GC), which is depending upon its JIT (Just In Time) compiler -the one dynamically translating JVM byte codes to machine code-.

And GC can be remarkably efficient. Read a good garbage collection handbook, and also Andrew's Appel old paper Garbage Collection can be faster than stack allocation

A well designed GC -with a well co-designed JIT- can allocate things really fast. A generational copying GC will spend a small amount of time on young live objects (so the young objects which are dead are essentially sort-of de-allocated "en masse" for free)

Upvotes: 0

cdeszaq
cdeszaq

Reputation: 31280

In Java there is a stack. Just because objects are allocated on the heap doesn't mean that there is no stack. Execution does not happen on the heap, execution is method calls being added to and unwound from the stack, just like the execution flow for C / C++.

Upvotes: 5

Related Questions