给定Queue<MyMessage>
,其中MyMessage
是某些类型消息的基类:所有消息类型具有不同的字段,因此它们将使用不同的字节数。因此,根据字节而不是队列中存在的元素来衡量此队列的填充级别是有意义的。 事实上,由于该队列与连接相关联,所以我可以更好地控制消息流,在队列几乎满时减少流量。监视队列的状态
为了达到这个目标,我想用一个自定义类MyQueue
包装一个简单的Queue
。
public class MyQueue
{
private Queue<MyMessage> _outputQueue;
private Int32 _byteCapacity;
private Int32 _currentSize; // number of used bytes
public MyQueue(int byteCapacity)
{
this._outputQueue = new Queue<MyMessage>();
this._byteCapacity = byteCapacity;
this._currentSize = 0;
}
public void Enqueue(MyMessage msg)
{
this._outputQueue.Enqueue(msg);
this._currentSize += Marshal.SizeOf(msg.GetType());
}
public MyMessage Dequeue()
{
MyMessage result = this._outputQueue.Dequeue();
this._currentSize -= Marshal.SizeOf(result.GetType());
return result;
}
}
的问题是,这是不好的班,因为Marshal.SizeOf
抛出ArgumentException
例外。
- 是否有可能以某种方式计算对象的大小(类的实例)?
- 是否有一些替代方案来监控字节方面的队列填充水平?
- 是否有任何可以用这种方式管理的队列?
UPDATE:作为一种替代解决方案,我可以在每个消息类型添加一个方法int SizeBytes()
,但这种方法似乎有点难看,尽管这也许是最有效的,因为你不能简单地测定被引用类型。
public interface MyMessage
{
Guid Identifier
{
get;
set;
}
int SizeBytes();
}
实现此接口的必须,除了实施SizeBytes()
方法,还实现了Identifier
属性的类。
public class ExampleMessage
{
public Guid Identifier { get; set; } // so I have a field and its Identifier property
public String Request { get; set; }
public int SizeBytes()
{
return (Marshal.SizeOf(Identifier)); // return 16
}
}
的sizeof
运营商无法与Guid
使用,因为它没有一个预定义的大小,所以我用Marshal.SizeOf()
。但在这一点也许应使用实验值:例如,由于用于Guid
Marshal.SizeOf()
返回16并且由于string
由N char
,则SizeBytes()
方法可以是如下:
public int SizeBytes()
{
return (16 + Request.Length * sizeof(char));
}
您无法轻松测量参考类型。 – SLaks 2012-02-09 19:15:10
如果使用Marshal.SizeOf(msg)而不是msg.GetType(),会发生什么? – Joe 2012-02-09 19:15:11
@Joe:它抛出一个'ArgumentException'。 – enzom83 2012-02-09 19:17:38