I found the way to fix it!
There is method in the JFrame
componet that allows you to “erase” a JFrame: dispose()
which does the following according to the Java API:
Releases all of the native screen resources used by this
Window
, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable.
So the right way to write my code should be:
view.dispose();
model.restart();
view = new View();
That fixes my memory leak.
solved How do I fix memory leaks in Java with a JFrame?