weak-references

Should adapters in Android be static inner classes or non-static inner classes

冷暖自知 提交于 2019-12-10 02:02:19
问题 I have a ListView in an Activity and I am setting a custom adapter to the ListView. Should my adapter class be: private static class MyAdapter extends ArrayAdapter or private class MyAdapter extends ArrayAdapter I guess it should make no difference as long as the adapter is enclosed within the activity reference but wanted to confirm that. 回答1: Holding onto the context is fine from within an adapter if you are careful about how you use the adapter. Adapters are usually tied to the lifecycle

Objective-C: Weak attritube don't work as expected [duplicate]

牧云@^-^@ 提交于 2019-12-09 15:14:00
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Why do weak NSString properties not get released in iOS? I'm a newbie to Objective C and I've got some questions that I cannot answer myself. I have a block of code for testing __weak variable (I'm using ARC, of course): NSString *myString = [[NSString alloc] initWithFormat:@"John"]; NSString * __weak weakString = myString; myString = nil; //<-- release the NSString object NSLog(@"string: %@", weakString); The

WeakHashMap and strongly referenced value

北战南征 提交于 2019-12-09 05:39:56
问题 Javadocs says "When a key has been discarded its entry is effectively removed from the map". But unless there is another thread that occasionally removes such Map.Entry entries, won't the value objects be strongly referenced by the map? But since there is no such thread running, only the get method invocations can remove such entries - one at a time. I almost always use WeakHashMap<K, WeakReference<V>> for that reason. Why would they not have made that the default behavior - values as weak

WeakReference to String and String constants

和自甴很熟 提交于 2019-12-09 03:13:47
问题 I have come across this example from wikipedia regarding weak reference: import java.lang.ref.WeakReference; 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() +

Test a weak reference before using it java

≯℡__Kan透↙ 提交于 2019-12-09 02:12:17
问题 In a multithreaded Android project, I'm seeing code like this: final WeakReference<MyClass> myClassObjectWeakRef = new WeakReference<MyClass>(aMyClassObject); ...then somewhere else: if (myClassObjectWeakRef.get() != null) { myClassObjectWeakRef.get().someMethod(); } I'm pretty sure there is a possible race condition between the check and the use of the reference, if the last strong reference to the object is released between the two in another thread, but I can't find any documentation or

Weak property in a Swift protocol may only be a class or class-bound protocol type

前提是你 提交于 2019-12-09 00:38:14
问题 I would like to define a protocol which is used in a Viper architecture to establish a connection between a Viper components using a protocol with a weak property but I get the following error message: 'weak' may only be applied to class and class-bound protocol types, not 'Self.ViperViewClass' protocol ViperPresenter: class { associatedtype ViperViewClass weak var view: ViperViewClass! { get set } } 回答1: Protocols can't currently require properties to be implemented as weak stored properties

How can you capture multiple arguments weakly in a Swift closure?

南楼画角 提交于 2019-12-08 14:30:19
问题 Is there a way to capture multiple arguments weakly in a swift closure? I know this is the syntax to capture one argument weakly: { [weak arg] arg.doSomething() } How can I do this for two objects that I wish to capture weakly? 回答1: From Expressions in "The Swift Programming Language" (emphasis added): Closure Expression ... A closure expression can explicitly specify the values that it captures from the surrounding scope using a capture list. A capture list is written as a comma separated

WeakReferenced object is not garbage collected after calling System.gc()

夙愿已清 提交于 2019-12-08 06:52:38
问题 I am a fresh new learner of Java. I'm now learning the concept of WeakReference. I came across a problem which probably looks stupid but I just wanna figure out the reason. The problem is: according to Java doc, "Weak reference objects, which do not prevent their referents from being made finalizable, finalized, and then reclaimed." So I did this small test: import java.lang.ref.WeakReference; public class A { public static void main(String[] args) { A a = new A(); WeakReference<A> wr = new

Playing around with ARC: Force release irritation?

佐手、 提交于 2019-12-07 17:11:57
问题 I am currently playing around with ARC a bit to get some things figured out, before starting to do the actual work. I did setup this code: NSNumber* n = [[NSNumber alloc] initWithInt:3]; __weak NSNumber* weakN = n; n = nil; NSLog(@">>>: %@ %@", n, weakN); I expected n and weakN to be nil, as n = nil; should trigger a release in my eyes? Unfortunately it doesn't. The output is ">>>: (null) 3". What am I missing here? Another thing is, that I am pretty sure, the below code was giving me a hard

WeakReference and memory leaks

时光怂恿深爱的人放手 提交于 2019-12-07 16:10:29
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? 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 you are filling the heap (which is very unlikely since 7Mb is too little memory for any heap) you can't