weak-references

Any weak interning collections (for immutable objects)

别说谁变了你拦得住时间么 提交于 2019-11-30 13:34:11
问题 In some situations involving immutable objects, it will be possible for many distinct objects to come into existence which are semantically identical. A simple example would be reading many lines of text from a file into strings. From the program's perspective, the fact that two lines have the same sequence of characters would be "coincidence", but from the programmer's perspective a large amount of duplication may be expected. If many string instances are identical, changing the references

What are the benefits to using WeakReferences?

若如初见. 提交于 2019-11-30 11:37:44
问题 I have some memory leaks in my app. They all originate around a specific view cluster that I have spent a loooot of time tweaking and trying to reduce a much contextual passing as possible. This leads me to believe that bitmaps used in the cluster are the issue. So I was thinking to use WeakReferences for all references to the bitmaps used by the views. I have never used a WeakReference and am not sure if this is a good application. Can any body provide an helpful pointers or tips? 回答1: So I

Collections of zeroing weak references under ARC

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 10:27:22
问题 How can I get an array of zeroing weak references under ARC? I don't want the array to retain the objects. And I'd like the array elements either to remove themselves when they're deallocated, or set those entries to nil. Similarly, how can I do that with a dictionary? I don't want the dictionary to retain the values. And again, I'd like the dictionary elements either to remove themselves when the values are deallocated, or set the values to nil. (I need to retain the keys, which are the

Android Handler changing WeakReference

心不动则不痛 提交于 2019-11-30 09:48:06
My static handler has a WeakReference to my Activity (this is to prevent the well documented memory leak issue). I post a long delayed message and I want this message delivered to my activity (which should be in the foreground). My concern is that on orientation change, my activity is destroyed and the handler has a reference to the old activity which should have been destroyed. In order to get around this in my onCreate for the activity I do this. if(mHandler == null) mHandler = new LoginHandler(this); else { mHandler.setTarget(this); } And my handler is declared as a static global variable:

ThreadLocal Resource Leak and WeakReference

泪湿孤枕 提交于 2019-11-30 08:38:01
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, in this code snippet where would the WeakReference be introduced? static class DateTimeFormatter { private static final ThreadLocal<SimpleDateFormat> DATE_PARSER_THREAD_LOCAL = new ThreadLocal<SimpleDateFormat>() { protected SimpleDateFormat initialValue() {

Any weak interning collections (for immutable objects)

纵然是瞬间 提交于 2019-11-30 07:23:59
In some situations involving immutable objects, it will be possible for many distinct objects to come into existence which are semantically identical. A simple example would be reading many lines of text from a file into strings. From the program's perspective, the fact that two lines have the same sequence of characters would be "coincidence", but from the programmer's perspective a large amount of duplication may be expected. If many string instances are identical, changing the references to those distinct instances into references to a single instance will save memory, and will also

Swift Closures - Capturing self as weak

▼魔方 西西 提交于 2019-11-30 05:02:51
I am trying to resolve a closure based strong reference cycle in Swift. In the code below, object is retained by the owning view controller. ProgressHUD is a UIView that's also retained by the owning view controller. ProgressHUD is leaked every time the completion handler is called. When using the new closure capture feature, declaring self as weak or unowned does not resolve the memory leak. object.setCompletionHandler { [weak self] (error) -> Void in if(!error){ self?.tableView.reloadData() } self?.progressHUD?.hide(false) } However, if I declare a weak var for self outside of the closure,

How to avoid memory leaks in callback?

╄→гoц情女王★ 提交于 2019-11-30 04:59:07
Effective Java says: A third common source of memory leaks is listeners and other callbacks. If you implement an API where clients register callbacks but don’t deregister them explicitly, they will accumulate unless you take some action. The best way to ensure that callbacks are garbage collected promptly is to store only weak references to them, for instance, by storing them only as keys in a WeakHashMap. I am a beginner in Java. Could somebody teach me how to create weak references in callbacks and tell me how they solve the memory leak problems? Thanks. Read this article The key take aways

Weak object in an NSDictionary?

こ雲淡風輕ζ 提交于 2019-11-30 04:47:26
I would like to store a zeroing weak reference to an object in a NSDictionary . This is for a reference to a parent NSDictionary , so I can crawl back up a large structure without searching. I can not use __weak here; even if my local reference is weak, the NSDictionary will store a strong reference to the object that was weakly referenced. And, of course, NSDictionary can't have nil objects. I'm on iOS, not Mac, so NSHashTable isn't available. And I only want one object to be weak; the rest should still be strong. (I'm going to post my answer, so I have something to mark as accepted if there

Compacting a WeakReference Dictionary

故事扮演 提交于 2019-11-30 03:55:34
I've got a class Foo with a property Id . My goal is that there are no two instances of Foo with the same Id at the same time. So I created a factory method CreateFoo which uses a cache in order to return the same instance for the same Id . static Foo CreateFoo(int id) { Foo foo; if (!cache.TryGetValue(id, out foo)) { foo = new Foo(id); foo.Initialize(...); cache.Put(id, foo); } return foo; } The cache is implemented as a Dictionary<TKey,WeakReference>, based on @JaredPar 's Building a WeakReference Hashtable : class WeakDictionary<TKey, TValue> where TValue : class { private readonly