0

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?

Flexo
  • 87,323
  • 22
  • 191
  • 272
Amith
  • 1,907
  • 5
  • 29
  • 48
  • `a` and `b` are not on the heap. Only things created by `new` reside on the heap. The storage and removal operation would probably be completely up to the JVM implementors. – James Jun 28 '12 at 11:24
  • Objects are removed from memory when *garbage collector* runs unpredicatebly. It runs when memory is full or system is idle. – Lion Jun 28 '12 at 11:25
  • Java: Memory Allocation For Objects [1]: http://stackoverflow.com/questions/320980/java-memory-allocation-for-objects – Theja Jun 28 '12 at 11:30

4 Answers4

3

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.
Romain
  • 12,679
  • 3
  • 41
  • 54
  • is garbage collector is run only when there is no suffient memmory or in some particular intervals – Amith Jun 28 '12 at 11:34
  • The garbage collector is run magically whenever it's smart to run. (Honestly, you should default to assuming that the GC is at least as smart as you are.) – Louis Wasserman Jun 28 '12 at 11:40
  • @LouisWasserman True. Although one should also expect the GC to be at least as lazy as you are. It's not here to slow down your program, so it'll try to avoid doing work too often ("too aften" being VM-subjective). – Romain Jun 28 '12 at 11:55
2

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

Karesh A
  • 1,731
  • 3
  • 22
  • 47
  • in which case the refernce of object is decreasing. in the above example first value of a is 4 , in second case value of a is 5 what will happen to object a at heap memmory – Amith Jun 28 '12 at 11:27
  • nothing will happen to the heap object it will call the method inside stack. So method processing will be done inside stack with the values of 4 and 5. – Karesh A Jun 28 '12 at 11:30
2

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.

Atmocreations
  • 9,923
  • 15
  • 67
  • 102
1

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.
Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
  • may i ask one thing : is obj will removed from memory when the program terminates. – Amith Jun 28 '12 at 11:59
  • your website is so nice. if the picture is taken by you. it is really appreciable..... so nice – Amith Jun 28 '12 at 12:01
  • @Amith If you refer to the link in my answer, the website is not mine, the information you refer to is from an author named Bill Venners and publisher of the book Inside the Java Virtual Machine, a bit old but still valid. Regarding your other question, when the JVM process is terminated, all resources would be reclaimed by the operating system, therefore everything you had in memory is lost forever. If you refer to my photographs site, thanks ;-) – Edwin Dalorzo Jun 28 '12 at 12:06
  • dude i visited your http://www.dalorzo.com/portfolio/ . really niceand i added to my favorate. i liked so much. it looks so fresh – Amith Jun 28 '12 at 12:11