1

I have a JFRAME that has it's setDefaultCloseOperation to JFrame.DISPOSE_ON_CLOSE.but the problem is that most of it's threads are not closed after being disposed. If I put it on JFrame.Exit_ON_CLOSE my whole system exits which is not what I want. Is there any way to just dispose and completely release all it's threads and resources after closure and not have all my system exited?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
armin
  • 1,985
  • 6
  • 38
  • 52
  • Check following thread on stackoverflow http://stackoverflow.com/questions/1234912/how-to-programmatically-close-a-jframe – voidMainReturn Jul 01 '13 at 08:32
  • Dispose releases the native resources, it may not release the Java objects referenced by it – MadProgrammer Jul 01 '13 at 08:35
  • 1
    What do you mean by "most of it's threads are not closed" ? What thread are you talking about ? – barjak Jul 01 '13 at 08:55
  • @MadProgrammer I also need to release those too,because in eclipse debug I close the window but I still keep seeing the threads of window still open and I have terminate manually.I don't open any threads manually if you need to know. – armin Jul 01 '13 at 08:57
  • There are various system threads which may come into existence when you use Swing. You don't need to terminate them; they're idle, not consuming much in the way of resources. I think you're trying to solve a problem that doesn't exist. – davmac Jul 01 '13 at 09:36
  • The simplest way is to make sure that the frame is no longer maintaining any strong references to any other objects – MadProgrammer Jul 01 '13 at 09:50
  • @davmac thanks.They seem like this,I thought they are consuming resources because this window will be opened and closed many times,So I though this might cause a memory issue in long term. – armin Jul 01 '13 at 10:03
  • 2
    @armin I'm sure you'll find that it's not the case that a thread (or threads) is created per window. The thread(s) are created when you first create a window, and may remain until the program terminates, but you can create further windows without further threads being created as a result. – davmac Jul 01 '13 at 10:07
  • 3
    There are always Daemon Threads running at the backend, one of them which runs on the low priority is the Garbage Collector, hope you not trying to stop that too :-) For little help on the topic, here is a line from [Java Docs related to Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) - __"The programmer does not need to provide code that explicitly creates these threads: they are provided by the runtime or the Swing framework. The programmer's job is to utilize these threads to create a responsive, maintainable Swing program."__ – nIcE cOw Jul 01 '13 at 10:56

2 Answers2

2

As shown here, some memory allocated by the host platform for the heavyweight peer of a top-level container may not be reclaimed until the JVM exits. The expedient solution is to use a single top-level container. The JVM may use multiple host threads in the normal course of execution; profile your application to identify leaks. It may help to compare the live thread display with a thread dump, for example.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • @armin: _I want to know where are those thread handles are stored_. As discussed [here](http://stackoverflow.com/q/2653458/230513), Oracle's JVM uses host threads. You might look among your platform's developer tools, as well as your chosen [profiler](http://stackoverflow.com/q/2064427/230513). – trashgod Jul 01 '13 at 13:12
-1

EXIT_ON_CLOSE will exit the application. You will have to manually clear the JFrame and (any invoked threads made by the user). using the

frame.dispose();
  1. Attach a window closed listener to the JFrame
  2. When triggered use the dispose method followed clearing swing elements and (killing threads that the user invokes).

    This will keep all the other windows open.
Sunny Patel
  • 535
  • 2
  • 5
  • 13