views:

661

answers:

4

In java Can objects be created with both static memory allocation and dynamic memory allocation?

+1  A: 

If by static memory, you mean on the stack, no, all objects are allocated on the heap. Only primitives are allocated on the stack.

Edit: I'm still not sure if by dynamic and static you mean heap and stack, respectively, but that is usually where the question comes from for people with a C/C++ background, because those languages give the developer control over that.

In Java, when you do a typical:

 Object o = new Object();

That will allocate memory on the heap. If inside a method you do:

 int i = 1;

Then that int is allocated on the stack (if it is a field in a class, then it will be allocated on the heap).

Yishai
can you please give an example of both static and dynamic allocation both??
A: 

All instance memory (by calling new) is allocated on the heap, all parameters are allocated on the stack. But java (non primitive) paramters are all passed by reference (excepty primitives).

Arne Burmeister
In Java all parameters are passed by value.
Esko Luontola
All instances are passed by a reference like a pointer to the instance. Right, the pointer is passed by value, but calling a method on the passed object will change the original
Arne Burmeister
Exactly, Java is always pass-by-value, which means that also object references are passed by value (and in Java objects are always accessed through a reference). That is completely different from pass-by-reference. http://stackoverflow.com/questions/40480/is-java-pass-by-reference
Esko Luontola
A: 

The answers claiming that non-primitives are always allocated on the heap are dead wrong.

JVMs can do escape analysis to determine whether objects will always be confined to a single thread and that the object's lifetime is bounded by the lifetime of a given stack frame. If it can determine that an object can be allocated on the stack, a JVM may allocate it there.

See this article for details.

Mike Daniels
Java 6u14 (when -XX:+DoEscapeAnalysis is enabled) uses escape analysis for scalar replacement, not stack allocation. Objects are *never* allocated on the stack, but their fields are inlined and treated as local variables. See http://java.sun.com/javase/6/webnotes/6u14.html
Esko Luontola
A: 

'Static' doesn't mean 'on the stack'.

Objects allocated in the initialisation of class-static variables, or in static code blocks, are statically allocated, in the sense that allocation is done at class-load time (which can be made to happen statically immediately after program startup).

You could, in theory, write a java program using only such allocations, and it would be statically allocated, the same as a C program that never called malloc, just had fixed buffers for the stuff it wanted to do.

If such a program successfully starts up, that proves there is enough memory available for everything it can do, and so it will never get an out of memory error, fragmentation problem, or GC pause.

It will just, if correctly written, return a lot of error messages saying 'I can't do that'.

soru