actionscript 3 - How to avoid memory leaks in this case? -
in order prevent memory leaks in actionscript 3.0, use member vector in classes have work vectors, example:
public class { private var mhelperpointvector:vector.<point> = new vector.<point>(); public static getfirstdata():vector.<point> { mhelperpointvector.length = 0; .... return mhelperpointvector; } public static getseconddata():vector.<point> { mhelperpointvector.length = 0; .... return mhelperpointvector; } } and have consumers uses getfirstdata , getseconddata methods, storing references vectors returned these methods, example:
public function onenterframe():void { var vector:vector.<point> = a.getseconddata(); .... } this trick seems good, need process vector returned getseconddata() after period of time, , in case, vector becomes overwritten call getseconddata() or getfirstdata()...the solution copy vector new vector...but in case better avoid trick @ all. how deal these problems? have work big amount of vectors (each of length between 1-10).
the thing garbage collection trying avoid instantiating (and disposing of) as possible. it's hard best approach since can't see how/why you're using vector data, @ first glance think approach you'll losing data (you're pretty creating equivalent of weak instances, since can overwritten) , changing length of vector doesn't avoid garbage collection (it may delay , reduce it, you're still throwing data away).
i frankly don't think you'd have memory leaks point vectors unless you're leaking reference vector left , right. in case, it'd better fix these leftover references, rather coming solution reuse same vectors (which can have many more adverse effects).
however, if you're really concerned memory, best solution, think, either creating vectors need in advance (if it's fixed number , know length ahead of time) or, better yet, using object pools. latter more robust solution, requires setup on end, both creating pool class , when using it. put in code, once implemented, used this:
// need vector length of 9 var myvector:vector.<point> = vectorpool.get(9); // use vector stuff ... // vector not needed anymore, put in pool vectorpool.put(myvector); myvector = null; // it's clear can't use anymore vectorpool control list of vectors have, letting other parts of code "borrow" vectors needed (in marked being "used" inside vectorpool) , give them (marking them unused). code create vectors on spot (inside get()), needed, if no usable vectors available within list of unused objects; make more flexible (not recommended in cases since you're still spending time instantiation, negligible in case).
this macro explanation (you'd still have write vectorpool), object pools believed definitive solution avoid re-instantiating garbage collection of objects going reused.
for reference, here's used generic object pool: https://github.com/zeh/as3/blob/master/com/zehfernando/data/objectpool.as
or more specialized one, use in situations when need bunch of throwaway bitmapdata instances of similar sizes: https://github.com/zeh/as3/blob/master/com/zehfernando/data/bitmapdatapool.as
i believe implementation of vectorpool class in molds of need similar link above.
as side note, if performance concern, i'd suggest using vectors of fixed length too, e.g.
// create vector of 9 items, filled `nulls` var mypoints:vector.<point> = new vector.<point>(9, true); this makes faster since won't have micro allocations on time. have set items directly, instead of using push():
mypoints[0] = new point(0, 0); but that's forced advantage since setting vector items faster push().
Comments
Post a Comment