Łukasz Bownik
Łukasz Bownik

Reputation: 6237

Why do my JDialogs leak?

I have a JFrame and I open a JDialog from it and another JDialog from that dialog - which menas i have 3 windows visible (JFrame, JDialog1, Jdialog2).

When I close both dialogs and run a garbage collectior few times (from netbeans profiler) I can see that JDialog2 (the one opened from JDialog1) is garbage collected but JDialog1 (opened from JFrame) is still hanging in live objects pool.

I create new objects every time - so after some time I have an OutOfMemoryError doue to memory leak.

Do I have to treat JDialogs in som special way so they don't leak ?

by the way i do setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE) on both dialogs,

Upvotes: 3

Views: 1210

Answers (3)

cagcowboy
cagcowboy

Reputation: 30858

Have you unregistered all of your listeners on the dialog (including any of it's components)?

Leaving listeners registered can be a major source of memory leaks.

Upvotes: 9

Philipp
Philipp

Reputation: 7659

In order to release allocated resources you have to call the dispose method. Simply hiding the dialog is not enough.

Upvotes: 1

David Berger
David Berger

Reputation: 12803

What is your default close operation? From the java JDialog api:

The value is set to HIDE_ON_CLOSE by default.

What this means is basically that setVisible(false) or a near equivalent of that is called when a user clicks close. The behavior you observe is consistent with that.

Try

jDialog1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE)

Upvotes: 4

Related Questions