PC Review


Reply
Thread Tools Rate Thread

Best practice to allocate this byte array for this situation?

 
 
trevor.farchild@gmail.com
Guest
Posts: n/a
 
      30th Dec 2006
Hi, long time reader, first time poster

I have an application that will be doing this 5 times a second:
Get a bunch of data from a NetworkStream and convert it into a Bitmap

Therefore, the process looks like:
Read the NetworkStream to find out size of the data
Allocate that amount as a byte array
Read the NetworkStream into the byte array
Put the byte array into a MemoryStream
Put the MemoryStream into a Bitmap

This is a tedious process and I was wondering if there was a way to
shortcut this. I tried putting a NetworkStream directly into a Bitmap
with no avail. Something to do with the NetworkStream not being
seekable.

Now, if there is no easy way to shortcut this, what's faster:
allocating a fixed amount of memory (10MB) and increase the size of
needed? Or to allocate the byte array each time. Keep in mind this
happens 5 times per second so I don't know how fast C# is at memory
management, or how it does it.

 
Reply With Quote
 
 
 
 
Carl Daniel [VC++ MVP]
Guest
Posts: n/a
 
      30th Dec 2006
(E-Mail Removed) wrote:
> Hi, long time reader, first time poster
>
> I have an application that will be doing this 5 times a second:
> Get a bunch of data from a NetworkStream and convert it into a Bitmap
>
> Therefore, the process looks like:
> Read the NetworkStream to find out size of the data
> Allocate that amount as a byte array
> Read the NetworkStream into the byte array
> Put the byte array into a MemoryStream
> Put the MemoryStream into a Bitmap
>
> This is a tedious process and I was wondering if there was a way to
> shortcut this. I tried putting a NetworkStream directly into a Bitmap
> with no avail. Something to do with the NetworkStream not being
> seekable.
>
> Now, if there is no easy way to shortcut this, what's faster:
> allocating a fixed amount of memory (10MB) and increase the size of
> needed? Or to allocate the byte array each time. Keep in mind this
> happens 5 times per second so I don't know how fast C# is at memory
> management, or how it does it.


5 times a second is not much - allocation take only a few microseconds on a
typical modern PC. That said, you'll get the best performance by making a
single large-enough allocation and re-using it time and again. You'll also
avoid fragmenting the GC heap and causing excess collection cycles by
holding onto a single large memory allocations.

-cd


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Allocate more memory into Byte array Arthur Yousif Microsoft C# .NET 11 31st Mar 2004 12:46 AM
Allocate more memory into Byte array Arthur Yousif Microsoft C# .NET 4 30th Mar 2004 02:21 AM
Allocate more memory into Byte array Arthur Yousif Microsoft Dot NET Framework 4 30th Mar 2004 02:21 AM
Allocate more memory into Byte array Arthur Yousif Microsoft C# .NET 0 29th Mar 2004 10:48 PM
Allocate more memory into Byte array Arthur Yousif Microsoft Dot NET Framework 0 29th Mar 2004 10:48 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:39 AM.