I am testing some JDKs and need to trigger garbage collection multiple times. How can I easily do this in a simple program? Examples would be extremely helpful. Thank you.
I am testing some JDKs and need to trigger garbage collection multiple times. How
Share
See here for a good discussion on garbage collection. You can request that it is run (as noted by previous answers), but it is not a guarantee, so you should not assume too much with the call. Your best option is to create and destroy new objects many, many times. To allow them to be destroyed, create them in a block and close it again. Perhaps something like this:
And you could monitor the memory use externally?