• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is really happening when an Object is created or removed from Memory at runtime?

 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can someone explain me what will happen when an Object is created or removed from memory at runtime?

Say for example, I just instantiated an Employee Object. Then what will happen in Memory?

Here I am putting my understanding on it.

First some memory will be reserved for the newly created Employee object in Heap memory, then initialize and store its defaults properties values, then a pointer to this memory location will be stored in Stack memory as a reference.

Suppose When an object is eligible for garbage collection and GC runs what is going to happen?

Will it remove the Object reference from Stack and clear the memory allocated on Heap?
If it cleans the Employee object from heap what exactly it will do on that memory location?

Can someone please explain whats really happening behind the scenes?

Thanks,
Siva
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JVM keeps track of which parts of the heap are being used to store instances. When the garbage collector runs, it doesn't actually clear the memory. It just remembers that the space that the instance took up is free to be overwritten by a new instance.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic