开发者

ref or value class for a list of 20000 objects

I would need to maintain a list of about 20000 objects of a class. This class would be a managed wrapper for a c++ one. I would like to know if a ref class or a value class is they way to go.

I was told the following for pros/cons w.r.t GC

In the case of a value class, there is a single point to garbage collect vs reference class with a pointer, where there would be 20000 objects to garbage collect. Also there is no finalizer or destructor for value class and hence these objects wont end up in the finalization queue which is single threaded.

Can someone el开发者_如何学编程aborate on the functioning of the GC in both these cases for large lists of objects.


In an answer of a question which I could not find at SO, @Hans Passant suggested that a .net struct (value class of C++ Cli) should be 16 bytes atmost.

So do not hesitate, use ref class.

Here is a useful link.


It's definitely easiest to use reference types - the 'Don't make me think' balance really tips this way


FWIW, I can perfectly see myself allocating a buffer of roughly 2Mb to contain an array of valuetype objects.

The thing is, how many times will these have to be transferred. If it just for a lookup table, the array of value objects will win a bit of performance, and perhaps other benefits[1]. This could be useful in a 'FlyWeight' kind of pattern

Just take care to only pass indexes to such objects around or the copying will soon make life miserable on your application

[1] e.g. quick serialization; the array indexes are under the control of the app developer, in contrast to the identity of a reference value. This can make for very efficient 'shallow' serialization of such lookup items by index value, which is useful in a marshalling type of use case (instead of actually marshalling the full reference object, you can refer to it by using the 'ubiquitous constant ID' that you have designed for that purpose).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜