Reputation: 1967
Can anybody explain to me how objects are stored and removed from heap memory of Java. I'm looking for more information than simply:
an
Object
will removed when there is no reference
For example:
class Heap
{
void add(int a, int b)
{
System.out.println(a+b);
}
public static void main(String ar[])
{
Heap obj=new Heap();
obj.add(4,3);
obj.add(5,5);
}
}
Here how is obj
and a
, `bJ allocated in java memory. When will it be removed from memory by the JVM?
Upvotes: 0
Views: 1995
Reputation: 12819
Put simply:
obj
is allocated on the heap when new Heap()
is invoked.a
and b
both are allocated on the stack (primitive types, method arguments), the memory will be released upon returning from add
.obj
will be removed from the heap whenever the garbage collector runs after the execution is out of main
(the specification does not guarantee the GC will at any given time, it'll figure out when's the right time on its own, although almost-full-heap is probably a very common trigger) - in this case though, since the program would terminate, it would be immediately after returning from main
.Upvotes: 3
Reputation: 78589
When you run your program:
main
method is executed, the JVM will allocate a new thread of execution for you, this thread (called the main thread) will have a stack associated with it, the stack is a place where local variables and temporary data is placed.obj
reference will be created in the main method stack frame.new Heap()
the JVM will attempt to allocate memory for your object in the heap memory area. If successful, it will assign an object reference to your local variable, if it fails, it may attempt to make room for your object running the garbage collector, if still fails to find enough room for it an OutOfMemoryError
is thrown.add(int, int)
method on your memory reference, the JVM will first dereference the pointer to gain access to the actual object in the heap, if it fails a NullPointerException
will be thrown, otherwise it will look for the method to be executed in what is called the method area (an area of memory where class information is located). main
method ends, its stack frame is removed. Now the obj
reference is lost and there is no longer a way to reach to your object in the heap. The object it was pointing to is now illegible for garbage collection.Upvotes: 1
Reputation: 10061
Well... a and b aren't allocated on the heap at all.
They are on the stack for passing into the function. As soon as the execution leaves add()
, the variables a
and b
cannot be used anymore and will be removed by the jvm.
Upvotes: 2
Reputation: 1751
The Heap object will be create inside heap. that will contain the methods inside the class and member variables. when you call a method, that will be loaded inside stack and will be discarded automatically by jvm after executing that method.
Stack section of memory contains methods, local variables and reference variables.
Heap section contains Objects (may also contain reference variables).
Upvotes: 2