Python cache 内存泄漏问题
@functools.cache 函数装饰器在一些特殊情况的时候会影响变量引用计数器的计数,从而导致内存泄漏。比如:@functools.cache和@@functools.property一起使用的时候,或者更复杂的嵌套引用
1 from functools import wraps 2 3 4 class CacheManager: 5 def __init__(self): 6 self._cache = {} 7 8 def get_cache_obj(self, key): 9 """获取缓存对象""" 10 return self._cache.get(key) 11 12 def add_cache_obj(self, key, obj): 13 """添加缓存对象""" 14 self._cache[key] = obj 15 return obj 16 17 def __del__(self): 18 """清除所有缓存对象""" 19 # print(f"CacheManager del") 20 for k, v in self._cache.items(): 21 # print(f"CacheManager del:{k=}") 22 del v 23 self._cache.clear() 24 del self._cache 25 26 @classmethod 27 def cache_result(cls, func): 28 """ 29 装饰器:缓存结果,支持参数作为缓存的唯一键。 30 """ 31 32 @wraps(func) 33 def wrapper(instance, *args, **kwargs): 34 # 根据函数名和参数构建唯一缓存键 35 key = f"{func.__name__}:{args}:{kwargs}" 36 37 # 检查是否已缓存 38 cache_obj = instance.cache_manager.get_cache_obj(key) 39 if cache_obj is not None: 40 return cache_obj 41 42 # 缓存中没有对象,执行函数并缓存结果 43 result = func(instance, *args, **kwargs) 44 instance.cache_manager.add_cache_obj(key, result) 45 return result 46 47 return wrapper
测试demo
1 from base.utils.tools import CacheManager 2 3 4 class MyClass: 5 def __init__(self): 6 self.cache_manager = CacheManager() 7 8 def __del__(self): 9 print("MyClass.__del__") 10 11 @property 12 @CacheManager.cache_result 13 def oms_user(self): 14 # 该代码只在缓存不存在时执行 15 return [1, 2, 3] 16 17 18 def test_cache(): 19 m = MyClass() 20 print(id(m.oms_user)) 21 print(id(m.oms_user))
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
2021-11-15 PostgreSQL 系统表
2021-11-15 PostgreSQL常用命令
2021-11-15 Docker 好用的镜像