stevedlawrence opened a new pull request, #1539: URL: https://github.com/apache/daffodil/pull/1539
Currently we make use of ThreadLocal's as a way to create a per-thread instance of a class that is not thread safe. One major drawback with this approach is that values stored in ThreadLocals are never guaranteed to be garbage collected unless you call ThreadLocal.remove, even if the ThreadLocal has no more direct strong references--this is beacause the Thread iself has a table of strong references to ThreadLocal values. And few of our ThreadLocal uses ever call remove(). This can lead to pretty significant memory leaks in some edge cases. The reason we don't call remove is because in most cases we use ThreadLocals as an efficient pool, where each thread gets its own slot from the pool, and we don't necessarily know when a thread will no longer need that instance. But that means these instances can leak. To resolve this, this adds a new ThreadSafePool class, which is close to a drop-in replacement for ThreadLocal. The only real difference is this uses defines `allocate()` instead of `initialValue()`, and a `withInstance` function must be used to get an instance and automatically return it to the pool when withInstance ends. The ThreadSafePool implementation makes use of a ConcurrentLinkedQueue to support thread safe pool access--note that this does allow for an unbounded number of pool instances, but in practice each pool should never contain more than the number of threads. And unlike ThreadLocal, this does allow different threads to reuse the same instance, so it could potentially reduce memory. Additionally, if a pool no longer has strong reference then its values can be garbage collected, so there is no need to call `remove()` to avoid memory leaks. The main drawback is that the ConcurrentLinkedQueue could add some overhead since it does have to deal with potential thread contention, and it does require allocations for the backing list. But hopefully this is all relatively minor. DAFFODIL-3030 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
