Hashtable question

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

    Hashtable question

    Hi,

    I am using a hashtable as a cache. I am only allowed for the cache to take
    up a certain amount of memory.

    I think I can state how large I want the hashtable to be on creation.

    What I need is someway to monitor the size of the hashtable so that as it
    nears the maximum size I have set, I can delete some item or empty the table
    altogether.

    Can anyone tell me how I can implement this?

    Also what happens if I try to add items to the table when it has reached its
    maximum size?

    Thanks In Advance
    Macca
  • Macca

    #2
    Re: Hashtable question

    Hi Vadym,

    I will be defining my own cache manager class which will contain a
    hashtable.
    The question I have is how do i find the size of the hashtable at runtime so
    i can control its size, i.e stop it becoming full.

    Thanks,

    Macca

    "Vadym Stetsyak" wrote:
    Hello, Macca!
    >
    MHi,
    >
    MI am using a hashtable as a cache. I am only allowed for the cache to
    Mtake
    Mup a certain amount of memory.
    >
    MI think I can state how large I want the hashtable to be on creation.
    >
    MWhat I need is someway to monitor the size of the hashtable so that
    Mas it
    Mnears the maximum size I have set, I can delete some item or empty
    Mthe table
    Maltogether.
    >
    MCan anyone tell me how I can implement this?
    >
    MAlso what happens if I try to add items to the table when it has
    Mreached its
    Mmaximum size?
    >
    If you're under .NET 2.0 have a look at
    ( http://www.codeproject.com/csharp/GenericCache.asp )
    >
    Another way is to implement your own cache manager class. This class
    intenally will contain Hashtable. and will control its size.
    >
    >
    --
    Regards, Vadym Stetsyak
    www: http://vadmyst.blogspot

    Comment

    • Chris Mullins

      #3
      Re: Hashtable question

      "Macca" <Macca@discussi ons.microsoft.c omwrote >
      I am using a hashtable as a cache. I am only allowed for the cache to take
      up a certain amount of memory.
      There may be a better options for you: Weak References.

      This will allow your cache to grow without bounds so long as there's no
      memory pressure. As memory presure builds, the Garbage Collector will
      collect the Weak References and your app keeps on going.

      You can find more information at:
      Learn about weak references, which allow the .NET garbage collector to collect an object while still allowing the application to access the object.

      Represents a weak reference, which references an object while still allowing that object to be reclaimed by garbage collection.


      --
      Chris Mullns, MCSD.NET, MCPD:Enterprise




      Comment

      • martin.zarate@gmail.com

        #4
        Re: Hashtable question


        Chris Mullins wrote:
        "Macca" <Macca@discussi ons.microsoft.c omwrote >
        >
        I am using a hashtable as a cache. I am only allowed for the cache to take
        up a certain amount of memory.
        >
        There may be a better options for you: Weak References.
        >
        This will allow your cache to grow without bounds so long as there's no
        memory pressure. As memory presure builds, the Garbage Collector will
        collect the Weak References and your app keeps on going.
        >
        You can find more information at:
        Learn about weak references, which allow the .NET garbage collector to collect an object while still allowing the application to access the object.

        Represents a weak reference, which references an object while still allowing that object to be reclaimed by garbage collection.

        >
        --
        Chris Mullns, MCSD.NET, MCPD:Enterprise
        http://www.coversant.net/blogs/cmullins
        I've often contemplated this approach myself... a question though, does
        the MS hashtable implementation have some sort of container object? I
        suppose even the weak reference object must have some overhead. Either
        way - if you use this approach, I suppose you must occaisionally go
        through and prune all the empty WeakReferences out of your hashtable...
        unless you can bind an event to the Weak Reference and have it remove
        itself... but at this point we're getting into the sort of complicated
        three-star programming that is best left to C++.

        Comment

        • Chris Mullins

          #5
          Re: Hashtable question


          The standard generic dictionary uses KeyValuePair<as it's container.
          There's no way I know of to wrap this with a weak reference and have this
          "just work". You would need (as you said) some sort of monitor thread to
          prune the null references out of the Dictionary.

          Poking around the web, it looks like a few people have implemented the
          IDictionary interface on a class that is Weak Reference aware. I haven't
          used any of them, and have a healthy skepticism for people's ability to
          implement complex data structures without bugs.

          On the other hand, the Asp.Net Cache is weak reference aware, and it can be
          used in any type of application. I've used this as a cache before and had a
          good experience.

          --
          Chris Mullins, MCSD.NET, MCPD:Enterprise


          <martin.zarate@ gmail.comwrote in message
          news:1159562706 .125657.7200@k7 0g2000cwa.googl egroups.com...
          >
          Chris Mullins wrote:
          >"Macca" <Macca@discussi ons.microsoft.c omwrote >
          >>
          I am using a hashtable as a cache. I am only allowed for the cache to
          take
          up a certain amount of memory.
          >>
          >There may be a better options for you: Weak References.
          >>
          >This will allow your cache to grow without bounds so long as there's no
          >memory pressure. As memory presure builds, the Garbage Collector will
          >collect the Weak References and your app keeps on going.
          >>
          >You can find more information at:
          >http://msdn2.microsoft.com/en-us/library/ms404247.aspx
          >http://msdn2.microsoft.com/en-us/lib...reference.aspx
          >>
          >--
          >Chris Mullns, MCSD.NET, MCPD:Enterprise
          >http://www.coversant.net/blogs/cmullins
          >
          I've often contemplated this approach myself... a question though, does
          the MS hashtable implementation have some sort of container object? I
          suppose even the weak reference object must have some overhead. Either
          way - if you use this approach, I suppose you must occaisionally go
          through and prune all the empty WeakReferences out of your hashtable...
          unless you can bind an event to the Weak Reference and have it remove
          itself... but at this point we're getting into the sort of complicated
          three-star programming that is best left to C++.
          >

          Comment

          Working...