0

let's consider swing java application that opens and closes many frames during running.

Frames can be open, closed, reopen again. Many different frames can be open at the same time.

Each frame is considered as a spring prototype scoped bean. But it seems that it doesn't even matter here, because spring doesn't manage the references of prototype scoped beans.

The problem arises when user closes frame. What happens when closing:

@Override
public void closeFrame(OurFrame frame) {

    if (isFrameOpened(frame)) {
        OurFrame openFrame = activeFrames.get(frame);

        frame.setVisible(false);
        frame.dispose();
        activeFrames.remove(false);
        frame = null;
    }
}

activeFrames keep all currently open frames. closeFrame(...) method is invoked by WindowAdapter when user decides to close window.

After all, as sampler says, the closed frames still hang in memory. Heap increases and if you try to open same frame second time, application creates new, another instance, still keeping all the hanging ghosts in memory.

In the end of the day, there can be a lot of them, what makes the problem... There are no frames that are eligible for garbage collector at all.

There are no explicit references referring to closed frames. VisualVM says that there are two referents: WeakReference and WeakHashMap$Entry.

Does anybody have any clue ?

Regards, Dreando

Dreando
  • 1
  • 1
  • *"let's consider swing java application that opens and closes many frames during running."* Way ahead of you. See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Apr 15 '15 at 12:41
  • What is `activeFrames`? – user253751 Apr 15 '15 at 12:45
  • @AndrewThompson The problem still exists without JFrames. Consider "Let's consider a Swing Java application that creates and destroys many panels during running" – user253751 Apr 15 '15 at 12:46
  • @immibis activeFrames is a ConcurrentHashMap that keeps all currently open frames – Dreando Apr 15 '15 at 12:49

1 Answers1

0

Well... what helped was:

frame.removeAll()

before disposing it. But this is only partial solution because it removes frame from heap but it doesn't remove any prototype children of this frame...

Now visualvm says that those children are referred from... layout in removed frame. That's weird.

Dreando
  • 1
  • 1