Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 803987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:50:45+00:00 2026-05-14T23:50:45+00:00

I have a JFrame that accepts top-level drops of files. However after a drop

  • 0

I have a JFrame that accepts top-level drops of files. However after a drop has occurred, references to the frame are held indefinitely inside some Swing internal classes. I believe that disposing of the frame should release all of its resources, so what am I doing wrong?

Example

import java.awt.datatransfer.DataFlavor;
import java.io.File;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.TransferHandler;

public class DnDLeakTester extends JFrame {
    public static void main(String[] args) {
        new DnDLeakTester();

        //Prevent main from returning or the jvm will exit
        while (true) {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {

            }
        }
    }
    public DnDLeakTester() {
        super("I'm leaky");

        add(new JLabel("Drop stuff here"));

        setTransferHandler(new TransferHandler() {
            @Override
            public boolean canImport(final TransferSupport support) {
                return (support.isDrop() && support
                        .isDataFlavorSupported(DataFlavor.javaFileListFlavor));
            }

            @Override
            public boolean importData(final TransferSupport support) {
                if (!canImport(support)) {
                    return false;
                }

                try {
                    final List<File> files = (List<File>) 
                            support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);

                    for (final File f : files) {
                        System.out.println(f.getName());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return true;
            }
        });

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        pack();
        setVisible(true);
    }
}

To reproduce, run the code and drop some files on the frame. Close the frame so it’s disposed of.

To verify the leak I take a heap dump using JConsole and analyse it with the Eclipse Memory Analysis tool. It shows that sun.awt.AppContext is holding a reference to the frame through its hashmap. It looks like TransferSupport is at fault.

image of path to GC root http://img402.imageshack.us/img402/4444/dndleak.png

What am I doing wrong? Should I be asking the DnD support code to clean itself up somehow?

I’m running JDK 1.6 update 19.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:50:46+00:00Added an answer on May 14, 2026 at 11:50 pm

    Although the DropHandler is not removed from the static AppContext map, that is not really the root cause, but merely one of the causes in the chain. (The drop handler is intended to be a singleton, and not cleared up until the AppContext class is unloaded, which in practice is never.) Use of a singleton DropHandler is by design.

    The real cause of the leak is that the DropHandler sets up an instance of TransferSupport, that is reused for each DnD operation, and during a DnD operation, provides it with a reference to the component involved in DnD. The problem is that it does not clear the reference when DnD finishes. If the DropHandler called TransferSupport.setDNDVariables(null,null) when the DnD exited, then the problem would go away. This is also the most logical solution, since the reference to the component is only required while DnD is in progress. Other approaches, such as clearing the AppContext map are circumventing the design rather than fixing a small oversight.

    But even if we fix this, the frame would still not be collected. Unfortunately, there appears to be another problem: When I commented out all the DnD related code, reducing to just a simple JFrame, this too was not being collected. The retaning reference was in javax.swing.BufferStrategyPaintManager. There is a bug report for this, as yet unfixed.

    So, if we fix the DnD, we hit another retention problem with repainting. Fortunately, all of these bugs hold on to only one frame (hopefully the same one!), so it is not as bad as it could be. The frame is disposed, so native resources are released, and all content can be removed, allowing that to be freed, reducing the severity of the memory leak.

    So, to finally answer your question, you are not doing anything wrong, you are just giving some of the bugs in the JDK a little air time!

    UPDATE: The repaint manager bug has a quick fix – adding

    -Dswing.bufferPerWindow=false
    

    To the jvm startup options avoids the bug. With this bug quashed, it makes sense to post a fix for the DnD bug:

    To fix the DnD problem, you can add a call to this method at the end of importData().

                private void cancelDnD(TransferSupport support)
                {
                    /*TransferSupport.setDNDVariables(Component component, DropTargetEvent event) 
                    Call setDNDVariables(null, null) to free the component.
    */
                    try
                    {
                        Method m = support.getClass().getDeclaredMethod("setDNDVariables", new Class[] { Component.class, DropTargetEvent.class });
                        m.setAccessible(true);
                        m.invoke(support, null, null);
                        System.out.println("cancelledDnd");
                    }
                    catch (Exception e)
                    {
                    }
                }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JFrame that has a large number of changing child components. (Many
I have a Jframe class that has a login and password fields. When loggin
I have a main JFrame that is interactive, i.e. user sets different options. After
I have made a JFrame and inside that frame, there is panel on which
I have this class called PollFrame that extends JFrame in a file called PollFrame.java
I have a program that creates a JFrame and makes it visible. Is there
I have an application that is works fine and the JFrame for it is
I have a JFrame which contains a JApplet. There are shortcut keys that I
I have a main JFrame that calls a class(A) and that class calls another
I have an application with a main JFrame that contains a default list model.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.