tags:

views:

96

answers:

4

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

+11  A: 

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.

Mehrdad Afshari
+1  A: 

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.

Jon Limjap
A: 

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.

Martin Wickman
+1  A: 

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.

MaLio