Each time an object is created in java, it goes into an area of memory known as The Heap. ( Heap – heap is an area of pre-reserved computer main storage( memory ) that a program process can use to store data in some variable amount that won’t be known until the program is running.)
All Objects – no matter when , where, or how they’re created – live on the the Heap.
But its not just any old memory heap; the Java heap is actually called the Garbage-Collectible heap.
When you create an Object, Java Allocates memory space on the heap according how much that particular object needs.
An Object with say , 15 instance variables, will probably need more space than an Object with only two instance variables.
But what happens when you need to reclaim the space ? How do you get an object out of the heap when you’re done with it ? Java manages that memory for you! when the JVM can ‘see’ that an Object can never be used again, that object become eligible for garbage Collection.
And if you’re running low on memory, the Garbage Collector will run, throw out the unreachable objects, and free up the space, so that the space can be reused.