3

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

1
  • 2
    What do you specifically mean with "static" and "dynamic" memory allocation ? Commented Aug 30, 2009 at 18:56

4 Answers 4

2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

can you please give an example of both static and dynamic allocation both??
0

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).

3 Comments

In Java all parameters are passed by value.
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
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. stackoverflow.com/questions/40480/is-java-pass-by-reference
0

'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'.

Comments

-1

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.

1 Comment

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 java.sun.com/javase/6/webnotes/6u14.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.