Java Links

Is there a way to find all references to an object (in Java)?

I have an object cache, and I would like to periodically check it to see if it will destroy it.

+3
source share
5 answers

You can take a look at the different types of links that Java provides: strong, soft, weak, and phantom .

In the cache, you usually wrap the link in WeakReference or SoftReference and allow you to collect the object if no stronger links remain.

+6
source

I have an object cache

Consider using WeakHashMapinstead HashMapto get all of them.

Here is an excerpt from the API:

Map hashtable . WeakHashMap , . , , , , , . , , , Map.

+5

, ​​. , JVM - , .

+2

, , .

( Java)?

. , , Java . Java, , .

, , , .

( , .) , , Java. SoftReference WeakReference , , , . , , , .

, - , , , WeakHashMap , WeakReference SoftReference.

EDIT. , . Java.

++, Java, , . , :

  • / ,
  • try { ... } finally , , ,
  • finalize , .

, () , . ( ), Java, ++, Java .

+1

. , , get() (, ), - . , , . 0 , .

, , , , .

0

Source: https://habr.com/ru/post/1726294/


All Articles