In the following code...
public class MyObject {
public static Map<String, WeakReference<MyObject>> cache = new HashMap<>();
public static ReferenceQueue<MyObject> queue = new ReferenceQueue<>();
public MyObject(String... args) {
//define variables...
cache.put(args[0], new WeakReference<>(this, queue));
}
// getters and setters
}
If MyObject
s are continuously created, since cache
contains only weak references, will the garbage collector eventually remove items from cache
? Will those items become null
? Is there a better way of caching objects? My objective is to store discord messages so I don't have to fetch them again.