Aujourd'hui, j'ai ouvert le Gestionnaire des tâches et j'ai vu que mon application perdait 200 Ko de mémoire par seconde. J'ai regardé ma boucle principale:JFrame fuite de mémoire?
public final void run() {
try {
Thread.sleep(27);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
Thread curThread = Thread.currentThread();
long lastLoopTime = System.nanoTime();
long OPTIMAL_TIME = 1000000000/FPS;
int fps = 0;
long lastFpsTime = 0;
while (thread == curThread) {
if (shouldClose)
{
running = false;
frame.dispose();
thread = null;
curThread.interrupt();
curThread = null;
}
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
//double delta = updateLength/((double)OPTIMAL_TIME);
lastFpsTime += updateLength;
fps++;
if (lastFpsTime >= 1000000000) {
System.out.println("FPS: " + fps + "");
fpsLabel.setText("FPS: " + fps);
lastFpsTime = 0;
fps = 0;
}
if (GuiNewProject.createButton.isEnabled() && createProjectDialogIsOpen)
if (GuiNewProject.folder.getText().length() == 0 || GuiNewProject.projectName.getText().length() == 0)
GuiNewProject.createButton.setEnabled(false);
if (!(GuiNewProject.createButton.isEnabled()) && createProjectDialogIsOpen)
if (GuiNewProject.folder.getText().length() > 0 && GuiNewProject.projectName.getText().length() > 0)
GuiNewProject.createButton.setEnabled(true);
//render();
fpsDone.setText("FPS: " + fps);
try {
if (shouldClose) {
return;
}
else
{
Thread.sleep((lastLoopTime - System.nanoTime() + OPTIMAL_TIME)/1000000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
SwingUtilities.invokeLater(this);
}
Et je n'arrive pas à comprendre pourquoi cela continue de fuir la mémoire? Toute indication ou solution à cette fuite de mémoire serait utile!
Cordialement, tambre
S'il vous plaît créer un [SSCCE] (http://www.sscce.org) qui démontre le problème et met à jour votre question. –
n'est pas possible à partir de code_snipped posté ici, pour une meilleure aide plus tôt post un SSCCE, court, runnable, compilable – mKorbel
FYI tout invoqué depuis invokeLater & killed par Thread.sleep (int) reste dans la mémoire JVM jusqu'à Thread.sleep (int) terminé – mKorbel