mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-07 00:16:57 +08:00
remove gc
This commit is contained in:
+105
-16
@@ -104,14 +104,37 @@ class MemoryHelper(metaclass=Singleton):
|
|||||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||||
snapshot_file = self._memory_snapshot_dir / f"memory_snapshot_{timestamp}.txt"
|
snapshot_file = self._memory_snapshot_dir / f"memory_snapshot_{timestamp}.txt"
|
||||||
|
|
||||||
|
# 获取系统内存使用情况
|
||||||
|
memory_usage = psutil.Process().memory_info().rss
|
||||||
|
|
||||||
|
logger.info(f"开始创建内存快照: {snapshot_file}")
|
||||||
|
|
||||||
|
# 第一步:写入基本信息和对象类型统计
|
||||||
|
self._write_basic_info(snapshot_file, memory_usage)
|
||||||
|
|
||||||
|
# 第二步:分析并写入类实例内存使用情况
|
||||||
|
self._append_class_analysis(snapshot_file)
|
||||||
|
|
||||||
|
# 第三步:分析并写入大内存变量详情
|
||||||
|
self._append_variable_analysis(snapshot_file)
|
||||||
|
|
||||||
|
logger.info(f"内存快照已保存: {snapshot_file}, 当前内存使用: {memory_usage / 1024 / 1024:.2f} MB")
|
||||||
|
|
||||||
|
# 清理过期的快照文件(保留最近30个)
|
||||||
|
self._cleanup_old_snapshots()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"创建内存快照失败: {e}")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _write_basic_info(snapshot_file, memory_usage):
|
||||||
|
"""
|
||||||
|
写入基本信息和对象类型统计
|
||||||
|
"""
|
||||||
# 获取当前进程的内存使用情况
|
# 获取当前进程的内存使用情况
|
||||||
all_objects = muppy.get_objects()
|
all_objects = muppy.get_objects()
|
||||||
sum1 = summary.summarize(all_objects)
|
sum1 = summary.summarize(all_objects)
|
||||||
|
|
||||||
# 获取系统内存使用情况
|
|
||||||
memory_usage = psutil.Process().memory_info().rss
|
|
||||||
|
|
||||||
# 写入内存快照文件
|
|
||||||
with open(snapshot_file, 'w', encoding='utf-8') as f:
|
with open(snapshot_file, 'w', encoding='utf-8') as f:
|
||||||
f.write(f"内存快照时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
|
f.write(f"内存快照时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
|
||||||
f.write(f"当前进程内存使用: {memory_usage / 1024 / 1024:.2f} MB\n")
|
f.write(f"当前进程内存使用: {memory_usage / 1024 / 1024:.2f} MB\n")
|
||||||
@@ -123,45 +146,111 @@ class MemoryHelper(metaclass=Singleton):
|
|||||||
for line in summary.format_(sum1):
|
for line in summary.format_(sum1):
|
||||||
f.write(line + "\n")
|
f.write(line + "\n")
|
||||||
|
|
||||||
# 添加详细的类实例内存使用情况
|
# 立即刷新到磁盘
|
||||||
|
f.flush()
|
||||||
|
|
||||||
|
logger.debug("基本信息已写入快照文件")
|
||||||
|
|
||||||
|
def _append_class_analysis(self, snapshot_file):
|
||||||
|
"""
|
||||||
|
分析并追加类实例内存使用情况
|
||||||
|
"""
|
||||||
|
with open(snapshot_file, 'a', encoding='utf-8') as f:
|
||||||
f.write("\n" + "=" * 80 + "\n")
|
f.write("\n" + "=" * 80 + "\n")
|
||||||
f.write("类实例内存使用情况 (按内存大小排序):\n")
|
f.write("类实例内存使用情况 (按内存大小排序):\n")
|
||||||
f.write("-" * 80 + "\n")
|
f.write("-" * 80 + "\n")
|
||||||
|
f.write("正在分析中...\n")
|
||||||
|
# 立即刷新,让用户知道这部分开始了
|
||||||
|
f.flush()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
logger.debug("开始分析类实例内存使用情况")
|
||||||
class_objects = self._get_class_memory_usage()
|
class_objects = self._get_class_memory_usage()
|
||||||
|
|
||||||
|
# 重新打开文件,移除"正在分析中..."并写入实际结果
|
||||||
|
with open(snapshot_file, 'r', encoding='utf-8') as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
# 替换"正在分析中..."
|
||||||
|
content = content.replace("正在分析中...\n", "")
|
||||||
|
|
||||||
|
with open(snapshot_file, 'w', encoding='utf-8') as f:
|
||||||
|
f.write(content)
|
||||||
|
|
||||||
if class_objects:
|
if class_objects:
|
||||||
for i, class_info in enumerate(class_objects[:50], 1): # 只显示前50个类
|
# 只显示前100个类
|
||||||
f.write(f"{i:3d}. {class_info['name']:<50} {class_info['size_mb']:>8.2f} MB ({class_info['count']} 个实例)\n")
|
for i, class_info in enumerate(class_objects[:100], 1):
|
||||||
|
f.write(f"{i:3d}. {class_info['name']:<50} "
|
||||||
|
f"{class_info['size_mb']:>8.2f} MB ({class_info['count']} 个实例)\n")
|
||||||
else:
|
else:
|
||||||
f.write("未找到有效的类实例信息\n")
|
f.write("未找到有效的类实例信息\n")
|
||||||
|
|
||||||
|
f.flush()
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"获取类实例信息失败: {e}")
|
logger.error(f"获取类实例信息失败: {e}")
|
||||||
f.write(f"获取类实例信息失败: {e}\n")
|
|
||||||
|
|
||||||
# 添加详细的变量内存使用情况
|
# 即使出错也要更新文件
|
||||||
|
with open(snapshot_file, 'r', encoding='utf-8') as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
content = content.replace("正在分析中...\n", f"获取类实例信息失败: {e}\n")
|
||||||
|
|
||||||
|
with open(snapshot_file, 'w', encoding='utf-8') as f:
|
||||||
|
f.write(content)
|
||||||
|
f.flush()
|
||||||
|
|
||||||
|
logger.debug("类实例分析已完成并写入")
|
||||||
|
|
||||||
|
def _append_variable_analysis(self, snapshot_file):
|
||||||
|
"""
|
||||||
|
分析并追加大内存变量详情
|
||||||
|
"""
|
||||||
|
with open(snapshot_file, 'a', encoding='utf-8') as f:
|
||||||
f.write("\n" + "=" * 80 + "\n")
|
f.write("\n" + "=" * 80 + "\n")
|
||||||
f.write("大内存变量详情 (前100个):\n")
|
f.write("大内存变量详情 (前100个):\n")
|
||||||
f.write("-" * 80 + "\n")
|
f.write("-" * 80 + "\n")
|
||||||
|
f.write("正在分析中...\n")
|
||||||
|
# 立即刷新,让用户知道这部分开始了
|
||||||
|
f.flush()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
logger.debug("开始分析大内存变量")
|
||||||
large_variables = self._get_large_variables(100)
|
large_variables = self._get_large_variables(100)
|
||||||
|
|
||||||
|
# 重新打开文件,移除"正在分析中..."并写入实际结果
|
||||||
|
with open(snapshot_file, 'r', encoding='utf-8') as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
# 替换最后的"正在分析中..."
|
||||||
|
content = content.replace("正在分析中...\n", "")
|
||||||
|
|
||||||
|
with open(snapshot_file, 'w', encoding='utf-8') as f:
|
||||||
|
f.write(content)
|
||||||
|
|
||||||
if large_variables:
|
if large_variables:
|
||||||
for i, var_info in enumerate(large_variables, 1):
|
for i, var_info in enumerate(large_variables, 1):
|
||||||
f.write(f"{i:3d}. {var_info['name']:<30} {var_info['type']:<15} {var_info['size_mb']:>8.2f} MB\n")
|
f.write(
|
||||||
|
f"{i:3d}. {var_info['name']:<30} {var_info['type']:<15} {var_info['size_mb']:>8.2f} MB\n")
|
||||||
else:
|
else:
|
||||||
f.write("未找到大内存变量\n")
|
f.write("未找到大内存变量\n")
|
||||||
|
|
||||||
|
f.flush()
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"获取大内存变量信息失败: {e}")
|
logger.error(f"获取大内存变量信息失败: {e}")
|
||||||
f.write(f"获取变量信息失败: {e}\n")
|
|
||||||
|
|
||||||
logger.info(f"内存快照已保存: {snapshot_file}, 当前内存使用: {memory_usage / 1024 / 1024:.2f} MB")
|
# 即使出错也要更新文件
|
||||||
|
with open(snapshot_file, 'r', encoding='utf-8') as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
# 清理过期的快照文件(保留最近30个)
|
content = content.replace("正在分析中...\n", f"获取变量信息失败: {e}\n")
|
||||||
self._cleanup_old_snapshots()
|
|
||||||
|
|
||||||
except Exception as e:
|
with open(snapshot_file, 'w', encoding='utf-8') as f:
|
||||||
logger.error(f"创建内存快照失败: {e}")
|
f.write(content)
|
||||||
|
f.flush()
|
||||||
|
|
||||||
|
logger.debug("大内存变量分析已完成并写入")
|
||||||
|
|
||||||
def _cleanup_old_snapshots(self):
|
def _cleanup_old_snapshots(self):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user