java - Does an ExecutorService get garbage collected when out of scope? -


I am asking this question because I am creating many executable services and I already have a memory leak There is a need to investigate, I think a recent change in the following code has really worsened, so I am trying to confirm what is happening:

 < Code> @ FunctionalInterface Public Interface BaseConsumer Consumer & Lt; Path & gt; {@ Override default accepts zero (last path path) {string name = path.jet filename (). ToString (); Executor Services = Exporter. NaveSinglethread Exclorator (runnable -> gt; thread thread = new thread (runnable, "document id =" + name); thread .SetDemon (true); return thread;}); Future & lt ;? & Gt; Future = service.submit (() -> gt; baseAccept (path); return tap;}); Try {future.get (); } Grip (Prerupted Pre-Probe) {Thread.currentThread (). Interrupt (); } Hold (Execution Exception) {new runtime exception (ex); }} Zero base acquisition (last path path) throws exceptions; }   

Then this consumer & lt; Path & gt; The second thread pool (usually) calls to N = 2 threads, I'm not sure it's relevant.

The question is whether the Execution Service Service exits the scope of and once gets collected garbage the base consumer # Accepted is finished?

Does the Execution Service exits service scope and accept BaseConsumer once () is over?

Yes

In fact, the connected thread pool should also be collected in the trash ... in the end.

Executable service which is built by executor. An example of NewSingleThreadExecutor () is FinalizableDelegatedExecutorService . In that class there is final () method, which calls the shutdown () wrapped ExecutorService object provided all outstanding functions are actually When finished, the service object closes its thread pool.

(AAAI, this is not specified.) But this has been implemented according to the source code after Java 6. )


Adding {service.shutdown () to one end; } Future.get () Help in retrieving resources in the effort-grabbing around? (Not necessarily to collect garbage service objects).

Yes it does calling shutdown () is due to the release of the outstanding tasks as well as the release is done, although this process begins immediately, though If you leave it to the garbage collector then it does not start until it is finally told.

Now if the resources were only "simple" Java objects, then it would not make any difference. But in this case, the resource you are retrieving is a Java thread, and it has Associate operating system resources (for example, a native thread), and is a non-trivial part of the out-of-the-hand memory. So maybe this is meaningful to do so.

But if you want to customize it, then you should build long term executable service objects, and it should share in many "consumer" examples.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -