• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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: 15714
367
  • 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.
 
I love a good mentalist. And so does this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic