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 7644681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:44:57+00:00 2026-05-31T09:44:57+00:00

According to the official Python documentation for the weakref module the primary use for

  • 0

According to the official Python documentation for the weakref module the “primary use for weak references is to implement caches or mappings holding large objects,…”. So, I used a WeakValueDictionary to implement a caching mechanism for a long running function. However, as it turned out, values in the cache never stayed there until they would actually be used again, but needed to be recomputed almost every time. Since there were no strong references between accesses to the values stored in the WeakValueDictionary, the GC got rid of them (even though there was absolutely no problem with memory).

Now, how am I then supposed to use the weak reference stuff to implement a cache? If I keep strong references somewhere explicitly to keep the GC from deleting my weak references, there would be no point using a WeakValueDictionary in the first place. There should probably be some option to the GC that tells it: delete everything that has no references at all and everything with weak references only when memory is running out (or some threshold is exceeded). Is there something like that? Or is there a better strategy for this kind of cache?

  • 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-31T09:44:59+00:00Added an answer on May 31, 2026 at 9:44 am

    I’ll attempt to answer your inquiry with an example of how to use the weakref module to implement caching. We’ll keep our cache’s weak references in a weakref.WeakValueDictionary, and the strong references in a collections.deque because it has a maxlen property that controls how many objects it holds on to. Implemented in function closure style:

    import weakref, collections
    def createLRUCache(factory, maxlen=64):
        weak = weakref.WeakValueDictionary()
        strong = collections.deque(maxlen=maxlen)
    
        notFound = object()
        def fetch(key):
            value = weak.get(key, notFound)
            if value is notFound:
                weak[key] = value = factory(key)
            strong.append(value)
            return value
        return fetch
    

    The deque object will only keep the last maxlen entries, simply dropping references to the old entries once it reaches capacity. When the old entries are dropped and garbage collected by python, the WeakValueDictionary will remove those keys from the map. Hence, the combination of the two objects helps us keep only maxlen entries in our LRU cache.

    class Silly(object):
        def __init__(self, v):
            self.v = v
    
    def fib(i):
        if i > 1:
            return Silly(_fibCache(i-1).v + _fibCache(i-2).v)
        elif i: return Silly(1)
        else: return Silly(0)
    _fibCache = createLRUCache(fib)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to the official documentation , os.path is a module. Thus, what is the
Here's the information according to the official documentation : There are four different pairs
According to the official documentation , the KeyDown event on a Windows Forms control
According to the official Firebird documentation , columns containing Unicode strings (what SQL Server
According to PHP's official documentation the imagepng() function has the following signature: bool imagepng
Ok guys I'm little stacked here. According to official documentation Google says that Once
According to the official (gregorian) calendar , the week number for 29/12/2008 is 1,
According to the official release statement version 1.4 has been re-written to be compressed
according to the official docs there are two options to create parallel collections: 1)
According to the documentation: public static <T> int binarySearch(T[] a, T key, Comparator<? super

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.