weak-references

WeakReference and memory leaks

不想你离开。 提交于 2019-12-23 01:35:16
问题 I'm profiling my application using VisualVM and I see that the heap size increased by about 7MB in about 3 days. When I use memory sampler, I also see that java.lang.ref.WeakReference is in the top five for the instances number. The number of WeakReference is increasing and GC has almost no effect. Any idea? 回答1: You do not have a memory leak. Java's GC only runs when the heap is full (actually is a bit more complicated since the heap itself is divided into generations, but anyway), so unless

Can a conforming C# compiler optimize away a local (but unused) variable if it is the only strong reference to an object?

孤人 提交于 2019-12-22 03:37:10
问题 See also these related resources: Does the .NET garbage collector perform predictive analysis of code? (on Stack Overflow) WP7: When does GC Consider a Local Variable as Garbage (blog article on MSDN) In other words: Can an object referenced by a local variable be reclaimed before the variable goes out of scope (eg. because the variable is assigned, but then not used again), or is that object guaranteed to be ineligible for garbage collection until the variable goes out of scope? Let me

WeakReference understanding

老子叫甜甜 提交于 2019-12-22 01:45:09
问题 I want to create the dictionary of all the ViewModels. public static Dictionary<string, WeakReference> vmCollection = new Dictionary<string, WeakReference>(); Adding it like this vmCollection.Add(name, new WeakReference(viewModel)); And calling the required method like this.. ((vmCollection[viewModel].Target) as BaseViewModel).NewMessage(message); Do I need maintain it as a WeakReference ? What could be the consequences if I don't maintain it as a WeakReference . 回答1: The only consequence of

When and why would I want to declare a local variable as __weak using ARC?

蹲街弑〆低调 提交于 2019-12-21 17:24:41
问题 Mike Ash has written this introduction to ARC where he introduces something like: __weak Foo *_weakFoo = [object foo]; Why would I want to do that for a local, temporary variable? __weak is a zeroing reference which will set the _weakFoo pointer automatically to nil as soon as the referenced object gets deallocated. Also, __weak is only available in iOS >= 5. When would I run into trouble when I simply do this?: Foo *_weakFoo = [object foo]; This is always expected to return an object or nil.

Would a “circular” reference be treated as “reachability” for a WeakMap?

一世执手 提交于 2019-12-21 09:29:10
问题 function f() { const w = new WeakMap(); const o = {}; w.set(o, { v: o }); return w; } const weakMap = f(); For the given code, would the only weakMap item considered as reachable or not? Hence, will it be garbage collected or not? PS: This question is asked from the perspective of the specification, not particular implementations. 回答1: Quoting WeakMap Objects section, If an object that is being used as the key of a WeakMap key/value pair is only reachable by following a chain of references

Would a “circular” reference be treated as “reachability” for a WeakMap?

拈花ヽ惹草 提交于 2019-12-21 09:28:02
问题 function f() { const w = new WeakMap(); const o = {}; w.set(o, { v: o }); return w; } const weakMap = f(); For the given code, would the only weakMap item considered as reachable or not? Hence, will it be garbage collected or not? PS: This question is asked from the perspective of the specification, not particular implementations. 回答1: Quoting WeakMap Objects section, If an object that is being used as the key of a WeakMap key/value pair is only reachable by following a chain of references

Nested blocks and references to self

℡╲_俬逩灬. 提交于 2019-12-21 04:50:21
问题 I have a block wherein I use self so I declare a weak reference to self: __weak MyClass *weakSelf = self; Now my questions: I get an error where I define weakSelf and I don't understand what this should mean.: weak attribute can not be specified on an automatic variable Inside my block I pass weakSelf to another block and I am not sure if I now have to do the same thing again like so: __weak MyClass *weakWeakSelf = weakSelf; And then pass weakWeakSelf to that block? 回答1: This is most likely

Is self captured within a nested function

人盡茶涼 提交于 2019-12-20 11:38:11
问题 With closures I usually append [weak self] onto my capture list and then do a null check on self: func myInstanceMethod() { let myClosure = { [weak self] (result : Bool) in if let this = self { this.anotherInstanceMethod() } } functionExpectingClosure(myClosure) } How do I perform the null check on self if I'm using a nested function in lieu of a closure (or is the check even necessary...or is it even good practice to use a nested function like this) i.e. func myInstanceMethod() { func

Is self captured within a nested function

你说的曾经没有我的故事 提交于 2019-12-20 11:38:02
问题 With closures I usually append [weak self] onto my capture list and then do a null check on self: func myInstanceMethod() { let myClosure = { [weak self] (result : Bool) in if let this = self { this.anotherInstanceMethod() } } functionExpectingClosure(myClosure) } How do I perform the null check on self if I'm using a nested function in lieu of a closure (or is the check even necessary...or is it even good practice to use a nested function like this) i.e. func myInstanceMethod() { func

How to implement a canonicalizing mapping in Java?

心不动则不痛 提交于 2019-12-20 09:40:37
问题 I am currently rolling my own little ORM, and find myself faced with the task of creating a canonicalizing mapping in order to prevent loading the same entity from the database more than once. My current approach is to use a HashMap<Object, WeakReference<Object>> . The key is the primary key of the mapped database-entity (an ArrayList<Object> if it is a composite key), and the values are WeakReference<Object> . My main problem is how to clean the map up? When an object is not used any more,