As far as I understand, 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 writes:

  • Objects are allocated in memory.
  • GC needs to run
  • Application is suspended
  • The collector walks through the object tree, marking live objects.
  • The collector traverses the entire memory, finding all unchecked chunks of memory, storing them in the "free list"
  • When new objects begin to allocate, 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, in fact, is the removal of these objects? Or are they clearly not deleted, but simply overwritten when creating new objects?

  • What do you mean by explicit deletion? - Mikhail Vaysman
  • @MikhailVaysman as far as I understand, the author wants to understand what is happening with the memory that was occupied by dead objects, it vanishes or remains unchanged or something else happens to it. - Artem Konovalov
  • @ArtemKonovalov wait for clarification from the vehicle. - Mikhail Vaysman
  • @MikhailVaysman, Freeing memory from objects. - Burence
  • @ArtemKonovalov, Yes, quite right. - Burence

2 answers 2

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

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

The delete operator doesn’t actually delete anything. It simply returns the memory system. It is then that the operating system is reassigned.

I think in java similar, but there is not an operating system, but jvm . (Not quite sure, I will be glad if someone corrects)

    If the object is no longer accessible by a strong reference (strong reference), then it can be collected by the garbage collector. The garbage collector may call the finalize method on this object. This method, potentially, can restore an object — make it accessible by a strong link (strong reference).

    The collector marks the memory as "free", the garbage collector does not produce any additional measures for clearing the memory.

    When creating a new object at the time of initialization, all fields will be set to the default value.

    If it is necessary to remove secret information (password, key, etc.), then it must be done independently and as soon as possible. As in the case of a memory dump, it may become available. Also, some of the information can be copied to another place of the heap and it will no longer be possible to clear the "old" area.