weak-references

When should weak references be used?

你。 提交于 2019-11-28 06:29:06
I recently came across a piece of Java code with WeakReferences - I had never seen them deployed although I'd come across them when they were introduced. Is this something that should be routinely used or only when one runs into memory problems? If the latter, can they be easily retrofitted or does the code need serious refactoring? Can the average Java (or C#) programmer generally ignore them? EDIT Can any damage be done by over-enthusiastic use of WRs? Weak references are all about garbage collection. A standard object will not "disappear" until all references to it are severed, this means

Why do we need weak reference in java

戏子无情 提交于 2019-11-28 05:57:04
I understand that weak references are at the mercy of the garbage collector, and we cannot guarantee that the weak reference will exist. I could not see a need to have weak reference, but sure there should be a reason. Why do we need weak reference in java? What are the practical (some) uses of weak reference in java? If you can share how you used in your project it will be great! It's actually quite often a bad idea to use weak hashmaps. For one it's easy to get wrong, but even worse it's usually used to implement some kind of cache. What this does mean is the following: Your program runs

Weak references - how useful are they?

﹥>﹥吖頭↗ 提交于 2019-11-28 04:40:43
So I've been mulling over some automatic memory management ideas lately - specifically, I've been looking at implementing a memory manager based on reference counting. Of course, everyone knows that circular references kill naive reference counting. The solution: weak references. Personally, I hate using weak references in this way (there are other more intuitive ways of dealing with this, via cycle detection), but it got me thinking: where else could a weak reference be useful? I figure that there must be some reason they exist, especially in languages with tracing garbage collection, which

WeakReference/AsyncTask pattern in android

徘徊边缘 提交于 2019-11-28 03:32:57
I have a question regarding this simple frequently occurring situation in android . We have a main activity , we invoke an AsyncTask alongwith the reference of the mainactivity , so that that the AsyncTask can update the views on the MainActivity. I will break down the event into steps MainActivity creates an AyncTask , passes its reference to it . AysncTask , starts it's work , downloading ten files for example The user changed the orientation of the device. This results in an orphan pointer in the AsyncTask When the AsyncTask completes , and tries to access the activity to update the status

Generic typeof for weak self references

久未见 提交于 2019-11-28 03:09:04
I am trying to figure out a way to use typeof to create a weak reference to self for use in blocks to avoid retain cycles. When I first read about this it seems that the convention was to use __block typeof(self) bself = self; , which compiles but using __block to avoid retain cycles doesn't work anymore and __weak should be used instead. However __weak typeof(self) bself = self; results in an error: The type 'typeof (self)' (aka 'TUAccountsViewController *const __strong') already has retainment attributes set on it Is there a way to use typeof or another call to generically create a weak

How can I use a std::map with std::weak_ptr as key?

▼魔方 西西 提交于 2019-11-28 03:01:18
问题 How can I use std::weak_ptr as key for a std::map as shown in the following code? #include <map> #include <memory> int main() { std::map< std::weak_ptr<int>, bool > myMap; std::shared_ptr<int> sharedptr(new int(5)); std::weak_ptr<int> weakptr = sharedptr; myMap[weakptr] = true; return 0; } The above program doesn't build and trying to compile it gives many error messages such as: 1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2784: 'bool std:

ConcurrentHashMap with weak keys and identity hash?

江枫思渺然 提交于 2019-11-28 02:36:53
问题 How do I get a ConcurrentHashMap with weak keys and identity hashes in Java? I think Google Guava Collections can give such a thing, but can I get it from the standard library? What other options do I have? 回答1: I think Google Guava Collections can give such a thing, but can I get it from the standard library? The short answer to that is No. Java SE does not implement this particular combination. You could instantiate a java.util.concurrent.ConcurrentHashMap with WeakReference keys, and do

WeakReference string didn't garbage collected? How?

ぐ巨炮叔叔 提交于 2019-11-28 02:04:15
问题 I'm reading about WeakReference in wikipedia and I saw this code public class ReferenceTest { public static void main(String[] args) throws InterruptedException { WeakReference r = new WeakReference(new String("I'm here")); WeakReference sr = new WeakReference("I'm here"); System.out.println("before gc: r=" + r.get() + ", static=" + sr.get()); System.gc(); Thread.sleep(100); // only r.get() becomes null System.out.println("after gc: r=" + r.get() + ", static=" + sr.get()); } } When It runs

Does WeakReference make a good cache?

放肆的年华 提交于 2019-11-28 00:46:43
i have a cache that uses WeakReferences to the cached objects to make them automatically removed from the cache in case of memory pressure. My problem is that the cached objects are collected very soon after they have been stored in the cache. The cache runs in a 64-Bit application and in spite of the case that more than 4gig of memory are still available, all the cached objects are collected (they usually are stored in the G2-heap at that moment). There are no garbage collection induced manually as the process explorer shows. What methods can i apply to make the objects live a litte longer?

How to delete every reference of an object in Python?

爱⌒轻易说出口 提交于 2019-11-28 00:44:01
问题 Supose you have something like: x = "something" b = x l = [b] How can you delete the object only having one reference, say x? del x won't do the trick; the object is still reachable from b, for example. 回答1: No no no. Python has a garbage collector that has very strong territory issues - it won't mess with you creating objects, you don't mess with it deleting objects. Simply put, it can't be done, and for a good reason. If, for instance, your need comes from cases of, say, caching algorithms