开发者

How are weak arrays used?

开发者 https://www.devze.com 2023-02-09 11:45 出处:网络
Question says it all.I have a data struc开发者_如何学编程ture I can\'t Marshal because of a weak hashtable .. wondering if I can get rid of it :)A weak array is an array of weak pointers. A weak point

Question says it all. I have a data struc开发者_如何学编程ture I can't Marshal because of a weak hashtable .. wondering if I can get rid of it :)


A weak array is an array of weak pointers. A weak pointer is a reference on a value that may be garbage collected.

If you use a regular pointer on a value you will prevent its garbage collection until the referee is itself garbage collected. With a weak reference the value may be collected before the referee.

An example of use is a source that feeds data to multiple sinks. If the source holds regular pointers to the sinks, whenever a sink is no longer needed it won't be garbage collected until the source is (which may for example never happen). If the source uses weak references to the sinks, given sinks may be garbage collected before the source.

Another example is hashconsing for a type which uses weak hashtables (which involve weak arrays). Quickly, hashconsing is a way to remember all values of a given type that are created and living in the program. Together with an appropriate value constructor this can ensure maximal sharing of values of that type and allows to implement structural equality on that type as physical equality. In that case if a non-weak hashtable is used, values no longer used by the program would never be garbage collected.

Finally, many people think (wrongly) that weak references are useful to implement caches. Keep a weak ref on a value, if it was garbage collected, reload/recompute the value. This is not a good cache algorithm because a major garbage collection reclaims any value no longer referenced. So your caching algorithm has no predictability or useful property like, for example, size of cache / available memory doesn't exceed a given ratio.


Use a bijective pair of map functions between your data structure and its structurally congruent representation that's compatible with the Marshall module.

0

精彩评论

暂无评论...
验证码 换一张
取 消