After reading several articles, it became clear that the Garbage Collector , independently determines which objects have become 'garbage' and independently clears the memory of these objects.

Calling the method for garbage collection will not bring any results, as the garbage collector takes care of all the operations on its own and so that nothing breaks it will ignore the compulsory garbage collection commands.

The question is: is it possible to force the garbage collector not to delete an object from memory and subsequently receive it?

  • yes - you need to save at least one link to this object. - KoVadim
  • And if without it? - JAVAvladuxa
  • write your GC. - KoVadim
  • you can collect jvm without a garbage collector, and why not?) - Artem Konovalov
  • @JAVAvladuxa And how do you want to receive an object to which there are no links? - Arnial

1 answer 1

If you want the object not to be removed by the garbage collector, there are several options:

  1. save link to this object
  2. make a phantom reference reference or override the finalize method and use the "resurrect" object using these mechanisms.
  3. make your jvm build without garbage collection, the source is good
  4. serialize the object and hide it in offheap