Enum memory allocation

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

    Enum memory allocation

    Regarding use of enum's, I am wondering what the cost of memory is when creating the enumeration on the calling side, and then using it on the function/method side. See example below. If I understand correctly, what is actually happening on the calling side is an instance of the enum class FavoriteSport is getting created and sent to the PrintFavorite routine with a default value of type int. Is this correct? Say I have a couple hundred uses of this enumeration at the same time (Citrix server for a Statewide application), would I have a couple hundred or more instances of this enum class in memory

    I'm still trying to understand .Net and the true object oriented nature of how it works now, so any information would be greatly appreciated

    Thanks

    Michael Isaac

    public enum FavoriteSpor
    Basebal
    Socce
    end enu

    Public Sub PrintFavorite(B yVal intFavorite as FavoriteSport
    Select Case intFavorite ..
    'if Baseball print "I Love baseball" , et
    End Su

    'calling pro
    PrintFavorite(F avoriteSport.So ccer


  • Derek Harmon

    #2
    Re: Enum memory allocation

    "Michael Isaacs" <anonymous@disc ussions.microso ft.com> wrote in message news:87388853-2CED-4316-A3A0-B3ECFA72F49F@mi crosoft.com...[color=blue]
    > Regarding use of enum's, I am wondering what the cost of memory is[/color]
    :[color=blue]
    > an instance of the enum class FavoriteSport is getting created and sent
    > to the PrintFavorite routine with a default value of type int. Is this correct?[/color]

    An Enum is a Structure (extending ValueType), and not a Class. Therefore,
    there is no explicit creation on the heap for this memory. Instead, it is a field
    within a Class, or it exists temporarily on the stack as a local variable.

    The cost is typically 4 bytes. Due to 32-bit word alignment, there's usually
    little benefit in assiging a smaller numeric type to the Enum (more Enums,
    but they're slower to access.)
    [color=blue]
    > Say I have a couple hundred uses of this enumeration at the same time
    > (Citrix server for a Statewide application), would I have a couple hundred
    > or more instances of this enum class in memory?[/color]

    You'll have copies of an integer (or perhaps byte, long, etc.) throughout
    memory and on the stack. (Although to highlight the magnitude of this
    memory consumption, observe that my PDA can hold up to 16 million
    enums; hopefully the server for your app is larger than my PDA.)

    : :[color=blue]
    > public enum FavoriteSport
    > Baseball
    > Soccer
    > end enum
    >
    > Public Sub PrintFavorite(B yVal intFavorite as FavoriteSport)[/color]

    4 bytes are pushed onto the stack to contain intFavorite, and the
    value of this argument has its value copied to it from the calling Sub.
    [color=blue]
    > Select Case intFavorite ...
    > 'if Baseball print "I Love baseball" , etc
    > End Sub
    >
    > 'calling proc
    > PrintFavorite(F avoriteSport.So ccer)[/color]

    As this is passing a constant argument, I believe the 4 bytes are pushed
    directly onto the stack of the called Sub. Contrast this to,

    Dim sport As FavoriteSport = FavoriteSport.S occer
    PrintFavorite( sport)

    Which has a 4-byte copy of the Enum in the calling Sub, and pushes a 4-byte
    copy of the Enum to the called Sub. Hence, here memory consumption is
    8-bytes.

    Also observe that both Enums are stored on the stack automatically, and when
    each Sub ends, the stack rolls back and the memory consumed by these Enums
    are returned to the system W/O requiring a garbage collection.

    Their drawback, if any, is all the copying that takes place. But, it doesn't get
    any faster than copying four-bytes at a time (at least on 32-bit machines), so
    this is ordinarily insignificant.


    Derek Harmon


    Comment

    • Michael Isaacs

      #3
      Re: Enum memory allocation

      Thanks - this was very informative to me. Follow up questions ...

      The question that then comes up is how VB knows that this is an enum object
      if it is only stored as an integer in memory? Ie. how does it know that it
      has methods, functions, etc? My manager is concerned that even if only 4
      bytes are being sent in to the function, that the function is still creating
      some kind of object that allows for these methods, properties, etc to be
      accessed.

      Also, is there any Microsoft documentation out there that explains this, or
      a good web site that anyone knows about?

      Thanks,

      Mike

      "Derek Harmon" <loresayer@msn. com> wrote in message
      news:ukUT1Y5FEH A.1240@TK2MSFTN GP10.phx.gbl...[color=blue]
      > "Michael Isaacs" <anonymous@disc ussions.microso ft.com> wrote in message[/color]
      news:87388853-2CED-4316-A3A0-B3ECFA72F49F@mi crosoft.com...[color=blue][color=green]
      > > Regarding use of enum's, I am wondering what the cost of memory is[/color]
      > :[color=green]
      > > an instance of the enum class FavoriteSport is getting created and sent
      > > to the PrintFavorite routine with a default value of type int. Is this[/color][/color]
      correct?[color=blue]
      >
      > An Enum is a Structure (extending ValueType), and not a Class. Therefore,
      > there is no explicit creation on the heap for this memory. Instead, it is[/color]
      a field[color=blue]
      > within a Class, or it exists temporarily on the stack as a local variable.
      >
      > The cost is typically 4 bytes. Due to 32-bit word alignment, there's[/color]
      usually[color=blue]
      > little benefit in assiging a smaller numeric type to the Enum (more Enums,
      > but they're slower to access.)
      >[color=green]
      > > Say I have a couple hundred uses of this enumeration at the same time
      > > (Citrix server for a Statewide application), would I have a couple[/color][/color]
      hundred[color=blue][color=green]
      > > or more instances of this enum class in memory?[/color]
      >
      > You'll have copies of an integer (or perhaps byte, long, etc.) throughout
      > memory and on the stack. (Although to highlight the magnitude of this
      > memory consumption, observe that my PDA can hold up to 16 million
      > enums; hopefully the server for your app is larger than my PDA.)
      >
      > : :[color=green]
      > > public enum FavoriteSport
      > > Baseball
      > > Soccer
      > > end enum
      > >
      > > Public Sub PrintFavorite(B yVal intFavorite as FavoriteSport)[/color]
      >
      > 4 bytes are pushed onto the stack to contain intFavorite, and the
      > value of this argument has its value copied to it from the calling Sub.
      >[color=green]
      > > Select Case intFavorite ...
      > > 'if Baseball print "I Love baseball" , etc
      > > End Sub
      > >
      > > 'calling proc
      > > PrintFavorite(F avoriteSport.So ccer)[/color]
      >
      > As this is passing a constant argument, I believe the 4 bytes are pushed
      > directly onto the stack of the called Sub. Contrast this to,
      >
      > Dim sport As FavoriteSport = FavoriteSport.S occer
      > PrintFavorite( sport)
      >
      > Which has a 4-byte copy of the Enum in the calling Sub, and pushes a[/color]
      4-byte[color=blue]
      > copy of the Enum to the called Sub. Hence, here memory consumption is
      > 8-bytes.
      >
      > Also observe that both Enums are stored on the stack automatically, and[/color]
      when[color=blue]
      > each Sub ends, the stack rolls back and the memory consumed by these Enums
      > are returned to the system W/O requiring a garbage collection.
      >
      > Their drawback, if any, is all the copying that takes place. But, it[/color]
      doesn't get[color=blue]
      > any faster than copying four-bytes at a time (at least on 32-bit[/color]
      machines), so[color=blue]
      > this is ordinarily insignificant.
      >
      >
      > Derek Harmon
      >
      >[/color]


      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: Enum memory allocation

        Michael,[color=blue]
        > The question that then comes up is how VB knows that this is an enum[/color]
        object[color=blue]
        > if it is only stored as an integer in memory?[/color]
        You know it is an enum object by virtual you defined it as an Enum type.
        [color=blue]
        > Ie. how does it know that it has methods, functions, etc?[/color]
        The compiler sees that it is of type FavoriteSport and knows that
        FavoriteSport and acts accordingly.
        [color=blue]
        > My manager is concerned that even if only 4
        > bytes are being sent in to the function, that the function is still[/color]
        creating[color=blue]
        > some kind of object that allows for these methods, properties, etc to be
        > accessed.[/color]
        Unfortunately this is a common misunderstandin g, the compiler knows that an
        Integer has certain methods and is able to call the methods directly. It
        knows that an FavoriteSport has certain methods & is able to call those
        directly. The only time a FavoriteSport would be "creating some kind of
        object" Object in the sense that it is on the heap, is when you pass it as
        an System.Object, System.Enum, System.ValueTyp e or an Interface parameter.
        Most of the time you do not/should not do this.
        [color=blue]
        > Also, is there any Microsoft documentation out there that explains this,[/color]
        or[color=blue]
        > a good web site that anyone knows about?[/color]
        "The Common Language Infrastructure Annotated Standard" by James S. Miller,
        from Addison Wesley. The Common Language Infrastructure Standard itself (non
        annotated) is available on your harddisk when you install the SDK, for
        VS.NET 2003 its at "\Program Files\Microsoft Visual Studio .NET
        2003\SDK\v1.1\T ool Developers Guide\docs". Primarily read up on the
        difference of Reference Types & Value Types.

        Hope this helps
        Jay


        "Michael Isaacs" <msposting@isaa csonline.org.no moresmp> wrote in message
        news:eBrtBaBGEH A.3180@TK2MSFTN GP12.phx.gbl...[color=blue]
        > Thanks - this was very informative to me. Follow up questions ...
        >
        > The question that then comes up is how VB knows that this is an enum[/color]
        object[color=blue]
        > if it is only stored as an integer in memory? Ie. how does it know that[/color]
        it[color=blue]
        > has methods, functions, etc? My manager is concerned that even if only 4
        > bytes are being sent in to the function, that the function is still[/color]
        creating[color=blue]
        > some kind of object that allows for these methods, properties, etc to be
        > accessed.
        >
        > Also, is there any Microsoft documentation out there that explains this,[/color]
        or[color=blue]
        > a good web site that anyone knows about?
        >
        > Thanks,
        >
        > Mike
        >
        > "Derek Harmon" <loresayer@msn. com> wrote in message
        > news:ukUT1Y5FEH A.1240@TK2MSFTN GP10.phx.gbl...[color=green]
        > > "Michael Isaacs" <anonymous@disc ussions.microso ft.com> wrote in message[/color]
        > news:87388853-2CED-4316-A3A0-B3ECFA72F49F@mi crosoft.com...[color=green][color=darkred]
        > > > Regarding use of enum's, I am wondering what the cost of memory is[/color]
        > > :[color=darkred]
        > > > an instance of the enum class FavoriteSport is getting created and[/color][/color][/color]
        sent[color=blue][color=green][color=darkred]
        > > > to the PrintFavorite routine with a default value of type int. Is[/color][/color][/color]
        this[color=blue]
        > correct?[color=green]
        > >
        > > An Enum is a Structure (extending ValueType), and not a Class.[/color][/color]
        Therefore,[color=blue][color=green]
        > > there is no explicit creation on the heap for this memory. Instead, it[/color][/color]
        is[color=blue]
        > a field[color=green]
        > > within a Class, or it exists temporarily on the stack as a local[/color][/color]
        variable.[color=blue][color=green]
        > >
        > > The cost is typically 4 bytes. Due to 32-bit word alignment, there's[/color]
        > usually[color=green]
        > > little benefit in assiging a smaller numeric type to the Enum (more[/color][/color]
        Enums,[color=blue][color=green]
        > > but they're slower to access.)
        > >[color=darkred]
        > > > Say I have a couple hundred uses of this enumeration at the same time
        > > > (Citrix server for a Statewide application), would I have a couple[/color][/color]
        > hundred[color=green][color=darkred]
        > > > or more instances of this enum class in memory?[/color]
        > >
        > > You'll have copies of an integer (or perhaps byte, long, etc.)[/color][/color]
        throughout[color=blue][color=green]
        > > memory and on the stack. (Although to highlight the magnitude of this
        > > memory consumption, observe that my PDA can hold up to 16 million
        > > enums; hopefully the server for your app is larger than my PDA.)
        > >
        > > : :[color=darkred]
        > > > public enum FavoriteSport
        > > > Baseball
        > > > Soccer
        > > > end enum
        > > >
        > > > Public Sub PrintFavorite(B yVal intFavorite as FavoriteSport)[/color]
        > >
        > > 4 bytes are pushed onto the stack to contain intFavorite, and the
        > > value of this argument has its value copied to it from the calling Sub.
        > >[color=darkred]
        > > > Select Case intFavorite ...
        > > > 'if Baseball print "I Love baseball" , etc
        > > > End Sub
        > > >
        > > > 'calling proc
        > > > PrintFavorite(F avoriteSport.So ccer)[/color]
        > >
        > > As this is passing a constant argument, I believe the 4 bytes are pushed
        > > directly onto the stack of the called Sub. Contrast this to,
        > >
        > > Dim sport As FavoriteSport = FavoriteSport.S occer
        > > PrintFavorite( sport)
        > >
        > > Which has a 4-byte copy of the Enum in the calling Sub, and pushes a[/color]
        > 4-byte[color=green]
        > > copy of the Enum to the called Sub. Hence, here memory consumption is
        > > 8-bytes.
        > >
        > > Also observe that both Enums are stored on the stack automatically, and[/color]
        > when[color=green]
        > > each Sub ends, the stack rolls back and the memory consumed by these[/color][/color]
        Enums[color=blue][color=green]
        > > are returned to the system W/O requiring a garbage collection.
        > >
        > > Their drawback, if any, is all the copying that takes place. But, it[/color]
        > doesn't get[color=green]
        > > any faster than copying four-bytes at a time (at least on 32-bit[/color]
        > machines), so[color=green]
        > > this is ordinarily insignificant.
        > >
        > >
        > > Derek Harmon
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: Enum memory allocation

          Michael,[color=blue]
          > The question that then comes up is how VB knows that this is an enum[/color]
          object[color=blue]
          > if it is only stored as an integer in memory?[/color]
          You know it is an enum object by virtual you defined it as an Enum type.
          [color=blue]
          > Ie. how does it know that it has methods, functions, etc?[/color]
          The compiler sees that it is of type FavoriteSport and knows that
          FavoriteSport and acts accordingly.
          [color=blue]
          > My manager is concerned that even if only 4
          > bytes are being sent in to the function, that the function is still[/color]
          creating[color=blue]
          > some kind of object that allows for these methods, properties, etc to be
          > accessed.[/color]
          Unfortunately this is a common misunderstandin g, the compiler knows that an
          Integer has certain methods and is able to call the methods directly. It
          knows that an FavoriteSport has certain methods & is able to call those
          directly. The only time a FavoriteSport would be "creating some kind of
          object" Object in the sense that it is on the heap, is when you pass it as
          an System.Object, System.Enum, System.ValueTyp e or an Interface parameter.
          Most of the time you do not/should not do this.
          [color=blue]
          > Also, is there any Microsoft documentation out there that explains this,[/color]
          or[color=blue]
          > a good web site that anyone knows about?[/color]
          "The Common Language Infrastructure Annotated Standard" by James S. Miller,
          from Addison Wesley. The Common Language Infrastructure Standard itself (non
          annotated) is available on your harddisk when you install the SDK, for
          VS.NET 2003 its at "\Program Files\Microsoft Visual Studio .NET
          2003\SDK\v1.1\T ool Developers Guide\docs". Primarily read up on the
          difference of Reference Types & Value Types.

          Hope this helps
          Jay


          "Michael Isaacs" <msposting@isaa csonline.org.no moresmp> wrote in message
          news:eBrtBaBGEH A.3180@TK2MSFTN GP12.phx.gbl...[color=blue]
          > Thanks - this was very informative to me. Follow up questions ...
          >
          > The question that then comes up is how VB knows that this is an enum[/color]
          object[color=blue]
          > if it is only stored as an integer in memory? Ie. how does it know that[/color]
          it[color=blue]
          > has methods, functions, etc? My manager is concerned that even if only 4
          > bytes are being sent in to the function, that the function is still[/color]
          creating[color=blue]
          > some kind of object that allows for these methods, properties, etc to be
          > accessed.
          >
          > Also, is there any Microsoft documentation out there that explains this,[/color]
          or[color=blue]
          > a good web site that anyone knows about?
          >
          > Thanks,
          >
          > Mike
          >
          > "Derek Harmon" <loresayer@msn. com> wrote in message
          > news:ukUT1Y5FEH A.1240@TK2MSFTN GP10.phx.gbl...[color=green]
          > > "Michael Isaacs" <anonymous@disc ussions.microso ft.com> wrote in message[/color]
          > news:87388853-2CED-4316-A3A0-B3ECFA72F49F@mi crosoft.com...[color=green][color=darkred]
          > > > Regarding use of enum's, I am wondering what the cost of memory is[/color]
          > > :[color=darkred]
          > > > an instance of the enum class FavoriteSport is getting created and[/color][/color][/color]
          sent[color=blue][color=green][color=darkred]
          > > > to the PrintFavorite routine with a default value of type int. Is[/color][/color][/color]
          this[color=blue]
          > correct?[color=green]
          > >
          > > An Enum is a Structure (extending ValueType), and not a Class.[/color][/color]
          Therefore,[color=blue][color=green]
          > > there is no explicit creation on the heap for this memory. Instead, it[/color][/color]
          is[color=blue]
          > a field[color=green]
          > > within a Class, or it exists temporarily on the stack as a local[/color][/color]
          variable.[color=blue][color=green]
          > >
          > > The cost is typically 4 bytes. Due to 32-bit word alignment, there's[/color]
          > usually[color=green]
          > > little benefit in assiging a smaller numeric type to the Enum (more[/color][/color]
          Enums,[color=blue][color=green]
          > > but they're slower to access.)
          > >[color=darkred]
          > > > Say I have a couple hundred uses of this enumeration at the same time
          > > > (Citrix server for a Statewide application), would I have a couple[/color][/color]
          > hundred[color=green][color=darkred]
          > > > or more instances of this enum class in memory?[/color]
          > >
          > > You'll have copies of an integer (or perhaps byte, long, etc.)[/color][/color]
          throughout[color=blue][color=green]
          > > memory and on the stack. (Although to highlight the magnitude of this
          > > memory consumption, observe that my PDA can hold up to 16 million
          > > enums; hopefully the server for your app is larger than my PDA.)
          > >
          > > : :[color=darkred]
          > > > public enum FavoriteSport
          > > > Baseball
          > > > Soccer
          > > > end enum
          > > >
          > > > Public Sub PrintFavorite(B yVal intFavorite as FavoriteSport)[/color]
          > >
          > > 4 bytes are pushed onto the stack to contain intFavorite, and the
          > > value of this argument has its value copied to it from the calling Sub.
          > >[color=darkred]
          > > > Select Case intFavorite ...
          > > > 'if Baseball print "I Love baseball" , etc
          > > > End Sub
          > > >
          > > > 'calling proc
          > > > PrintFavorite(F avoriteSport.So ccer)[/color]
          > >
          > > As this is passing a constant argument, I believe the 4 bytes are pushed
          > > directly onto the stack of the called Sub. Contrast this to,
          > >
          > > Dim sport As FavoriteSport = FavoriteSport.S occer
          > > PrintFavorite( sport)
          > >
          > > Which has a 4-byte copy of the Enum in the calling Sub, and pushes a[/color]
          > 4-byte[color=green]
          > > copy of the Enum to the called Sub. Hence, here memory consumption is
          > > 8-bytes.
          > >
          > > Also observe that both Enums are stored on the stack automatically, and[/color]
          > when[color=green]
          > > each Sub ends, the stack rolls back and the memory consumed by these[/color][/color]
          Enums[color=blue][color=green]
          > > are returned to the system W/O requiring a garbage collection.
          > >
          > > Their drawback, if any, is all the copying that takes place. But, it[/color]
          > doesn't get[color=green]
          > > any faster than copying four-bytes at a time (at least on 32-bit[/color]
          > machines), so[color=green]
          > > this is ordinarily insignificant.
          > >
          > >
          > > Derek Harmon
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Michael Isaacs

            #6
            Re: Enum memory allocation

            Thanks Jay.

            I also did a test using enum objects and checking memory usage to confirm
            that enum objects only take the amount of memory of the base type (default
            integer = 4 bytes). This satisfied my manager on the subject. Also, good
            point about integers and the comparison between integer variables and
            enums - both do have methods, etc, but are both stored as 4 bytes when used.

            Michael Isaacs


            "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
            news:uxhx02EGEH A.3724@TK2MSFTN GP11.phx.gbl...[color=blue]
            > Michael,[color=green]
            > > The question that then comes up is how VB knows that this is an enum[/color]
            > object[color=green]
            > > if it is only stored as an integer in memory?[/color]
            > You know it is an enum object by virtual you defined it as an Enum type.
            >[color=green]
            > > Ie. how does it know that it has methods, functions, etc?[/color]
            > The compiler sees that it is of type FavoriteSport and knows that
            > FavoriteSport and acts accordingly.
            >[color=green]
            > > My manager is concerned that even if only 4
            > > bytes are being sent in to the function, that the function is still[/color]
            > creating[color=green]
            > > some kind of object that allows for these methods, properties, etc to be
            > > accessed.[/color]
            > Unfortunately this is a common misunderstandin g, the compiler knows that[/color]
            an[color=blue]
            > Integer has certain methods and is able to call the methods directly. It
            > knows that an FavoriteSport has certain methods & is able to call those
            > directly. The only time a FavoriteSport would be "creating some kind of
            > object" Object in the sense that it is on the heap, is when you pass it as
            > an System.Object, System.Enum, System.ValueTyp e or an Interface parameter.
            > Most of the time you do not/should not do this.
            >[color=green]
            > > Also, is there any Microsoft documentation out there that explains this,[/color]
            > or[color=green]
            > > a good web site that anyone knows about?[/color]
            > "The Common Language Infrastructure Annotated Standard" by James S.[/color]
            Miller,[color=blue]
            > from Addison Wesley. The Common Language Infrastructure Standard itself[/color]
            (non[color=blue]
            > annotated) is available on your harddisk when you install the SDK, for
            > VS.NET 2003 its at "\Program Files\Microsoft Visual Studio .NET
            > 2003\SDK\v1.1\T ool Developers Guide\docs". Primarily read up on the
            > difference of Reference Types & Value Types.
            >
            > Hope this helps
            > Jay
            >
            >
            > "Michael Isaacs" <msposting@isaa csonline.org.no moresmp> wrote in message
            > news:eBrtBaBGEH A.3180@TK2MSFTN GP12.phx.gbl...[color=green]
            > > Thanks - this was very informative to me. Follow up questions ...
            > >
            > > The question that then comes up is how VB knows that this is an enum[/color]
            > object[color=green]
            > > if it is only stored as an integer in memory? Ie. how does it know that[/color]
            > it[color=green]
            > > has methods, functions, etc? My manager is concerned that even if only[/color][/color]
            4[color=blue][color=green]
            > > bytes are being sent in to the function, that the function is still[/color]
            > creating[color=green]
            > > some kind of object that allows for these methods, properties, etc to be
            > > accessed.
            > >
            > > Also, is there any Microsoft documentation out there that explains this,[/color]
            > or[color=green]
            > > a good web site that anyone knows about?
            > >
            > > Thanks,
            > >
            > > Mike
            > >
            > > "Derek Harmon" <loresayer@msn. com> wrote in message
            > > news:ukUT1Y5FEH A.1240@TK2MSFTN GP10.phx.gbl...[color=darkred]
            > > > "Michael Isaacs" <anonymous@disc ussions.microso ft.com> wrote in[/color][/color][/color]
            message[color=blue][color=green]
            > > news:87388853-2CED-4316-A3A0-B3ECFA72F49F@mi crosoft.com...[color=darkred]
            > > > > Regarding use of enum's, I am wondering what the cost of memory is
            > > > :
            > > > > an instance of the enum class FavoriteSport is getting created and[/color][/color]
            > sent[color=green][color=darkred]
            > > > > to the PrintFavorite routine with a default value of type int. Is[/color][/color]
            > this[color=green]
            > > correct?[color=darkred]
            > > >
            > > > An Enum is a Structure (extending ValueType), and not a Class.[/color][/color]
            > Therefore,[color=green][color=darkred]
            > > > there is no explicit creation on the heap for this memory. Instead,[/color][/color][/color]
            it[color=blue]
            > is[color=green]
            > > a field[color=darkred]
            > > > within a Class, or it exists temporarily on the stack as a local[/color][/color]
            > variable.[color=green][color=darkred]
            > > >
            > > > The cost is typically 4 bytes. Due to 32-bit word alignment, there's[/color]
            > > usually[color=darkred]
            > > > little benefit in assiging a smaller numeric type to the Enum (more[/color][/color]
            > Enums,[color=green][color=darkred]
            > > > but they're slower to access.)
            > > >
            > > > > Say I have a couple hundred uses of this enumeration at the same[/color][/color][/color]
            time[color=blue][color=green][color=darkred]
            > > > > (Citrix server for a Statewide application), would I have a couple[/color]
            > > hundred[color=darkred]
            > > > > or more instances of this enum class in memory?
            > > >
            > > > You'll have copies of an integer (or perhaps byte, long, etc.)[/color][/color]
            > throughout[color=green][color=darkred]
            > > > memory and on the stack. (Although to highlight the magnitude of this
            > > > memory consumption, observe that my PDA can hold up to 16 million
            > > > enums; hopefully the server for your app is larger than my PDA.)
            > > >
            > > > : :
            > > > > public enum FavoriteSport
            > > > > Baseball
            > > > > Soccer
            > > > > end enum
            > > > >
            > > > > Public Sub PrintFavorite(B yVal intFavorite as FavoriteSport)
            > > >
            > > > 4 bytes are pushed onto the stack to contain intFavorite, and the
            > > > value of this argument has its value copied to it from the calling[/color][/color][/color]
            Sub.[color=blue][color=green][color=darkred]
            > > >
            > > > > Select Case intFavorite ...
            > > > > 'if Baseball print "I Love baseball" , etc
            > > > > End Sub
            > > > >
            > > > > 'calling proc
            > > > > PrintFavorite(F avoriteSport.So ccer)
            > > >
            > > > As this is passing a constant argument, I believe the 4 bytes are[/color][/color][/color]
            pushed[color=blue][color=green][color=darkred]
            > > > directly onto the stack of the called Sub. Contrast this to,
            > > >
            > > > Dim sport As FavoriteSport = FavoriteSport.S occer
            > > > PrintFavorite( sport)
            > > >
            > > > Which has a 4-byte copy of the Enum in the calling Sub, and pushes a[/color]
            > > 4-byte[color=darkred]
            > > > copy of the Enum to the called Sub. Hence, here memory consumption is
            > > > 8-bytes.
            > > >
            > > > Also observe that both Enums are stored on the stack automatically,[/color][/color][/color]
            and[color=blue][color=green]
            > > when[color=darkred]
            > > > each Sub ends, the stack rolls back and the memory consumed by these[/color][/color]
            > Enums[color=green][color=darkred]
            > > > are returned to the system W/O requiring a garbage collection.
            > > >
            > > > Their drawback, if any, is all the copying that takes place. But, it[/color]
            > > doesn't get[color=darkred]
            > > > any faster than copying four-bytes at a time (at least on 32-bit[/color]
            > > machines), so[color=darkred]
            > > > this is ordinarily insignificant.
            > > >
            > > >
            > > > Derek Harmon
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Michael Isaacs

              #7
              Re: Enum memory allocation

              Thanks Jay.

              I also did a test using enum objects and checking memory usage to confirm
              that enum objects only take the amount of memory of the base type (default
              integer = 4 bytes). This satisfied my manager on the subject. Also, good
              point about integers and the comparison between integer variables and
              enums - both do have methods, etc, but are both stored as 4 bytes when used.

              Michael Isaacs


              "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
              news:uxhx02EGEH A.3724@TK2MSFTN GP11.phx.gbl...[color=blue]
              > Michael,[color=green]
              > > The question that then comes up is how VB knows that this is an enum[/color]
              > object[color=green]
              > > if it is only stored as an integer in memory?[/color]
              > You know it is an enum object by virtual you defined it as an Enum type.
              >[color=green]
              > > Ie. how does it know that it has methods, functions, etc?[/color]
              > The compiler sees that it is of type FavoriteSport and knows that
              > FavoriteSport and acts accordingly.
              >[color=green]
              > > My manager is concerned that even if only 4
              > > bytes are being sent in to the function, that the function is still[/color]
              > creating[color=green]
              > > some kind of object that allows for these methods, properties, etc to be
              > > accessed.[/color]
              > Unfortunately this is a common misunderstandin g, the compiler knows that[/color]
              an[color=blue]
              > Integer has certain methods and is able to call the methods directly. It
              > knows that an FavoriteSport has certain methods & is able to call those
              > directly. The only time a FavoriteSport would be "creating some kind of
              > object" Object in the sense that it is on the heap, is when you pass it as
              > an System.Object, System.Enum, System.ValueTyp e or an Interface parameter.
              > Most of the time you do not/should not do this.
              >[color=green]
              > > Also, is there any Microsoft documentation out there that explains this,[/color]
              > or[color=green]
              > > a good web site that anyone knows about?[/color]
              > "The Common Language Infrastructure Annotated Standard" by James S.[/color]
              Miller,[color=blue]
              > from Addison Wesley. The Common Language Infrastructure Standard itself[/color]
              (non[color=blue]
              > annotated) is available on your harddisk when you install the SDK, for
              > VS.NET 2003 its at "\Program Files\Microsoft Visual Studio .NET
              > 2003\SDK\v1.1\T ool Developers Guide\docs". Primarily read up on the
              > difference of Reference Types & Value Types.
              >
              > Hope this helps
              > Jay
              >
              >
              > "Michael Isaacs" <msposting@isaa csonline.org.no moresmp> wrote in message
              > news:eBrtBaBGEH A.3180@TK2MSFTN GP12.phx.gbl...[color=green]
              > > Thanks - this was very informative to me. Follow up questions ...
              > >
              > > The question that then comes up is how VB knows that this is an enum[/color]
              > object[color=green]
              > > if it is only stored as an integer in memory? Ie. how does it know that[/color]
              > it[color=green]
              > > has methods, functions, etc? My manager is concerned that even if only[/color][/color]
              4[color=blue][color=green]
              > > bytes are being sent in to the function, that the function is still[/color]
              > creating[color=green]
              > > some kind of object that allows for these methods, properties, etc to be
              > > accessed.
              > >
              > > Also, is there any Microsoft documentation out there that explains this,[/color]
              > or[color=green]
              > > a good web site that anyone knows about?
              > >
              > > Thanks,
              > >
              > > Mike
              > >
              > > "Derek Harmon" <loresayer@msn. com> wrote in message
              > > news:ukUT1Y5FEH A.1240@TK2MSFTN GP10.phx.gbl...[color=darkred]
              > > > "Michael Isaacs" <anonymous@disc ussions.microso ft.com> wrote in[/color][/color][/color]
              message[color=blue][color=green]
              > > news:87388853-2CED-4316-A3A0-B3ECFA72F49F@mi crosoft.com...[color=darkred]
              > > > > Regarding use of enum's, I am wondering what the cost of memory is
              > > > :
              > > > > an instance of the enum class FavoriteSport is getting created and[/color][/color]
              > sent[color=green][color=darkred]
              > > > > to the PrintFavorite routine with a default value of type int. Is[/color][/color]
              > this[color=green]
              > > correct?[color=darkred]
              > > >
              > > > An Enum is a Structure (extending ValueType), and not a Class.[/color][/color]
              > Therefore,[color=green][color=darkred]
              > > > there is no explicit creation on the heap for this memory. Instead,[/color][/color][/color]
              it[color=blue]
              > is[color=green]
              > > a field[color=darkred]
              > > > within a Class, or it exists temporarily on the stack as a local[/color][/color]
              > variable.[color=green][color=darkred]
              > > >
              > > > The cost is typically 4 bytes. Due to 32-bit word alignment, there's[/color]
              > > usually[color=darkred]
              > > > little benefit in assiging a smaller numeric type to the Enum (more[/color][/color]
              > Enums,[color=green][color=darkred]
              > > > but they're slower to access.)
              > > >
              > > > > Say I have a couple hundred uses of this enumeration at the same[/color][/color][/color]
              time[color=blue][color=green][color=darkred]
              > > > > (Citrix server for a Statewide application), would I have a couple[/color]
              > > hundred[color=darkred]
              > > > > or more instances of this enum class in memory?
              > > >
              > > > You'll have copies of an integer (or perhaps byte, long, etc.)[/color][/color]
              > throughout[color=green][color=darkred]
              > > > memory and on the stack. (Although to highlight the magnitude of this
              > > > memory consumption, observe that my PDA can hold up to 16 million
              > > > enums; hopefully the server for your app is larger than my PDA.)
              > > >
              > > > : :
              > > > > public enum FavoriteSport
              > > > > Baseball
              > > > > Soccer
              > > > > end enum
              > > > >
              > > > > Public Sub PrintFavorite(B yVal intFavorite as FavoriteSport)
              > > >
              > > > 4 bytes are pushed onto the stack to contain intFavorite, and the
              > > > value of this argument has its value copied to it from the calling[/color][/color][/color]
              Sub.[color=blue][color=green][color=darkred]
              > > >
              > > > > Select Case intFavorite ...
              > > > > 'if Baseball print "I Love baseball" , etc
              > > > > End Sub
              > > > >
              > > > > 'calling proc
              > > > > PrintFavorite(F avoriteSport.So ccer)
              > > >
              > > > As this is passing a constant argument, I believe the 4 bytes are[/color][/color][/color]
              pushed[color=blue][color=green][color=darkred]
              > > > directly onto the stack of the called Sub. Contrast this to,
              > > >
              > > > Dim sport As FavoriteSport = FavoriteSport.S occer
              > > > PrintFavorite( sport)
              > > >
              > > > Which has a 4-byte copy of the Enum in the calling Sub, and pushes a[/color]
              > > 4-byte[color=darkred]
              > > > copy of the Enum to the called Sub. Hence, here memory consumption is
              > > > 8-bytes.
              > > >
              > > > Also observe that both Enums are stored on the stack automatically,[/color][/color][/color]
              and[color=blue][color=green]
              > > when[color=darkred]
              > > > each Sub ends, the stack rolls back and the memory consumed by these[/color][/color]
              > Enums[color=green][color=darkred]
              > > > are returned to the system W/O requiring a garbage collection.
              > > >
              > > > Their drawback, if any, is all the copying that takes place. But, it[/color]
              > > doesn't get[color=darkred]
              > > > any faster than copying four-bytes at a time (at least on 32-bit[/color]
              > > machines), so[color=darkred]
              > > > this is ordinarily insignificant.
              > > >
              > > >
              > > > Derek Harmon
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              Working...