weak-references

Is there a way to FORCE weak and/or soft referenced objects to be GC'd in Java?

让人想犯罪 __ 提交于 2019-12-04 00:59:16
Here's my use case. We are trying to narrow down a potential memory leak in an application, and we are using a memory analysis tool to snapshot the heap so we can look for object instances and references. (In case it helps, we're using YourKit.) This application makes extensive use of dynamic and CGLIB proxies, which end up storing tons of references to classes and classloaders in WeakHashMaps. After our test case runs, we are expecting all hard references to object X and its classloader to be gone, but since there were many proxies involved in the test case in the end we have many weak/soft

HashMap with weak values

元气小坏坏 提交于 2019-12-03 23:44:27
I'm implementing a cache for Objects stored persistently. The idea is: Method getObjectFromPersistence(long id); ///Takes about 3 seconds Method getObjectFromCache(long id) //Instantly And have a method: getObject(long id) with the following pseudocode: synchronized(this){ CustomObject result= getObjectFromCache(id) if (result==null){ result=getObjectFromPersistence(id); addToCache(result); } return result; } But I need to allow the CustomObject to be collected by the garbage collector. Until now I was using an HashMap<Long,WeakReference<CustomObject> for the implementation. The problem is

Testing/Verifying a WeakReference

自古美人都是妖i 提交于 2019-12-03 23:17:38
I'd like to verify that code setting up a WeakReference does not accidentally hold a strong reference to the referenced object. (Here's an example of how it is easy to accidentally do this.) Does this look like the best way to check for inadvertent strong references? TestObject testObj = new TestObject(); WeakReference wr = new WeakReference(testObj); // Verify that the WeakReference actually points to the intended object instance. Assert.Equals(wr.Target, testObject); // Force disposal of testObj; testObj = null; GC.Collect(); // If no strong references are left to the wr.Target, wr.IsAlive

Bug in WeakAction in case of Closure Action

有些话、适合烂在心里 提交于 2019-12-03 22:47:01
In one of the projects I take part in there is a vast use of WeakAction . That's a class that allows to keep reference to an action instance without causing its target not be garbage collected. The way it works is simple, it takes an action on the constructor, and keeps a weak reference to the action's target and to the Method but discards the reference to the action itself. When the time comes to execute the action, it checks if the target is still alive, and if so, invokes the method on the target. It all works well except for one case - when the action is instantiated in a closure. consider

How can I lower the weak ref processing time during GC?

我是研究僧i 提交于 2019-12-03 17:25:18
问题 Currently I am facing the problem that my application is showing long GC times sporadically, but all these are only caused by weak reference processing. So the thread stopped time is always close to the weak ref processing time. All other GC cycles are 0.0001 sec to 0.200 sec. From the gc.log (reformatted): 10388.186: [GC[YG occupancy: 206547 K (306688 K)]10388.186: [Rescan (parallel) , 0.1095860 secs]10388.295: [weak refs processing, 2.0799570 secs] [1 CMS-remark: 2973838K(3853568K)]

Weakreference get() method how safe is it? (Android, asynctask)

六月ゝ 毕业季﹏ 提交于 2019-12-03 17:13:42
问题 I am making an Android mobile app. I have a WeakReference to my Activity in the AsyncTask to ensure that it can be garbage collected. When onPostExecute() gets called, I do Acitivty activity = mWeakRef.get(); Then I use the activity object to display dialogs to the user etc etc. My question is, as I am trying to determine which dialog to show and what to do, could my activity object become null? Could it ever become null if the GC runs in between my line of execution? Am I safe to keep using

Android Weak Reference of Inner Class

别来无恙 提交于 2019-12-03 16:27:31
I have gone through the article http://developer.android.com/resources/articles/avoiding-memory-leaks.html . In this article it is suggested to use static inner class with Weak Reference . public class GalleryVideo extends Activity { private int AUDIO_NO = 1; ........................... ................ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); gallery = (Gallery) findViewById(R.id.examplegallery); gallery.setAdapter(new AddImgAdp(this)); } static public class AddImgAdp extends BaseAdapter { private int GalItemBg; private Context cont;

Can't make weak reference to closure in Swift

怎甘沉沦 提交于 2019-12-03 16:25:17
问题 Update: I tried writing it without making it weak, and there doesn't seem to be a leak. So maybe the question is no longer necessary. In Objective-C ARC, when you want to have a closure be able to use itself inside of the closure, the block cannot capture a strong reference to itself, or it will be a retain cycle, so instead you can make the closure capture a weak reference to itself, like so: // This is a simplified example, but there are real uses of recursive closures int (^fib)(int); _

Nested blocks and references to self

岁酱吖の 提交于 2019-12-03 14:25:28
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? This is most likely occurring as you are targeting down to iOS 4. You should change it to be __unsafe_unretained MyClass

Weak reference instead of getActivity() (Android avoid memory leak)?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 13:32:17
To avoid a memory leak I wrote the following method that will be used in activities and mainly in fragments (using inheritance). That method is supposed to allow me to never directly refer to the activity by calling //this or getActivity() The method is: private WeakReference<BaseActivity> activityWeakReference = null; public BaseActivity getActivityFromWeakReference(){ activityWeakReference = activityWeakReference == null ? new WeakReference<BaseActivity>((BaseActivity)getActivity()) : activityWeakReference; return activityWeakReference.get(); } Is calling this method