Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Ways to improve the efficiency of JVM memory use through soft references and weak references

Shulou Source: shulou.com Published: 2022-06-03 07:33:03 09月18日 Update

Reference types: new, virtual reference, soft reference, weak reference

1. Soft reference (SoftReference)

If an object has only soft references and the current virtual machine stack has enough memory, the garbage collector will not reclaim it; instead, it will reclaim the objects to which these soft references point.

two。 The difference between WeakReference and soft reference

Once the garbage collector finds that there are only weak references on a block of memory, it will reclaim the memory regardless of whether the current memory space is sufficient or not.

two。 Improve memory performance through soft / weak references

1) when there is enough memory, there is no need to use soft references.

2) improve efficiency with cache

First go to the cache to query, query to return; if the query is not available, go to the database to query, store the acquired content in the cache, and return.

Eg: soft reference / weak reference caching

3) Why not use strong references for caching

Strong references cannot be recycled

Tags: Memory cache query garbage object collector efficiency no content only stack performance pointing data database space type storage method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS Shulou Information Redmi Docker