java – Mark-and-sweep garbage collection algorithm


As I understand it, there are two fundamental approaches to garbage collection:

  1. Copying collectors
  2. Mark-and-sweep

Both algorithms are described in this article . According to the second algorithm, the author of the article writes:

  • Objects are allocated in memory
  • Need to run GC
  • The application is suspended
  • The collector walks through the object tree, marking live objects
  • The collector goes through the entire memory, finding all unmarked chunks of memory, storing them in the "free list"
  • When new objects start to be allocated, they are allocated to the memory available in the "free list"

If I understand correctly, the objects that need to be deleted are transferred to the "free list". But where does the removal of these objects actually take place? Or are they not explicitly removed, but simply overwritten when new objects are created?


As far as I understand, there is no need to perform additional operations to delete objects in memory. JVM will create new objects in place of the old ones.

An article on how to create and delete objects in C++

The delete operator does not actually delete anything. It simply returns the memory being pointed to back to the operating system. The operating system is then free to reassign that memory to another application (or to this application again later).

I think it's the same in java , only there is not an operating system, but jvm . (I'm not quite sure, I will be glad if someone will correct it)

Scroll to Top