weak-references

Testing WeakReference

帅比萌擦擦* 提交于 2021-02-18 21:29:32
问题 What is the proper approach to testing a weak reference in Java? My initial idea is to do the following: public class WeakReferenceTest { public class Target{ private String value; public Target(String value){ this.value = value; } public String toString(){ return value; } } public class UsesWeakReference{ WeakReference<Target> reference; public UsesWeakReference(Target test){ reference = new WeakReference<Target>(test); } public String call(){ Target test = reference.get(); if(test != null){

Testing WeakReference

隐身守侯 提交于 2021-02-18 21:29:06
问题 What is the proper approach to testing a weak reference in Java? My initial idea is to do the following: public class WeakReferenceTest { public class Target{ private String value; public Target(String value){ this.value = value; } public String toString(){ return value; } } public class UsesWeakReference{ WeakReference<Target> reference; public UsesWeakReference(Target test){ reference = new WeakReference<Target>(test); } public String call(){ Target test = reference.get(); if(test != null){

Are WeakHashMap cleared during a full GC?

点点圈 提交于 2021-02-18 05:14:43
问题 I encountered some troubles with WeakHashMap. Consider this sample code: List<byte[]> list = new ArrayList<byte[]>(); Map<String, Calendar> map = new WeakHashMap<String, Calendar>(); String anObject = new String("string 1"); String anOtherObject = new String("string 2"); map.put(anObject, Calendar.getInstance()); map.put(anOtherObject, Calendar.getInstance()); // In order to test if the weakHashMap works, i remove the StrongReference in this object anObject = null; int i = 0; while (map.size(

Are WeakHashMap cleared during a full GC?

我的未来我决定 提交于 2021-02-18 05:14:19
问题 I encountered some troubles with WeakHashMap. Consider this sample code: List<byte[]> list = new ArrayList<byte[]>(); Map<String, Calendar> map = new WeakHashMap<String, Calendar>(); String anObject = new String("string 1"); String anOtherObject = new String("string 2"); map.put(anObject, Calendar.getInstance()); map.put(anOtherObject, Calendar.getInstance()); // In order to test if the weakHashMap works, i remove the StrongReference in this object anObject = null; int i = 0; while (map.size(

How should `ICommand.CanExecuteChanged` be implemented? [closed]

谁说我不能喝 提交于 2021-02-07 14:13:19
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 days ago . Improve this question Background While looking at Josh Smith's article about CommandGroup , I noticed that there are a number of comments on the Internet about how to implement ICommand.CanExecuteChanged . A similar question was posted here on StackOverflow, but I don't feel like

How should `ICommand.CanExecuteChanged` be implemented? [closed]

若如初见. 提交于 2021-02-07 14:04:17
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 days ago . Improve this question Background While looking at Josh Smith's article about CommandGroup , I noticed that there are a number of comments on the Internet about how to implement ICommand.CanExecuteChanged . A similar question was posted here on StackOverflow, but I don't feel like

Python: dereferencing weakproxy

随声附和 提交于 2021-02-06 15:48:08
问题 Is there any way to get the original object from a weakproxy pointed to it? eg is there the inverse to weakref.proxy() ? A simplified example(python2.7): import weakref class C(object): def __init__(self, other): self.other = weakref.proxy(other) class Other(object): pass others = [Other() for i in xrange(3)] my_list = [C(others[i % len(others)]) for i in xrange(10)] I need to get the list of unique other members from my_list . The way I prefer for such tasks is to use set : unique_others =

Property is unable to set to nil via weak reference [duplicate]

安稳与你 提交于 2021-01-29 03:56:58
问题 This question already has answers here : Weak references in Swift playground don't work as expected (5 answers) Closed 4 years ago . The following code defines Person and Apartment . Person instance may own an Apartment , Apartment may have a tenant( Person instance) class Person { let name: String init(name: String) { self.name = name } var apartment: Apartment? deinit { print("\(name) is being deinitialized") } } class Apartment { let unit: String init(unit: String) { self.unit = unit }

Why does the weak reference not get collected in this simple F# example?

落花浮王杯 提交于 2021-01-28 08:12:04
问题 open System let WeakReferenceExample() = let mutable obj = new Object(); let weak = new WeakReference(obj); GC.Collect(); Console.WriteLine("IsAlive: {0}\nobj <> null is {1}\n---", weak.IsAlive, obj <> null); obj <- null; GC.Collect(); Console.WriteLine("IsAlive: {0}", weak.IsAlive); WeakReferenceExample() Console.ReadKey() Translated from the Rx In Action book sample. The above when run gives the following output which is different than what I get when I compile it in C# and run it. IsAlive: