开发者

Wrap Byte[] in a class to reduce memory overhead

I have large amount of data been passed around in my application as byte[] objects. Which is also turing out to be memory problems in 开发者_开发技巧a lot of cases. What about if i wrap byte[] in a class like

[Serializable] public class MyClass { public byte[] Data { get; set; } }

Do you guys think i would gain any performance becoz now a reference type would be passed around rather a value type ,hence data doesnt have to be copied every time.

Looking forward to your answers


Why should it make an improvement? It can only make things worse. A byte[] is a reference type itself, not a value type. The effect will be one unnecessary level of indirection and heap allocation for the class.


Have you tried a memory profiling tool to find out where the memory leak is?

I think you could more easily identify your problems if you use a tool like dotTrace to do an actual memory profile and then find which parts of your program are eating up memory.

That's the only time you could find real solutions for your memory problems.


It doesn't matter since the byte array is still there.

But, if you are willing to sacrifice performance for memory I would suggest that you indeed wrap your byte array in an object, but keep the array itself on disk and only read it when you need it. Ie a proxy object.

Of course, that depends on your application and how you are using the bytes. For instance, you may not even need to load the whole array if the client only need parts of it.


As Mehrad Afshari already points out, the byte[] is a reference type already.

Considerations:

You should check that you are referencing the same instance where appropriate (i.e. make sure you are not deserializing the instance for read only access multiple times), and you may wish to implement some compression if appropriate. Also consider if you can divide the array for processing into smaller chunks and operate on them sequentially.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜