1

Let's say I have this code:

{
    int var = 2;
    // more code
}

What happens with 'var' after the code is executed and it is not used anymore? Is it deleted from memory or it stays there occupying memory, or something else?

Related to this, is it better to work with variables that way^, or to make some global variable and just change it's value?

2
  • 1
    if variable is instance variable then it will be saved in Heap so removal of that instance is duty of garbage collector. but if its local variable then it will be stored in stack and optimize by compiler on different-different implementation way. Commented Apr 18, 2015 at 19:45
  • @David: you're right. GC is only for objects that are stored on the heap (docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/geninfo/…). Commented Apr 18, 2015 at 20:50

4 Answers 4

3

Local variables live on the stack. If it's a reference to an object then only variable is on the stack.

Instance variables live on the heap because they belong to an object.

Also this post (Stack and heap memory in java) might be helpful.

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

2 Comments

@Prashant It sure sounds like what he's asking.
@chrylis : he is asking when the memory will be freed.
0

To make a long story short, in java (and other JVM languages), you don't have to care about memory allocation and dealocation at all. You really shouldn't be worrying about it. Once you lose the reference to that variable (in this case, when the method call ends), the variable is effectively gone. Some indefinite amount of time after that, the Garbage collecting thread will notice that you can't possibly access that variable anymore, and free up the memory it was using. See: Garbage Collection in Java.

Comments

0

if you are defining any variable as instance variable then that variable will be used by instance. and instance will be saved in Heap memory area. Garbage collector will run periodically to clean non referenced object from memory.

but if that variable is defined inside any block or method then that will be stored Stack memory.

Java Stack memory is used for execution of a thread. They contain method specific values that are short-lived and references to other objects in the heap that are getting referred from the method. Stack memory is always referenced in LIFO (Last-In-First-Out) order. Whenever a method is invoked, a new block is created in the stack memory for the method to hold local primitive values and reference to other objects in the method. As soon as method ends, the block becomes unused and become available for next method.

Comments

0

Everything in Java is removed from memory when it is no longer referenced. It takes a lot of effort to cause true memory leaks in Java.
Java primitives like int, boolean, and char are put on the stack and removed from memory as soon as they leave scope. Java objects like String, arrays, or ArrayList are allocated on the heap (and referenced by the local variable on the stack). Objects are garbage collected (removed from memory) when there is no longer a reference to them.
Static variables belong to a class and will be a reference an object as long as the class is loaded, which is usually the entire run time of the Java program. Statics are the closest thing Java has to global variables, but overuse or misuse of statics is actually a way to cause memory issues rather than solve them.

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.