mirror of
https://github.com/ysoftdevs/odc-analyzer.git
synced 2026-01-13 23:33:53 +01:00
25 lines
724 B
Scala
25 lines
724 B
Scala
package com.ysoft.memory
|
|
|
|
import java.lang.ref.{WeakReference => JWeakReference}
|
|
import java.util
|
|
|
|
class ObjectPool{
|
|
|
|
private val objects = new util.WeakHashMap[Any, JWeakReference[Any]]()//new MapMaker().concurrencyLevel(1).weakKeys().weakValues().makeMap[Any, Any]()
|
|
|
|
def apply[T](obj: T): T = synchronized{
|
|
// The code is intentionally low-level for performance reasons. No Option[_] used for performance reasons, no scala.ref._ wrapper is used for memory overhead reasons.
|
|
val res = objects.get(obj) match {
|
|
case null => null
|
|
case weakObj => weakObj.get()
|
|
}
|
|
if(res == null){
|
|
objects.put(obj, new JWeakReference[Any](obj))
|
|
obj
|
|
}else{
|
|
res.asInstanceOf[T]
|
|
}
|
|
}
|
|
|
|
}
|