473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Best practice to allocate this byte array for this situation?

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.

Dec 30 '06 #1
1 3359
tr*************@gmail.com 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
Dec 30 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
6
by: Gator | last post by:
Hi All, Basically my situation is this, I have a server implemented in C++, unmanaged code, using proprietery protocol over TCP/IP to communicate with the cilent(c++ also). Now, I am implementing...
5
by: Philippe Bertrand | last post by:
Using C#, I want to send a byte array to an unmanaged function with the minimum amount of copies. The array is input only and won't be modified (its copied on the unmanaged side). I'm currently...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
7
by: Cory Toms | last post by:
Hey All, I have question about the best way to go about doint this: SqlDataReader _dr=components.getItems(); fooclass _myarray = new fooclass; //create new array of my class int i=0; ...
4
by: marora | last post by:
I have created class definition which contains a charater pointer as one of it's data memeber. The objective is to read some data from a file, and assign it to a data member; Size of data is...
5
by: Trapulo | last post by:
Hello, I need to send to a webservice a parameter that is a string containing an XML doc. In this xml, a node value came from a byte array (it's an RSA signature). What is the best way to convert...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
2
by: O.B. | last post by:
I have operation within a class that marshals the data into a byte array. Below are three different ways that work. Are there any downsides to using one over the the other? public virtual byte...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.