HomeSearch

VB.NET Byte Array: Memory Usage

Use Byte arrays to store data. BinaryReader and ReadByte can be used with a byte array.
Byte array. Byte arrays hold data in an efficient way. Any file can be read in, or downloaded, into a Byte array. Often this is much more efficient than other representations.ByteArray
Notes, handling. Data in a Byte array is sometimes harder to manipulate and read. Often strings or string arrays are a better storage format.
First example. We measure byte array memory use. We allocate a Byte array. The reference to the Byte array requires 4 or 8 bytes on the stack.

And: The 3 million elements, bytes, of the array require a bit less than 3 megabytes of memory.

Important: In an array, each byte element requires exactly 1 byte. But there are additional costs involved.

Program: This example uses a Byte array but not in a useful way. Byte arrays are useful as storage regions for images or binary data.

VB.NET program that creates Byte array Module Module1 Sub Main() 'Get memory. Dim value1 As Long = GC.GetTotalMemory(False) ' Create an array of 3 million bytes. ' ... We specify the last index to create an array. Dim bytes(1000 * 1000 * 3 - 1) As Byte bytes(0) = 0 ' Get memory and print the change. Dim value2 As Long = GC.GetTotalMemory(False) Console.WriteLine(value2 - value1) ' Ensure we have exactly 3 million bytes. Console.WriteLine(bytes.Length) End Sub End Module Output 3000032 3000000
Example 2. The BinaryReader and BinaryWriter types are often used with Byte arrays. It is possible to use a Byte array, wrapped in a MemoryStream, with these types as a backing store.

Info: This is an efficient approach to reading bytes, as it does not access the hard disk more than once.

BinaryReader

Here: We use the File.ReadAllBytes Function. Then we wrap the Byte array in a MemoryStream.

And: We pass that MemoryStream to a BinaryReader. We can use the Byte array, which stores a JPG image now, in the same way as a file.

File
VB.NET program that uses Byte array Imports System.IO Module Module1 Sub Main() ' Byte array stores a JPG. Dim array() As Byte = File.ReadAllBytes("C:\athlete.jpg") Using memory As MemoryStream = New MemoryStream(array) Using reader As BinaryReader = New BinaryReader(memory) ' Display values of first two bytes of JPG in memory. Console.WriteLine(reader.ReadByte()) Console.WriteLine(reader.ReadByte()) End Using End Using End Sub End Module Output 255 216
Note, size. To create an array of 3 million elements, we must specify the last required element index, which is 3 million minus one. This is a key difference between VB.NET and C#.

Note: Thanks to David Deley for pointing out an error in a previous version of the byte array program.

A summary. A Byte array is a versatile type. It stores a direct, accurate representation of binary data. This includes images (JPG, PNG) as well as many different file formats.
Home
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.