Object size in memory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cameron

    Object size in memory

    I need to get the size of an objet in memory. I have tried:

    System.IO.Memor yStream m = new System.IO.Memor yStream();

    System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er b = new
    System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er();
    b.Serialize(m, Obj);
    double size = Convert.ToDoubl e(m.Length);

    but not everything is serializable. The thing is I want to be able to
    take an arbitarty object, (since everything I am attempting to size is
    comming out of the cache), and get the size of it. I am wondering if it
    not possible to treat it like a stream or a byte array or something like
    that so that it is easy to get the size. I don't need any attributes off
    of the object, I just need the size. Anyone have any ideas?

    -Cam

  • Jon Skeet [C# MVP]

    #2
    Re: Object size in memory

    cameron <cameron.charle bois@appdepot.c om> wrote:[color=blue]
    > I need to get the size of an objet in memory. I have tried:
    >
    > System.IO.Memor yStream m = new System.IO.Memor yStream();
    >
    > System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er b = new
    > System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er();
    > b.Serialize(m, Obj);
    > double size = Convert.ToDoubl e(m.Length);
    >
    > but not everything is serializable. The thing is I want to be able to
    > take an arbitarty object, (since everything I am attempting to size is
    > comming out of the cache), and get the size of it. I am wondering if it
    > not possible to treat it like a stream or a byte array or something like
    > that so that it is easy to get the size. I don't need any attributes off
    > of the object, I just need the size. Anyone have any ideas?[/color]

    "Size" can be either misleading or meaningless when you consider
    reference types. For instance:

    public class Foo
    {
    string x;

    public Foo (string x)
    {
    this.x=x;
    }
    }


    Foo f1 = new Foo ("hello");
    Foo f2 = new Foo ("hello");

    Now, what is the size of f1 and f2? Combined, it should be double the
    size of the Foo class instance itself (probably 12 bytes - an object
    header of 8 bytes plus a reference) and the size of the string (16 or
    20 bytes, at a guess) - but the size of each of them individually would
    be the size of the Foo class instance itself plus the string - so the
    whole is greater than the sum of the parts... but if you ignore the
    string itself, it becomes meaningless in the other way.

    So, what do you *really* mean, and what do you *really* want to try to
    measure?

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Stoitcho Goutsev \(100\) [C# MVP]

      #3
      Re: Object size in memory

      Hi cameron,
      Serialization won't help. First because it will put more info than you maybe
      expect. The stream is goint to be bigger then the object itself is. Second
      because serializer (if you don't take any precautios) will serialize any
      serializable objects your object references.

      IMHO there is no way to get the size of a managed object. Even the size
      might not be the same if you run the application on machines with different
      configuration and/or different versions of .NET. If not stated otherways CLR
      is free to rearange and layout the objects as it sees fit. That means that
      you may end up with diffrent padding of the fields and different sizes in
      memory.

      The only size that you can get is the size on bytes of the unmanaget
      equivalent of your type
      Marshal.SizeOf( ...)
      --
      B\rgds
      100

      "cameron" <cameron.charle bois@appdepot.c om> wrote in message
      news:%23dfhFz8% 23DHA.3500@tk2m sftngp13.phx.gb l...[color=blue]
      > I need to get the size of an objet in memory. I have tried:
      >
      > System.IO.Memor yStream m = new System.IO.Memor yStream();
      >
      > System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er b = new
      > System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er();
      > b.Serialize(m, Obj);
      > double size = Convert.ToDoubl e(m.Length);
      >
      > but not everything is serializable. The thing is I want to be able to
      > take an arbitarty object, (since everything I am attempting to size is
      > comming out of the cache), and get the size of it. I am wondering if it
      > not possible to treat it like a stream or a byte array or something like
      > that so that it is easy to get the size. I don't need any attributes off
      > of the object, I just need the size. Anyone have any ideas?
      >
      > -Cam
      >[/color]


      Comment

      • cameron

        #4
        Re: Object size in memory

        Fair enough. The Object is being placed in the Cache - I want to know
        how much space is being taken up by that object, (in it's entirity), on
        the Heap.

        -Cam

        Jon Skeet [C# MVP] wrote:[color=blue]
        > cameron <cameron.charle bois@appdepot.c om> wrote:
        >[color=green]
        >>I need to get the size of an objet in memory. I have tried:
        >>
        >>System.IO.Mem oryStream m = new System.IO.Memor yStream();
        >>
        >>System.Runtim e.Serialization .Formatters.Bin ary.BinaryForma tter b = new
        >>System.Runtim e.Serialization .Formatters.Bin ary.BinaryForma tter();
        >>b.Serialize(m , Obj);
        >>double size = Convert.ToDoubl e(m.Length);
        >>
        >>but not everything is serializable. The thing is I want to be able to
        >>take an arbitarty object, (since everything I am attempting to size is
        >>comming out of the cache), and get the size of it. I am wondering if it
        >>not possible to treat it like a stream or a byte array or something like
        >>that so that it is easy to get the size. I don't need any attributes off
        >>of the object, I just need the size. Anyone have any ideas?[/color]
        >
        >
        > "Size" can be either misleading or meaningless when you consider
        > reference types. For instance:
        >
        > public class Foo
        > {
        > string x;
        >
        > public Foo (string x)
        > {
        > this.x=x;
        > }
        > }
        >
        >
        > Foo f1 = new Foo ("hello");
        > Foo f2 = new Foo ("hello");
        >
        > Now, what is the size of f1 and f2? Combined, it should be double the
        > size of the Foo class instance itself (probably 12 bytes - an object
        > header of 8 bytes plus a reference) and the size of the string (16 or
        > 20 bytes, at a guess) - but the size of each of them individually would
        > be the size of the Foo class instance itself plus the string - so the
        > whole is greater than the sum of the parts... but if you ignore the
        > string itself, it becomes meaningless in the other way.
        >
        > So, what do you *really* mean, and what do you *really* want to try to
        > measure?
        >[/color]

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Object size in memory

          cameron <cameron.charle bois@appdepot.c om> wrote:[color=blue]
          > Fair enough. The Object is being placed in the Cache - I want to know
          > how much space is being taken up by that object, (in it's entirity), on
          > the Heap.[/color]

          Right - as I said, that really depends on other objects etc. There's no
          way of determining it, I'm afraid. If you know more about your objects
          - like that the bulk of the size is going to be taken up by a byte
          array which you have access to and which won't have duplicate
          references within the cache, for instance, you could use the size of
          that as a rough indication...

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Jeffrey Tan[MSFT]

            #6
            RE: Object size in memory


            Hi cameron,

            Thank you for posting in the community!

            I think Stoitcho and Jon have explained the reason why you can not get the
            managed class instance size.

            But I think what you want to do is getting the unmanaged class instance
            size, so just as Stoitcho point out, you can use Marshal.SizeOf to returns
            the unmanaged size of an object in bytes.

            For more information, please refer to:

            frlrfsystemrunt imeinteropservi cesmarshalclass sizeoftopic.asp

            =============== =============== ============
            Thank you for your patience and cooperation. If you have any questions or
            concerns, please feel free to post it in the group. I am standing by to be
            of assistance.

            Best regards,
            Jeffrey Tan
            Microsoft Online Partner Support
            Get Secure! - www.microsoft.com/security
            This posting is provided "as is" with no warranties and confers no rights.

            Comment

            • Jeffrey Tan[MSFT]

              #7
              RE: Object size in memory


              Hi cameron,

              Does the community's reply make sense to you?

              If you still have anything unclear, please feel free to post, we will help
              you.

              Best regards,
              Jeffrey Tan
              Microsoft Online Partner Support
              Get Secure! - www.microsoft.com/security
              This posting is provided "as is" with no warranties and confers no rights.

              Comment

              Working...