0

I would like to know how to place a local variable in memory? In method1, do the variable take a place into memory, one time? In method2, do the variable take a place after deleting old place in memory, for each time?

public void method1() {
    Object obj = null;
    for(.....) {
        obj = come from other-->

    }
}

public void method2() {
    for(.....) {
        Object obj = come from other-->
    }
}

5 Answers 5

2

You have a local variables which may be in a register or once in memory.

You also have an object which the local variable references. This will be created on each iteration in both cases.

They are effectively the same, except I would prefer the second case if it is possible as it narrows the scope of the local variable.

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

2 Comments

Primatic data type for one time? Object data type for each times?
local variables such as primitives or references once for call to the method. Objects are created on the heap every time new is used. Note: Object obj is a reference to an Object, not an Object on the stack.
1

Each method call is associated with Activation Record that is stored on a call stack. The activation record holds references to the memory blocks in heap corresponding to the method level variables. Once the method call returns to the caller, this activation record will be removed from the stack and the memory references are potentially available to be garbage-collected.

In your case,

  1. the obj in the first method, it's reference is stored in the call stack and the actual memory is on the heap and this is done once per method call.
  2. the obj in the for loop in the second method is created once for each iteration and goes out of scope at the end of each iteration. So, the reference and the memory on the heap are allocated for each iteration.

2 Comments

Both methods just create one times?
Do you mean to say that both methods create the object only once in the memory? No, the first one creates once per method call, the second one creates 'n' times per method call where 'n' is the number of times that for loop runs.
1

The local variables are usually (unless e.g. optimized away) kept on the stack memory. But they can only store primitive values or references. The referenced objects themselves are usually allocated on the heap (withstanding any JIT optimization).

See Stack based memory allocation (Wikipedia) vs. Heap based memory allocation (Wikipedia).

Storing values on the stack is very cheap. Similar to a function call, where you store the return pointer on the stack. It does not require much more than incrementing the stack pointer (and you can imagine that incrementing a dedicated CPU register is fast!)

The object itself is different. Note that theoretically, some java compiler or JIT might be able to optimize your second code better, because you indicate clearly that the value is not needed for the next iteration. (An even better compiler should be able to figure this out itself.)

In general, a modern compiler should produce the same machine code after optimization for both cases. (This may happen in the JIT compiler, so the Java bytecode may still show the difference).

Anyway: do not try to overoptimize by reusing local variables. Instead, write explicit code and let the compiler optimize. By using a fresh variable inside the loop, you make it explicit that it is not reused anywhere. This can prevent some programming errors!

Comments

0

I believe in both cases a new Object is created in memory for every iteration. It is up to the garbage collector to notice that there are no references to any but the most 'recent' Object.

Comments

0

Objects in method1 and method2 will be placed in heap, but java compiler perform Escape analysis for determination we need release this kind of object after method execution or not. Escape analysis is implemented in Java Standard Edition 6

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.