effectiveJava(5) eliminates expired object references

  Expired References: References that will never be released again.

  Common sources of memory leaks:

    1. When a class manages its own memory, programmers should be alert to memory leaks. Once the element is freed, any object references contained in the element should be emptied.

    Second, the cache. Once an object reference is placed in the cache, it is easily forgotten, leaving the object in the cache long after it is no longer useful. When implementing a cache, whenever a reference to an item's key exists outside of the cache

  If the item makes sense, then the cache can be represented by a WeakHashMap; when the items in the cache expire, they will be automatically deleted. WeakHashMap is useful only if the lifetime of the desired cache item is determined by an external reference to the key and not by the value

    3. Listeners and other callbacks.

 

    Since memory leaks usually do not manifest themselves as obvious failures, they can exist in a system for many years. Generally, memory leaks can only be found by careful inspection of the code, or with the help of Heap analysis tools. Therefore, if it is possible to know the memory leak before it happens

How to predict such problems and prevent them from happening is the best.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325306198&siteId=291194637