Or there is no possibility to control the release of memory?

  • you can manually start the garbage collector unless. But not the fact, of course, that it will clear the memory of your object with this call. - teran
  • if the answer helped, you can put a tick to accept it as true - StiG
  • Unfortunately did not see where this can be done. - Ididon
  • @ Identicon to the left of the answer, there is a check mark under the check marks arrows. But it is of course up to you. - StiG

1 answer 1

Send the object pointer to another object, or to null . The old object will remain without reference to it and will be subject to removal from memory. After either wait until the GC collects the garbage itself, or prompt the application to start it using the System.gc() command. However, this also does not guarantee the immediate launch of the GC.

  • However, System.gc does not mean at all that the collector will start its work momentarily. JVM decides when to do it. Moreover, this challenge causes the application to sink in performance - Alexey Shimansky
  • @Alexey Shimansky I’m certainly not sure that it always starts right away with a forced indication, but in my cases it always helped to free up memory. And it was clearly visible in VisualVM how it was launched. - StiG
  • I think this is some lucky luck. No more. - Alexey Shimansky
  • Even guys with enSO stackoverflow.com/questions/15632734/… write about this When you make call using System.gc(), it is just a request to JVM and JVM can anytime decide to ignore it. and so on. - Alexey Shimansky
  • @ Alexey Shimansky corrected the answer. Thanks for the info - StiG