Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7447335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:34:47+00:00 2026-05-29T12:34:47+00:00

Given a Queue<MyMessage> , where MyMessage is the base class for some types of

  • 0

Given a Queue<MyMessage>, where MyMessage is the base class for some types of messages: all message types have different fields, so they will use a different amount of bytes. Therefore it would make sense to measure the fill level of this queue in terms of bytes rather than of elements present in the queue.
In fact, since this queue is associated with a connection, I could better control the message flow, reducing the traffic if the queue is nearly full.

In order to get this target, I thought to wrap a simple Queue with a custom class MyQueue.

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;
    }
}

The problem is that this is not good for classes, because Marshal.SizeOf throws an ArgumentException exception.

  1. Is it possible to calculate in some way the size of an object (instance of a class)?
  2. Are there some alternatives to monitor the fill level of a queue in terms of bytes?
  3. Are there any queues that can be managed in this way?

UPDATE: As an alternative solution I could add a method int SizeBytes() on each message type, but this solution seems a little ugly, although it would perhaps be the most efficient since You cannot easily measure a reference type.

public interface MyMessage
{
    Guid Identifier
    {
        get;
        set;
    }

    int SizeBytes();
}

The classes that implement this interface must, in addition to implementing the SizeBytes() method, also implement an Identifier property.

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
    }
}

The sizeof operator can not be used with Guid because it does not have a predefined size, so I use Marshal.SizeOf(). But at this point perhaps I should use the experimentally determined values​​: for example, since Marshal.SizeOf() returns 16 for a Guid and since a string consists of N char, then the SizeBytes() method could be as following:

public int SizeBytes()
{
   return (16 + Request.Length * sizeof(char));
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T12:34:48+00:00Added an answer on May 29, 2026 at 12:34 pm

    If you could edit the MyMessage base class with a virtual method SizeOf(), then you could have the message classes use the c# sizeof operator on its primitive types. If you can do that, the rest of your code is gold.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two systems that produce different types of messages that I need to
I was just given the task of PUTTING messages onto a Azure message queue
I have an NServiceBus application for which a given message may not be processed
I am presently designing a message queue system that will be used by various
We have an application that uses the window message queue to pass data from
I have a class called Root which serves as some kind of phonebook for
Situation Users can upload Documents, a queue message will be placed onto the queue
Given all the options to have processes or threads interact with each other (locks,
How do I remove a queued message of a given type from a handler's
I have to use <base href=http://www.mysite.com/it//> on my site because I am using apache

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.