API Reference
clear_cache

dbzero.clear_cache()

def clear_cache()

The clear_cache method manually evicts all objects from the in-memory cache. While dbzero automatically manages memory and removes least-recently-used objects when the cache is full, this method allows you to force a complete cache cleanup on demand.

This is useful in memory-constrained environments or after completing a resource-intensive task, allowing you to immediately release memory back to the operating system.


Parameters

This method takes no parameters.


Returns

This method does not return any value.


Side Effects

  • Frees Memory: All data associated with dbzero objects is removed from the in-memory cache, reducing the application's memory footprint.
  • Performance Impact: Subsequent access to any object's attributes will be slightly slower for the first time, as dbzero will need to reload its data from the backing storage file.

Example

Freeing Memory After a Large Operation

You can use clear_cache to clean up after a temporary, memory-intensive task is finished.

# Perform a large task that populates the cache
print(f"Cache usage before: {db0.get_cache_stats()['size']} bytes")
 
for i in range(10000):
    # Each object created is added to the cache
    _ = DataPoint(value=i, metadata="temporary data")
 
print(f"Cache usage during task: {db0.get_cache_stats()['size']} bytes")
 
# Once the task is done, clear the cache to free up memory
db0.clear_cache()
 
print(f"Cache usage after clearing: {db0.get_cache_stats()['size']} bytes")
ℹ️

Clearing the cache does not delete your objects or their data from the dbzero file. It only removes them from the fast, in-memory cache. The data remains safely persisted on disk and will be loaded back into memory on demand.