Re: How do you figure out how much memory a class is using?

From: Joe Fallon (jfallon1_at_nospamtwcny.rr.com)
Date: 04/29/04


Date: Wed, 28 Apr 2004 23:10:23 -0400

Jay,
It helps a lot.

I tried System.Runtime.InteropServices.Marshal.SizeOf on one of my classes
and it failed.

Now that I know it is basically "impossible" I can stop going down that
path.

I decided to use a web.config appsetting flag for this.
When the flag is False then I use Session to store the collection.
When it is True, I will serialize the collection to disk instead.

I only plan to do this for potentially large objects.
Smaller ones will be tossed into Session or Viewstate.

So instead of the normal 2 lines of code (1 to put the object into Session
and 1 to take it out)
I just wrap them in an If statement and check the flag.

So by default, we will store things in Session, but in a pinch can move
large objects to disk and back.
I guess that is a workable approach.

(My original goal was to dynamically test the size of the object and if it
crossed a threshold to move it to disk.)

Thanks for the help Jay!

-- 
Joe Fallon
Access MVP
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in message
news:uXAAVjYLEHA.2692@tk2msftngp13.phx.gbl...
> Joe,
> There is no way to get the size of a managed object per se.
>
> You could use System.Runtime.InteropServices.Marshal.SizeOf, however this
> returns the size of the object when it is marshaled to the unmanaged
world,
> it does not report the true size of the object in the managed realm.
>
> Also I do not believe the Marshal.SizeOf method takes into account
> collection classes, you could use the enumerator of the class to get the
> size of each element, unfortunately this will not give you a good idea of
> any overhead in the management of the collection...
>
> Hope this helps
> Jay
>
> "Joe Fallon" <jfallon1@nospamtwcny.rr.com> wrote in message
> news:OFEVVETLEHA.3548@TK2MSFTNGP10.phx.gbl...
> > I would like to know how you can figure out how much memory a given
> instance
> > of a class is using.
> >
> > For example, if I load a collection class with 10 items it might use
1KB,
> > and if I load it with 1000 items it might use 100KB.
> > How do I measure the amount of memory used once the class is loaded?
> >
> > Thanks!
> > -- 
> > Joe Fallon
> >
> >
> >
> >
>
>


Relevant Pages

  • Re: [Full-disclosure] [WEB SECURITY] Preventing Cross-site Request Forgeries
    ... creating another layer of state management on top of the HTTP ... addition to the usual session state. ... solutions for workflow and pageflow. ... Additional levels of state management like these allow developers to ...
    (Full-Disclosure)
  • Re: Suspend to disk
    ... > whereby a Linux session can be saved to disk, ... Suspend to disk means that it writes all ... it should resume the session from, that is the swap partition. ...
    (comp.os.linux.misc)
  • Re: Beginner question re postback
    ... Session is extremely useful and user specific. ... Querystring is great for passing small pieces of info in the URL like a PK ... Cache can be set with priorities and expirations. ... > "Joe Fallon" wrote in message ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: How to backup to a DVD?
    ... However the exchange of data during a write session is ... as an IDE device it is inherently more CPU intensive vs SCSI. ... > DVD-RAM capacity. ... >> I just formatted a DVD Ram disk using the Disk Management tool in ...
    (microsoft.public.windows.server.sbs)
  • Re: How do you figure out how much memory a class is using?
    ... Can you elaborate on how you control these with some sample code? ... Session, Viewstate, Cache, Application. ... > "Joe Fallon" wrote in message ... >> Jay, ...
    (microsoft.public.dotnet.languages.vb)