Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KMeansClustering cause thread leak #4766

Closed
gogo111007 opened this issue Mar 7, 2018 · 7 comments
Closed

KMeansClustering cause thread leak #4766

gogo111007 opened this issue Mar 7, 2018 · 7 comments
Labels
Bug Bugs and problems

Comments

@gogo111007
Copy link

gogo111007 commented Mar 7, 2018

Issue Description

i use KMeansClustering like KMeansTest.java
every client request, i get new kmeans object by KMeansClustering.setup()
but there is no way to destroy threads allocate by KMeansClustering
after thousands or more request, too many threads cause crash

Version Information

  • deeplearning4j ver newest
  • windows 7 and centos 6.x
@jentfoo
Copy link

jentfoo commented Mar 9, 2018

I recently stumbled across this too. The issue is in BaseClusteringAlgorithm. It constructs an ExecutorService but provides no way to free it.

https://github.com/deeplearning4j/deeplearning4j/blob/master/deeplearning4j-nearestneighbors-parent/nearestneighbor-core/src/main/java/org/deeplearning4j/clustering/algorithm/BaseClusteringAlgorithm.java#L70

I would be happy to help provide a PR to solve this if the maintainers give me some direction as to how this should be resolved.

the jdk attempts to implement a finalizer in ThreadPoolExecutor to shutdown the pool if it is garbage collected, however this does not work correctly if threads are started since they also hold a reference to their parent pool (and thus the finalizer is never invoked even if BaseClusteringAlgorithm is garbage collected).

If you wanted to rely on garbage collection I see two possible options:

  1. Selfish plug of using a better thread pool. Thread pools (PriorityScheduler for example) from my threadly project do not suffer from this issue. They can be collected normally, or you can use CentralThreadlyPool as a way to avoid needing to manage the lifecycle at all.
  2. You can implement a finalizer in BaseClusteringAlgorithm which invokes a shutdown to it's retained pool.

I personally don't like depending on finalizers. My personal preference (though potentially unpopular) would be to have people supply an Executor (Executor is way better than ExecutorService if you can), and then put the thread pool management on the user.

If you are willing to depend on threadly, if an executor is not provided using a threadly executor can provide the ability to fall back to garbage collection for releasing the created threads.

@jentfoo
Copy link

jentfoo commented Mar 9, 2018

I guess after looking at ExecutorServiceProvider maybe the original intention was to set instance (which currently goes unused) and reuse that instance.

@saudet saudet added the Bug Bugs and problems label Mar 10, 2018
@saudet
Copy link
Contributor

saudet commented Mar 10, 2018

@jentfoo Thanks for the input! Using threadly looks like a good technical solution, but I think the Mozilla license is a problem, @agibsonccc ?

@agibsonccc
Copy link
Contributor

We follow apache legal guidelines: https://www.apache.org/legal/resolved.html It doesn't appear to conflict with the EPL, I'd be ok with this in that case.

@jentfoo
Copy link

jentfoo commented Mar 12, 2018

@agibsonccc and @saudet Thanks for taking a look. One of the primary reasons for choosing MPL 2.0 was its compatibility with other licenses. It seemed like a license that would be a nice fit for a library like threadly. I want anyone who wants to be able to use threadly, my only reason for not using apache is I want development on the project to remain open source and encourage modifications needed to be merged to the upstream project if possible.

So in short, I don't see any reason it should be a problem either. I was going to provide a PR for #4781 as well, so if you wanted I could include adding threadly and any other concurrency cleanup things I might notice (all open for discussion of course, but to give us a starting point for that discussion).

@saudet
Copy link
Contributor

saudet commented Mar 13, 2018 via email

@lock
Copy link

lock bot commented Sep 23, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked and limited conversation to collaborators Sep 23, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Bugs and problems
Projects
None yet
Development

No branches or pull requests

4 participants