473,805 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3410
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
9468
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 code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
6
15323
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 the another client in C#. Server side can not be changed :( So, I am using tcp/ip sockets. In the end I get byte on the client side, which originally was some C++ class object, converted to byte array.
5
4794
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 using fixed byte *. My question is: Should I be using In byte parameter instead? Ref parameter? fixed( byte *b = byte_array ) { MyUnmangedFunc2( b, byte_array.Length ); }
15
34615
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
36939
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; while (_dr.Read()) //loop through data reader to add items to the array
4
5005
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 not known in the begining. We can assume that it will not exceed 256; class definition:
5
5883
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 the original byte () value to the xml and viceversa? I've tried a lof of way, as encoding.utf8, encoding.unicode, bitconverter, etc, but the only working solution I found is Convert.XXbase64String: writer.WriteElementString("ContentSignature",
16
2824
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 efficient navigating within Visual Studio 2005. Let's say your project (or solution) has dozens of forms and hundreds or even thousands of routines. Two Questions: 1) BUILT-IN to Visual Studio 2005. What ideas do you have to quickly
2
15950
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 ToRaw1() { byte byteArray = new byte; IntPtr pointer = Marshal.AllocHGlobal(Size); Marshal.StructureToPtr(this, pointer, false); Marshal.Copy(pointer, byteArray, 0, Size);
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.