Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private val cache = new java.util.concurrent.ConcurrentHashMap[T, java.lang.Long]()
- var currentThreshold = 1L
- {
- val sizeMonitor = new Thread(new Runnable {
- override def run() {
- while (true) {
- Thread.sleep(1000)
- if (MemoryPressureMonitor.haveMemoryPressure(10)) {
- val initialSize = cache.size()
- val entryIterator = cache.entrySet().iterator()
- var foundMinimum = Long.MaxValue
- var removedEnough = false
- while (!removedEnough) {
- while (entryIterator.hasNext) {
- val nextValue = entryIterator.next()
- if (nextValue.getValue <= currentThreshold)
- entryIterator.remove()
- else
- if (nextValue.getValue < foundMinimum)
- foundMinimum = nextValue.getValue
- }
- removedEnough = cache.size().toDouble / initialSize <= 0.9
- if (!removedEnough)
- currentThreshold = foundMinimum
- }
- }
- }
- }
- })
- sizeMonitor.setName("ObjectCounter-size-monitor")
- sizeMonitor.setDaemon(true)
- sizeMonitor.start()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement