IT

언제 WeakHashMap 또는 WeakReference를 사용 하시겠습니까?

lottoking 2020. 6. 2. 21:15
반응형

언제 WeakHashMap 또는 WeakReference를 사용 하시겠습니까?


약한 참조의 사용은 구현을 본 적이없는 것이므로 사용 사례가 무엇인지, 구현이 어떻게 작동하는지 파악하려고합니다. 때 당신은을 사용하는 데 필요한 한 WeakHashMap또는 WeakReference어떻게 사용 되었는가?


강력한 참조의 한 가지 문제점은 캐싱, 특히 이미지와 같은 매우 큰 구조의 캐싱입니다. 내가 작업하는 웹 사이트 디자인 도구와 같이 사용자 제공 이미지로 작업해야하는 응용 프로그램이 있다고 가정합니다. 디스크에서 이미지를로드하는 것은 매우 비싸기 때문에 한 번에 두 개의 (잠재적으로 거대한) 이미지 사본을 메모리에 넣을 가능성을 피하기를 원합니다.

이미지 캐시는 반드시 필요하지 않을 때 이미지를 다시로드하지 못하도록하기 때문에 캐시에 항상 메모리에있는 모든 이미지에 대한 참조가 포함되어 있어야합니다. 그러나 일반적인 강력한 참조를 사용하면 해당 참조 자체로 이미지가 메모리에 남아있게되므로 이미지가 더 이상 메모리에 더 이상 필요하지 않은 시점을 판별하고 캐시에서 이미지를 제거하여 가비지 콜렉션에 적합하게해야합니다. 가비지 수집기의 동작을 복제하고 객체가 메모리에 있어야하는지 여부를 수동으로 결정해야합니다.

약한 참조 이해 , Ethan Nicholas


WeakReferenceSoftReference

명확하게 구분할 수있는 한 가지 차이점은 a WeakReference와 a 의 차이 SoftReference입니다.

기본적으로 a WeakReference참조 된 객체에 대한 하드 참조 가 없으면 JVM에 의해 간절히 GC-d가 됩니다 . 반면에 d 객체는 실제로 메모리를 회수해야 할 때까지 가비지 수집기에 의해 남겨지는 경향이 있습니다.SoftReference

WeakReferences 안에 들어 있는 캐시 쓸모가 없습니다 (a WeakHashMap에서는 약하게 참조되는 키입니다). SoftReferences사용 가능한 메모리로 증가 및 축소 할 수있는 캐시를 구현할 때 값을 감싸는 데 유용합니다.


WeakReferences와 WeakHashMaps 일반적인 용도 중 하나 는 객체에 속성을 추가하는 것입니다. 경우에 따라 일부 기능이나 데이터를 객체에 추가하려고하지만 하위 클래스 및 / 또는 컴포지션이 옵션이 아닌 경우 추가하려는 속성으로 확장하려는 객체를 연결하는 해시 맵을 만드는 것이 분명합니다. . 그런 다음 속성이 필요할 때마다지도에서 찾을 수 있습니다. 그러나 속성을 추가하는 객체가 많이 파괴되고 생성되는 경향이있는 경우 맵에서 많은 메모리를 차지하는 오래된 객체가 많이 생길 수 있습니다.

WeakHashMap대신에 객체 를 사용하는 경우 객체가 더 이상 프로그램의 나머지 부분에서 더 이상 사용되지 않는 즉시 맵을 떠나게되며 이는 바람직한 동작입니다.

나는 몇 가지 데이터를 추가하기 위해이 작업을 수행했다 java.awt.Component1.4.2과 1.5 사이의 JRE의 변화를 해결하기 위해, 내가 관심 INT (있었다 모든 구성 요소를 서브 클래 싱하여 문제를 해결 한 수 JButton, JFrame, JPanel...) 그러나 이것은 훨씬이었다 훨씬 적은 코드로 쉽게 수행 할 수 있습니다.


또 다른 유용한 경우 WeakHashMapWeakReferenceA는 리스너 레지스트리 구현 .

특정 이벤트를 듣고 싶은 것을 만들 때 일반적으로 리스너를 등록합니다.

manager.registerListener(myListenerImpl);

manager리스너를 리스너에 저장하는 경우 리스너 또는 리스너를 보유한 구성 요소를 사용할 수 없게되면 WeakReference레지스터 manager.removeListener(myListenerImpl)가 자동으로 제거되므로 레지스터를 제거 할 필요 가 없습니다.

물론 리스너를 수동으로 제거 할 수는 있지만 잊어 버렸거나 잊어 버린 경우 메모리 누수가 발생하지 않으며 리스너가 가비지 수집되지 않습니다.

WeakHashMap사진은 어디 에서 나오나요?

등록 된 리스너를 WeakReferences 로 저장하는 리스너 레지스트리 에는 이러한 참조를 저장하기위한 콜렉션이 필요합니다. WeakHashSet표준 Java 라이브러리 에는 구현 이 없지만 WeakHashMap후자를 사용하여 첫 번째 기능을 "구현"할 수 있습니다.

Set<ListenerType> listenerSet =
    Collections.newSetFromMap(new WeakHashMap<ListenerType, Boolean>());

이를 통해 listenerSet새 리스너를 등록하기 만하면 리스너를 세트에 추가하기 만하면됩니다. 명시 적으로 제거되지 않더라도 리스너가 더 이상 참조되지 않으면 JVM에 의해 자동으로 제거됩니다.


이 블로그 게시물은 Java : ID에서 동기화 두 클래스의 사용법을 보여줍니다 . 사용법은 다음과 같습니다.

private static IdMutexProvider MUTEX_PROVIDER = new IdMutexProvider();

public void performTask(String resourceId) {
    IdMutexProvider.Mutex mutext = MUTEX_PROVIDER.getMutex(resourceId);
    synchronized (mutext) {
        // look up the resource and do something with it
    }
}

IdMutextProvider는 동기화 할 id 기반 객체를 제공합니다. 요구 사항은 다음과 같습니다.

  • 동등한 ID를 동시에 사용하려면 동일한 객체에 대한 참조를 반환해야합니다.
  • 다른 ID에 대해 다른 객체를 반환해야합니다
  • 릴리스 메커니즘 없음 (개체가 제공자에게 리턴되지 않음)
  • 누출되지 않아야 함 (사용하지 않은 객체는 가비지 수집 대상 임)

이는 다음 유형의 내부 스토리지 맵을 사용하여 수행됩니다.

WeakHashMap<Mutex, WeakReference<Mutex>>

The object is both key and value. When nothing external to the map has a hard reference to the object, it can be garbage collected. Values in the map are stored with hard references, so the value must be wrapped in a WeakReference to prevent a memory leak. This last point is covered in the javadoc.


If you for example want to keep track of all objects created of a certain class. To still allow these objects to be garbage collected, you keep a list/map of weak references to the objects instead of the objects themselves.

Now if someone could explain phantom-references to me, I'd be happy...


As stated above, weak reference are held for as long as a strong reference exists.

An example usage would be to use WeakReference inside listeners, so that the listeners are no longer active once the main reference to their target object is gone. Note that this does not mean the WeakReference is removed from the listeners list, cleaning up is still required but can be performed, for example, at scheduled times. This has also the effect of preventing the object listened to from holding strong references and eventually be a source of memory bloat. Example: Swing GUI components refering a model having a longer lifecycle than the window.

While playing with listeners as described above we rapidly realised that objects get collected "immediately" from a user's point of view.


One real world use I had for WeakReferences is if you have a single, very large object that's rarely used. You don't want to keep it in memory when it's not needed; but, if another thread needs the same object, you don't want two of them in memory either. You can keep a weak reference to the object somewhere, and hard references in the methods that use it; when the methods both finish, the object will be collected.


I did a google code search for "new WeakHashMap()".

I got a bunch of matches from the GNU classpath project and

  1. Apache xbean Project : WeakHashMapEditor.java
  2. Apache Lucene project : CachingWrapperFilter.java

you can use weakhashmap to implement a resource-free caching for expansive object creation.

but note that it is not desireable to have mutable objects. i used it to cache query results (which take about 400 ms to execute) to a text-search engine, which is rarely updated.

참고URL : https://stackoverflow.com/questions/154724/when-would-you-use-a-weakhashmap-or-a-weakreference

반응형