Amith
Amith

Reputation: 1967

Object is storage and removal from heap memory

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

Answers (4)

Romain
Romain

Reputation: 12819

Put simply:

  1. obj is allocated on the heap when new Heap() is invoked.
  2. a and b both are allocated on the stack (primitive types, method arguments), the memory will be released upon returning from add.
  3. 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

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78589

When you run your program:

  1. Before the 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.
  2. Within the stack a new frame will be associated with the main method.
  3. The obj reference will be created in the main method stack frame.
  4. When you invoke the 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.
  5. When you invoke the 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).
  6. If the method is found, a new frame is created in the in the stack of the currently executing thread, and the control of execution is transferred to the new method.
  7. Two variables are placed in the method stack frame a and b and they are initialized with copies of the values you passed as arguments (i.e. 4, 3).
  8. The method does what it does, and when it is done the current stack frame is destroyed, and control of execution returns to the previous frame.
  9. Since you invoke the method add again, the process is repeated.
  10. The 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.
  11. The JVM process is terminated and all related resources are freed. Probably in this scenario, the garbage collection actually never run, but if it did, the memory occupied by your object in the heap would be reclaimed.

Upvotes: 1

Atmocreations
Atmocreations

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

Karesh A
Karesh A

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

Related Questions