weakreference

Why doesn't .NET have a SoftReference as well as a WeakReference, like Java?

I really love WeakReference's. But I wish there was a way to tell the CLR how much (say, on a scale of 1 to 5) how weak you consider the reference to be. That would be brilliant. Java has SoftReference, WeakReference and I believe also a third type called a "phantom reference". That's 3 levels right there which the GC has a different be...

Lambda Expression cause weakreference's target cannot be GC?

namespace Test { class Test { delegate void HandleMessage(string message); public void handleMessage(string message){} static void Main(string[] args) { HandleMessage listener1 = new Test().handleMessage; WeakReference w1 = new WeakReference(listener1); HandleMessage listener2 = (message) => { ...

Why doesn't the weakref work on this bound method?

I have a project where i'm trying to use weakrefs with callbacks, and I don't understand what I'm doing wrong. I have created simplified test that shows the exact behavior i'm confused with. Why is it that in this test test_a works as expected, but the weakref for self.MyCallbackB disappears between the class initialization and calling...

Equivalent to SoftReference in .net?

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. ...

Does this Caching function work how I think it does?

I've tentatively written this method: public static Func<T> WeakCacheFor<T>( Func<T> provider ) where T: class { var cache = new WeakReference(null); return () => { var x = (T)cache.Target; if( x == null ) { x = provider(); cache.Target = x; } return x; }; } S...

Does WeakReference make a good cache?

Hi, 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 mem...

ThreadLocal Resource Leak and WeakReference

My limited understanding of ThreadLocal is that it has resource leak issues. I gather this problem can be remedied through proper use of WeakReferences with ThreadLocal (although I may have misunderstood this point.) I would simply like a pattern or example for correctly using ThreadLocal with WeakReference, if one exists. For instance,...

Python weakref callbacks and __del__ execution order

In Python, is there a way to call a function after an object is finalized? I thought the callback in a weakref would do it, but it appears a weakref's callback is called once the object is garbage collected, but before the objects __del__ method is called. This seems contrary to the notes on weakrefs and garbace collection in the Pytho...

Does WeakReference work with String?

In .NET 3.5, Does WeakReference work with String or shall I wrap it in a small "class" to make it work with it? ...

Does WeakReference have redundant properties?

WeakReference implementation in .NET has an IsAlive Property. 1) Are there any performance/behavior differences between using the IsAlive property or testing whether the Target property is not null? 2) IsAlive is a redundant property? Thanks. ...

How-to implement a custom debugger visualiser in VS2008 on an array using a WeakReference?

I would like to implement a custom debugger visualiser in vs2008 for a typical array as the standard one does not display the data as I would like it. However Visual Studio prevents doing this for arrays for security reasons. I seem to remember though reading about using a WeakReference as a wrapper object to get around this limitation....

Memory leak of java.lang.ref.WeakReference objects inside JDK classes

The following simple code reproduces the growth of java.lang.ref.WeakReference objects in the heap: public static void main(String[] args) throws Exception { while (true) { java.util.logging.Logger.getAnonymousLogger(); Thread.sleep(1); } } Here is the output of jmap command within a few seconds interval: user@t1007:~> jmap -d64 -hi...

Is there any class for weak References on iPhoneOS ==> none

My APP received a big data that was used about twice times. So, I think It`ll cause a memory problem. In this case I use the WeakReference class on java. But, I couldn`t find any class for weak references like java. Is there any class for weak refereces on iPhoneOS? Is this the only way in this case, that using didReceiveMemoryWarning...

WeakHashMap: how to know the value that was associated with a removed entry

I've something like this private Map<MyObj1, MyObj2> map = new WeakHashMap<MyObj1, MyObj2>(); ... somewhere in the code ... MyObj1 myObj1 = new MyObj1(); map.put(myObj1, new MyObj2(); ... myObj1 = null; ... somewhere else in a thread ... (I would like to pass to a checkThis(MyObj2) method the Value associated with the entry that...

WeakReference in Java inside Thread

Hi, i am trying to create an background thread that updates a Runnable at a given interval. It should also not prevent the "parent" from beeing garbage collected. My problem is as follows. My WeakReference seems to act as a "strong" Reference, It doesn't stop my thread form accessing the runnable that i am supposed to have made availab...

Inherited WeakReference throwing ReflectionTypeLoadException in Silverlight

Hi all. I'm trying to use a type-safe WeakReference in my Silverlight app. I'm following the recipe on this site: http://ondevelopment.blogspot.com/2008/01/generic-weak-reference.html only using the System.WeakReference and omitting the stuff that references Serialization. It's throwing a ReflectionTypeLoadException when I try to run i...

How to use WeakReference in Java and Android development?

I have been a java developer 2 years. But I have never wrote a WeakReference in my code. How to use WeakReference to make my application more efficiency especially in Android application. ...

Python: Thread safe dictionary with short lived keys, is this correct?

import threading import weakref _mainlock = threading.RLock() _job_locks = weakref.WeakValueDictionary() def do_thing(job_id): _mainlock.acquire() #Dictionary modification lock acquire _job_locks.setdefault(job_id, threading.RLock()) #Possibly modifies the dictionary _mainlock.release() _job_locks[job_id].acquire() tr...

Silverlight WP7 messaging, should I use a WeakReference and if so how?

I'm creating a simple messaging system for a windows phone silverlight app. The idea is various xaml pages & other objects will subscribe to a messaging object, passing in the type of message they want to recieve/handle and an Action<> Delegate as the handler. When an action happens a message (with payload) will be sent to the correct ...

Will GC collect an object referred to by a SoftReference and a WeakReference?

I have a cache built from a Map to SoftReferences. When they are added they get put into another queue to be lazily compressed down via gzip or some such. My idea is this: I want to have WeakReferences to the objects in the compress queue, so that when the compressor task gets to the object, if it is already gone we needn't bother compre...