Is the Queue Object really this inefficient?

using System;
using Microsoft.SPOT;
using System.Collections;

namespace PerformanceTest {
    public class Program {
        public static void Main() {
            Debug.Print("Bytes Free: " + Debug.GC(true));

            Queue _queueTest = new Queue();
            ///storing 1024 bytes to determine how much memory is used
            Debug.Print("Bytes Free: " + Debug.GC(true));
            for (int i = 0; i < 1024; i++) {
            Debug.Print("Bytes Free: " + Debug.GC(true));


Debug Response:

Bytes Free: 83352
Bytes Free: 83004
Bytes Free: 46188

By my calculations it takes 35 bytes to store 1 byte of data in a Queue.
In contrast it takes just over 1 byte to store data in a byte[].

I’m not complaining, I just want to know the technical reasons why.

Keep in mind that every integer is boxed to an object for queuing.

Ah, so it’s really object[] that’s being stored. Got it.

To avoid the boxing, use an int[] instead of a Queue. You’ll have to manage the queue semantics yourself, then, so it’s a tradeoff between memory efficiency and code size.

Once I made quick calc on an OutOfMemoryException when adding items to an ArrayList (which hosts an object[]) and I came to 12 Bytes per element (without it’s content).
So an object reference seams to need 12 bytes in RAM.

Code size I can handle, I’ll take the memory efficiency.

I’m with you, I’ll take larger code size every time if it makes my job easier and/or my code more reliable.

I’m thinking about using a Queue (first time for me) in a serial port event handler. The gizmo on the other end of the serial port sends several messages once a second, each message is terminated with a line feed. The event handler will read the bytes available into a byte array and when the line feed comes in, it will dumb the whole byte array into the queue. Processing the messages in the queue will be done outside the event handler in the main program.

However, if Queues create a lot of garbage, or are really slow, or whatever, I’ll probably stay away from them. I’d be very interested in hearing your comments about Queues on these (or any other) issues.

Thanks - Gene

@ Gene - I would create a queue that is strongly typed. The link below contains some code that I provided as an example, look for the ByteBuffer implementation which is a queue implementation over a byte array.

@ taylorza - Man, you’re my new best friend. Your ByteBuffer class looks really good for my needs, also really clean and easy to read. I’m going hiking with my family right now but I’ll give it a whirl when I get back.

Thanks - Gene

@ taylorza - It works great. I changed the buffer type to a jagged array (byte[ ][ ]) so I can enqueue and dequeue a complete line feed terminated byte array message (in this case, a bunch of different NMEA messages from a GPS receiver).

Many thanks - Gene

@ Gene - That sounds great, I am glad that you have had success with the code and been able to adapt it to your needs.

It sounds like the class deserves a code share entry…