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++) {
_queueTest.Enqueue(i);
}
Debug.Print("Bytes Free: " + Debug.GC(true));
}
}
}
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.
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.
@ 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.
@ 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).