soft-references

Equivalent to SoftReference in .net?

不打扰是莪最后的温柔 提交于 2019-12-05 09:01:10
I am familiar with WeakReference , but I am looking for a reference type that is cleared only when memory is low, not simply every time when the gc runs (just like Java's SoftReference ). I'm looking for a way to implement a memory-sensitive cache. Andrew Hare No there isn't an equivalent. Is there a particular reason why WeakReference won't do the job? Here is a similar question to yours: Why doesn't .NET have a SoftReference as well as a WeakReference, like Java? Maybe the ASP.NET Cache class ( System.Web.Caching.Cache ) might help achieve what you want? It automatically remove objects if

Soft (not: weak) references in C++ - Is it possible? Is there an implementation?

喜欢而已 提交于 2019-12-05 08:08:07
In C++ I'm using boost::shared_ptr and boost::weak_ptr to automatically delete objects that are no longer needed. I know these work with reference counting. In Java, memory is managed by a garbage collector, which consideres the built-in object references as strong , WeakReference as weak and SoftReference as something in between (may be collected by the GC, but may as well survive the GC), which is really handy for caching objects for some time, but throwing them away as soon as free memory is getting low. So now I'm back in C++ and I miss the comfort of having soft references. I wonder if

When to use Weak and Phantom references in Java

让人想犯罪 __ 提交于 2019-12-04 21:14:12
问题 I read many articles, but I don't understand - where do I need to use Weak and Phantom references in practice? Soft references - is a good choice for cache, as I understand. But weak and phantom, I don't know when to use. Please provide examples of real tasks where we need to use them. 回答1: You can use weak references for cache, simply like soft references as you said. What good are PhantomReferences? I'm only aware of two serious cases for them: first, they allow you to determine exactly

Soft reference LinkedHashMap in Java?

◇◆丶佛笑我妖孽 提交于 2019-12-04 13:24:03
问题 Is there softreference-based LinkedHashMap in Java? If no, has anyone got a snippet of code that I can probably reuse? I promise to reference it correctly. Thanks. 回答1: WeakHashMap doesn't preserve the insertion order. It thus cannot be considered as a direct replacement for LinkedHashMap. Moreover, the map entry is only released when the key is no longer reachable. Which may not be what you are looking for. If what you are looking for is a memory-friendly cache, here is a naive

Is there a way to FORCE weak and/or soft referenced objects to be GC'd in Java?

让人想犯罪 __ 提交于 2019-12-04 00:59:16
Here's my use case. We are trying to narrow down a potential memory leak in an application, and we are using a memory analysis tool to snapshot the heap so we can look for object instances and references. (In case it helps, we're using YourKit.) This application makes extensive use of dynamic and CGLIB proxies, which end up storing tons of references to classes and classloaders in WeakHashMaps. After our test case runs, we are expecting all hard references to object X and its classloader to be gone, but since there were many proxies involved in the test case in the end we have many weak/soft

Android: the GC doesn't respect SoftReferences?

隐身守侯 提交于 2019-12-03 13:23:44
问题 It seams that Dalvik's garbage collector doesn't respect SoftReferences and removes them as soon as possible, just like WeakReferences. I'm not 100% sure yet, but despite the fact that there is still ~3MB of free memory my SoftReferences get cleared after I see "GC freed bla-bla-bla bytes" in LogCat. Also, I saw a comment by Mark Murphy here: Except that it doesn't work on Android, at least in the 1.5 timeframe. I have no idea if the GC SoftReference bugs have been fixed. SoftReferences get

Can “soft references” exist in Python?

与世无争的帅哥 提交于 2019-12-03 12:31:45
In other languages (e.g. Java), object references can be Strong, Weak, Soft or Phantom ( http://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html ). In Python, references are Strong by default and the WeakRef module allows weak references. Is it possible to have "soft references" in Python? In my particular case, I have a cache of objects that are time-consuming to create. Sometimes there may be no references to a cached object, but I don't want to throw the cached object away if I don't have to (i.e. if memory is plentiful). Python doesn't natively offer any flavors of

Android: the GC doesn't respect SoftReferences?

跟風遠走 提交于 2019-12-03 02:54:16
It seams that Dalvik's garbage collector doesn't respect SoftReferences and removes them as soon as possible, just like WeakReferences. I'm not 100% sure yet, but despite the fact that there is still ~3MB of free memory my SoftReferences get cleared after I see "GC freed bla-bla-bla bytes" in LogCat. Also, I saw a comment by Mark Murphy here : Except that it doesn't work on Android, at least in the 1.5 timeframe. I have no idea if the GC SoftReference bugs have been fixed. SoftReferences get GC'd too soon with this bug. Is it true? Are SoftReferences not respected? How to workaround this?

What are the “practical consequences” of using soft references?

本小妞迷上赌 提交于 2019-11-30 14:20:20
问题 Per the documentation for Guava's MapMaker.softValues(): Warning: in most circumstances it is better to set a per-cache maximum size instead of using soft references. You should only use this method if you are well familiar with the practical consequences of soft references. I have an intermediate understanding of soft references - their behavior, uses, and their contract with garbage collection. However I'm wondering what these practical consequences are which the doc alludes to. Why exactly

What are the “practical consequences” of using soft references?

岁酱吖の 提交于 2019-11-30 09:42:55
Per the documentation for Guava's MapMaker.softValues() : Warning: in most circumstances it is better to set a per-cache maximum size instead of using soft references. You should only use this method if you are well familiar with the practical consequences of soft references. I have an intermediate understanding of soft references - their behavior, uses, and their contract with garbage collection. However I'm wondering what these practical consequences are which the doc alludes to. Why exactly is it better to use maximum size rather than soft references? Don't the algorithms and behavior of