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

  • Home
  • SEARCH
  • 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 8821917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:58:20+00:00 2026-06-14T05:58:20+00:00

I have a dictionary object with about 60,000 keys that I cache and access

  • 0

I have a dictionary object with about 60,000 keys that I cache and access in my Django view. The view provides basic search functionality where I look for a search term in the dictionary like so:

projects_map = cache.get('projects_map')
projects_map.get('search term')

However, just grabbing the cached object (in line 1) causes a a giant spike in memory usage on the server – upwards of 100MBs sometimes – and the memory isn’t released even after the values are returned and the template rendered.

How can I keep the memory from jacking up like this? Also, I’ve tried explicitly deleting the object after I grab the value but even that doesn’t release the memory spike.

Any help is greatly appreciated.

Update: Solution I ultimately implemented

I decided to implement my own indexing table in which I store the keys and their pickled value. Now, instead of using get() on a dictionary, I use:

ProjectsIndex.objects.get(index_key=<search term>)

and unpickle the value. This seems to take care of the memory issue as I’m no longer loading a giant object into memory. It adds another small query to the page but that’s about it. Seems to be the perfect solution…for now.

  • 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-06-14T05:58:22+00:00Added an answer on June 14, 2026 at 5:58 am

    ..what about using some appropriate service for caching, such as redis or memcached instead of loading the huge object in memory python-side? This way, you’ll even have the ability to scale on extra machines, should the dictionary grow more..

    Anyways, the 100MB memory contain all the data + hash index + misc. overhead; I noticed myself the other day that many times memory doesn’t get deallocated until you quit the Python process (I filled up couple gigs of memory from the Python interpreter, loading a huge json object.. :)); it would be interesting if anybody has a solution for that..

    Update: caching with very few memory

    Your options with only 512MB ram are:

    • Use redis, and have a look here http://redis.io/topics/memory-optimization (but I suspect 512MB isn’t enough, even optimizing)
    • Use a separate machine (or a cluster of, since both memcached and redis support sharding) with way more ram to keep the cache
    • Use the database cache backend, much slower but less memory-consuming, as it saves everything on the disk
    • Use filesystem cache (although I don’t see the point of preferring this over database cache)

    and, in the latter two cases, try splitting up your objects, so that you never retrieve megabytes of objects from the cache at once.

    Update: lazy dict spanning over multiple cache keys

    You can replace your cached dict with something like this; this way, you can continue treating it as you would with a normal dictionary, but data will be loaded from cache only when you really need it.

    from django.core.cache import cache
    from UserDict import DictMixin
    
    class LazyCachedDict(DictMixin):
        def __init__(self, key_prefix):
            self.key_prefix = key_prefix
    
        def __getitem__(self, name):
            return cache.get('%s:%s' % (self.key_prefix, name))
    
        def __setitem__(self, name, value):
            return cache.set('%s:%s' % (self.key_prefix, name), value)
    
        def __delitem__(self, name):
            return cache.delete('%s:%s' % (self.key_prefix, name))
    
        def has_key(self, name):
            return cache.has_key(name)
    
        def keys():
            ## Just fill the gap, as the cache object doesn't provide
            ## a method to list cache keys..
            return []
    

    And then replace this:

    projects_map = cache.get('projects_map')
    projects_map.get('search term')
    

    with:

    projects_map = LazyCachedDict('projects_map')
    projects_map.get('search term')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a big dictionary object that has several key value pairs (about 16),
I have a Dictionary object that is formed using a double as its key.
I have such dictionary Dictionary<string, object> , dictionary holds string keys and objects as
I have a simple Dictionary(of String, Object) that I need to iterate through and
I have a C#-Application that stores data from a TextFile in a Dictionary-Object. The
I have dictionary of about 20,000 objects The key is a string representation of
I have a dictionary. Dictionary<YMD, object> cache = new Dictionary<YMD, object>(); The YMD class
I have a dictionary object which i would like to encrypt, then put it
Possible Duplicate: Modifying .NET Dictionary while Enumerating through it I have a dictionary object
in runtime i will have a dynamic dictionary object return to me e.g var

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.