当前位置:首页>python>Python gc模块详细介绍

Python gc模块详细介绍

  • 2026-02-10 21:26:27
Python gc模块详细介绍

1. 创始时间与作者

  • 创始时间Python的垃圾回收机制最早在 Python 1.5 版本中引入(约1998年),最初的引用计数机制更早存在于Python诞生之初(1991年)

  • 核心开发者

    • Guido van Rossum:Python创始人,设计了最初的引用计数机制

    • Neil Schemenauer:实现了Python的循环垃圾收集器(Python 2.0)

    • Martin von Löwis:改进和优化了GC系统

    • Antoine Pitrou:为Python 3.4重新设计了GC,引入了分代收集

    • Python核心团队:持续维护和优化

  • 项目定位Python内存管理核心组件,自动管理内存分配和回收,防止内存泄漏

2. 官方资源

  • Python文档地址https://docs.python.org/3/library/gc.html

  • 源代码位置Python源码中的 Modules/gcmodule.c 和 Include/internal/pycore_gc.h

  • 相关PEP文档

    • PEP 349:允许GC影响对象复活

    • PEP 442:安全的对象终结

    • PEP 523:GC性能改进

  • GitHub讨论区https://github.com/python/cpython/issues?q=label%3Agc

3. 核心功能

4. 应用场景

1. 内存泄漏检测

import gcimport tracemallocimport weakrefclass LeakyObject:def __init__(selfname):self.name = nameself.data = [for in range(10000)]  # 占用内存def __del__(self):print(f"删除对象: {self.name}")def detect_memory_leak():"""检测内存泄漏"""# 启用内存跟踪tracemalloc.start()# 创建对象但保留引用objects = []for in range(100):obj = LeakyObject(f"obj_{i}")objects.append(obj)  # 保持引用# 检查GC统计print("GC统计:")print(f"第0代: {gc.get_count()[0]}")print(f"第1代: {gc.get_count()[1]}")print(f"第2代: {gc.get_count()[2]}")# 手动触发垃圾回收collected = gc.collect()print(f"回收的对象数: {collected}")# 获取内存快照snapshot = tracemalloc.take_snapshot()top_stats = snapshot.statistics('lineno')[:5]print("\n内存使用前5:")for stat in top_stats:print(stat)tracemalloc.stop()# 运行检测detect_memory_leak()

2. 循环引用处理

import gcimport sysclass Node:"""循环引用的节点类"""def __init__(selfvalue):self.value = valueself.parent = Noneself.children = []def add_child(selfchild):self.children.append(child)child.parent = selfdef __del__(self):print(f"删除节点: {self.value}")def create_cycle():"""创建循环引用"""root = Node("根节点")child1 = Node("子节点1")child2 = Node("子节点2")root.add_child(child1)root.add_child(child2)# 创建循环引用child2.add_child(root)  # 子节点引用父节点return rootdef manage_cycles():"""管理循环引用"""print("创建循环引用对象...")obj = create_cycle()# 查看引用计数print(f"根节点引用计数: {sys.getrefcount(obj)}")# 删除引用del obj# 检查垃圾回收print(f"GC阈值: {gc.get_threshold()}")print(f"GC计数: {gc.get_count()}")# 强制垃圾回收collected = gc.collect()print(f"回收的循环引用对象: {collected}")# 运行示例manage_cycles()

3. 性能敏感应用优化

import gcimport timeimport numpy as npclass HighPerformanceProcessor:"""高性能处理器,需要禁用GC"""def __init__(self):self.buffer = []self.gc_was_enabled = gc.isenabled()def process_data(selfdata_size):"""处理大量数据"""# 在处理前禁用GCif self.gc_was_enabled:gc.disable()try:start_time = time.time()# 处理大量数据results = []for in range(data_size):# 模拟数据处理data = np.random.randn(1000100)processed = data.mean(axis=0)results.append(processed)# 定期手动触发GCif i%1000 == and i>0:gc.collect()end_time = time.time()print(f"处理 {data_size} 个数据点耗时: {end_time - start_time:.2f}秒")return resultsfinally:# 恢复GC状态if self.gc_was_enabled:gc.enable()def benchmark_gc_impact():"""基准测试GC影响"""processor = HighPerformanceProcessor()print("禁用GC处理...")results1 = processor.process_data(10000)print("\n启用GC处理...")gc.enable()start_time = time.time()results2 = []for in range(10000):data = np.random.randn(1000100)results2.append(data.mean(axis=0))end_time = time.time()print(f"启用GC处理耗时: {end_time - start_time:.2f}秒")# 运行基准测试benchmark_gc_impact()

4. 弱引用管理

import gcimport weakrefclass CacheManager:"""使用弱引用的缓存管理器"""def __init__(self):self._cache = weakref.WeakValueDictionary()self._callbacks = []def add_to_cache(selfkeyobj):"""添加对象到缓存(弱引用)"""self._cache[key] = obj# 添加对象被回收时的回调def cleanup(ref):print(f"对象 {key} 已被垃圾回收")self._callbacks.append(weakref.ref(objcleanup))def get_from_cache(selfkey):"""从缓存获取对象"""return self._cache.get(key)def cache_stats(self):"""获取缓存统计"""print(f"缓存大小: {len(self._cache)}")print(f"回调数量: {len(self._callbacks)}")def demonstrate_weak_refs():"""演示弱引用"""cache = CacheManager()# 创建大对象class LargeObject:def __init__(selfid):self.id = idself.data = [for in range(10000)]def __del__(self):print(f"LargeObject {self.id} 被销毁")# 添加到缓存obj1 = LargeObject("obj1")cache.add_to_cache("key1"obj1)print("添加对象后:")cache.cache_stats()# 删除强引用del obj1print("\n删除强引用后:")# 强制垃圾回收gc.collect()print("\n垃圾回收后:")cache.cache_stats()# 尝试获取(应该失败)retrieved = cache.get_from_cache("key1")print(f"从缓存获取: {retrieved}")# 运行演示demonstrate_weak_refs()

5. 底层逻辑与技术原理

Python GC系统架构

关键技术实现

  1. 引用计数机制

    // 简化版引用计数实现type def struct {PyObject_HEADlongob_refcnt;          // 引用计数PyTypeObject*ob_type;PyObject;// 增加引用#define Py_INCREF(op) ((op)->ob_refcnt++)// 减少引用#define Py_DECREF(op) \if (--(op)->ob_refcnt == 0) \_Py_Dealloc(op)  // 立即回收
  2. 分代垃圾回收算法

    # Python中的分代阈值# 默认阈值 (700, 10, 10)# 第0代: 分配对象数 - 释放对象数 > 700 时触发GC# 第1代: 第0代GC执行10次后触发第1代GC# 第2代: 第1代GC执行10次后触发第2代GC# Python实现的核心逻辑def collect_generations():# 从最老的一代开始检查for generation in reversed(range(NUM_GENERATIONS)):if gc.should_collect(generation):n = gc.collect(generation)if generation<NUM_GENERATIONS-1:# 年轻一代存活的对象晋升gc.promote_young_objects(generation)break
  3. 循环引用检测算法

    • 标记-清除算法

    • 三色标记法

      # 三色标记状态WHITE = 0# 未访问GRAY = 1# 访问中BLACK = 2# 已访问def mark_from_root(root):# 将所有根对象标记为灰色gray_list = [root]while gray_list:obj = gray_list.pop()# 标记为黑色obj.color = BLACK# 遍历所有引用for ref in get_references(obj):if ref.color == WHITE:ref.color = GRAYgray_list.append(ref)
    1. 从根对象(全局变量、栈变量等)开始遍历

    2. 标记所有可达对象

    3. 清除所有未标记对象

  4. 弱引用处理

    • 弱引用不增加引用计数

    • GC时自动清理弱引用目标已被回收的弱引用

    • 支持弱引用字典、弱引用集合等数据结构


6. 安装与配置

基础说明

gc是Python标准库的一部分,无需单独安装

# 验证gc模块python -c "import gc; print(f'GC已启用: {gc.isenabled()}')"

Python版本GC特性

Python版本GC特性重要改进
Python 1.5引入循环GC基本标记-清除算法
Python 2.0分代GC三色标记法
Python 2.1弱引用支持weakref模块
Python 3.0引用计数优化消除部分引用计数
Python 3.4改进的分代GCPEP 442安全终结
Python 3.7GC性能优化减少停顿时间
Python 3.11更快的GC专用GC分配器

环境变量配置

# 设置GC环境变量export PYTHONGC=enable    # 启用GC(默认)export PYTHONGC=disable   # 禁用GCexport PYTHONGC=debug     # 调试模式# 设置GC阈值export PYTHONGCENABLE=700,10,10  # 第0,1,2代阈值# 设置GC调试标志export PYTHONGCDEBUG=leak        # 只报告泄漏export PYTHONGCDEBUG=collect     # 报告所有收集export PYTHONGCDEBUG=stats       # 显示统计信息

运行时配置

import gcimport osdef configure_gc():"""运行时配置GC"""# 1. 检查当前配置print(f"GC已启用: {gc.isenabled()}")print(f"当前阈值: {gc.get_threshold()}")print(f"当前计数: {gc.get_count()}")# 2. 设置自定义阈值# 格式: (第0代阈值, 第1代阈值, 第2代阈值)gc.set_threshold(10001515)  # 更频繁的GCprint(f"新阈值: {gc.get_threshold()}")# 3. 启用调试gc.set_debug(gc.DEBUG_STATS|gc.DEBUG_COLLECTABLE)# 4. 禁用自动GC(手动控制)# gc.disable()# 5. 设置GC触发回调def gc_callback(phaseinfo):if phase == "start":print(f"GC开始: {info}")elif phase == "stop":print(f"GC结束: {info}")# Python 3.10+ 支持if hasattr(gc'callbacks'):gc.callbacks.append(gc_callback)# 应用配置configure_gc()

7. 性能优化与调优

GC性能基准测试

import gcimport timeimport psutilimport osclass GCBenchmark:"""GC性能基准测试类"""@staticmethoddef memory_usage():"""获取当前内存使用"""process = psutil.Process(os.getpid())return process.memory_info().rss/1024/1024# MB@staticmethoddef benchmark_allocation(num_objects=100000):"""测试对象分配性能"""print(f"分配 {num_objects} 个对象...")# 禁用GCgc_was_enabled = gc.isenabled()if gc_was_enabled:gc.disable()start_mem = GCBenchmark.memory_usage()start_time = time.time()# 创建大量对象objects = []for in range(num_objects):obj = {'id'i,'data''x'*100,  # 100字节数据'nested': [for in range(10)]            }objects.append(obj)end_time = time.time()end_mem = GCBenchmark.memory_usage()# 恢复GC状态if gc_was_enabled:gc.enable()print(f"分配时间: {end_time - start_time:.4f}秒")print(f"内存增加: {end_mem - start_mem:.2f}MB")return objects@staticmethoddef benchmark_collection():"""测试垃圾回收性能"""print("\n测试垃圾回收性能...")# 创建一些对象objects = GCBenchmark.benchmark_allocation(50000)# 测量GC时间start_time = time.time()collected = gc.collect()end_time = time.time()print(f"GC回收对象: {collected}")print(f"GC耗时: {end_time - start_time:.4f}秒")# 获取GC统计stats = gc.get_stats()print("\nGC统计:")for genstat in enumerate(stats):print(f"第{gen}代: {stat}")# 运行基准测试if __name__ == "__main__":benchmark = GCBenchmark()benchmark.benchmark_collection()

最佳实践优化

import gcimport numpy as npfrom collections import defaultdictclass OptimizedMemoryManager:"""优化内存管理的实用类"""@staticmethoddef reduce_memory_fragmentation():"""减少内存碎片"""# 1. 使用对象池object_pool = defaultdict(list)def get_object(cls*args):"""从对象池获取或创建对象"""if object_pool[cls]:obj = object_pool[cls].pop()obj.__init__(*args)  # 重新初始化return objreturn cls(*args)def return_object(obj):"""返回对象到池中"""object_pool[type(obj)].append(obj)# 2. 预分配内存def preallocate_list(size):"""预分配列表减少重新分配"""return [None*size# 3. 使用数组代替列表(对于数值数据)def use_array_for_numbers(data):"""使用数组减少内存使用"""return np.array(datadtype=np.float32)return {'get_object'get_object,'return_object'return_object,'preallocate_list'preallocate_list,'use_array_for_numbers'use_array_for_numbers        }@staticmethoddef optimize_gc_settings(application_type):"""根据应用类型优化GC设置"""presets = {'web_server': {'threshold': (10001010),  # 频繁小规模GC'auto_enable'True,'debug'0            },'batch_processing': {'threshold': (10000100100),  # 减少GC频率'auto_enable'False,  # 手动控制'debug'0            },'data_science': {'threshold': (50005050),'auto_enable'True,'debug'gc.DEBUG_STATS            },'game_development': {'threshold': (7001010),  # 默认值'auto_enable'True,'debug'gc.DEBUG_SAVEALL            }        }preset = presets.get(application_typepresets['web_server'])# 应用设置gc.set_threshold(*preset['threshold'])if not preset['auto_enable']:gc.disable()gc.set_debug(preset['debug'])print(f"应用 {application_type} 的GC设置已配置")return preset# 使用优化器optimizer = OptimizedMemoryManager()# 配置为Web服务器optimizer.optimize_gc_settings('web_server')# 获取内存管理工具tools = optimizer.reduce_memory_fragmentation()# 使用对象池class DatabaseConnection:def __init__(selfdb_name):self.db_name = db_nameself.connection = Nonedef connect(self):print(f"连接到 {self.db_name}")# 使用对象池获取对象conn = tools['get_object'](DatabaseConnection'my_database')conn.connect()# 使用后返回对象池tools['return_object'](conn)

8. 调试与问题排查

内存泄漏检测

import gcimport objgraphimport tracemallocimport sysclass MemoryLeakDetector:"""内存泄漏检测器"""def __init__(self):self.snapshots = []self.object_types = {}tracemalloc.start()def take_snapshot(selflabel=""):"""获取内存快照"""snapshot = tracemalloc.take_snapshot()self.snapshots.append((labelsnapshot))# 分析对象类型gc.collect()self.object_types[label] = objgraph.most_common_types(limit=10)print(f"\n=== 快照: {label} ===")print(f"活跃对象数: {len(gc.get_objects())}")# 显示前5个对象类型for obj_typecount in self.object_types[label][:5]:print(f"  {obj_type}: {count}")def compare_snapshots(selfsnapshot1snapshot2):"""比较两个快照"""print(f"\n=== 比较 {snapshot1[0]} 和 {snapshot2[0]} ===")stats = snapshot2[1].compare_to(snapshot1[1], 'lineno')print("内存增加前5:")for stat in stats[:5]:print(f"  {stat}")print("\n对象类型变化:")old_types = dict(self.object_types[snapshot1[0]][:10])new_types = dict(self.object_types[snapshot2[0]][:10])for obj_type in set(old_typesset(new_types):old_count = old_types.get(obj_type0)new_count = new_types.get(obj_type0)if new_count>old_count:print(f"  {obj_type}: +{new_count - old_count}")def detect_circular_references(self):"""检测循环引用"""print("\n=== 检测循环引用 ===")# 获取可能包含循环引用的对象garbage = gc.garbageprint(f"未回收的垃圾对象数: {len(garbage)}")if garbage:print("未回收的对象类型:")for obj in garbage[:5]:  # 显示前5个print(f"  {type(obj).__name__}")# 显示循环引用图objgraph.show_backrefs(garbage[:3], max_depth=5,filename='circular_refs.png'            )print("循环引用图已保存到 circular_refs.png")def monitor_memory_growth(selfiterations=10):"""监控内存增长"""print(f"=== 监控内存增长 ({iterations}次迭代) ===")baseline = tracemalloc.take_snapshot()for in range(iterations):# 创建一些对象data = [{'id'j'value'j*2}  for in range(1000)]# 获取快照current = tracemalloc.take_snapshot()stats = current.compare_to(baseline'lineno')if stats:print(f"\n迭代 {i+1}:")print(f"  总内存增长: {stats[0].size / 1024:.2f} KB")# 更新基线baseline = current# 使用内存泄漏检测器def example_leaky_function():"""有内存泄漏的函数"""cache = []def process_data():# 模拟数据处理,但会泄漏内存data = [for in range(10000)]processed = [x*for in data]# 错误:将数据添加到缓存但从不清理cache.append(processed)return len(processed)return process_data# 运行检测detector = MemoryLeakDetector()# 初始快照detector.take_snapshot("初始状态")# 运行可能有泄漏的函数leaky_func = example_leaky_function()for in range(5):result = leaky_func()# 再次快照detector.take_snapshot("运行后")# 比较快照detector.compare_snapshots(detector.snapshots[0],detector.snapshots[1])# 检测循环引用detector.detect_circular_references()# 监控内存增长detector.monitor_memory_growth(3)# 清理tracemalloc.stop()

GC调试标志

import gcdef demonstrate_gc_debug_flags():"""演示GC调试标志"""debug_flags = {'DEBUG_STATS'gc.DEBUG_STATS,'DEBUG_COLLECTABLE'gc.DEBUG_COLLECTABLE,'DEBUG_UNCOLLECTABLE'gc.DEBUG_UNCOLLECTABLE,'DEBUG_SAVEALL'gc.DEBUG_SAVEALL,'DEBUG_LEAK'gc.DEBUG_LEAK,'DEBUG_INSTANCES'gc.DEBUG_INSTANCES,'DEBUG_OBJECTS'gc.DEBUG_OBJECTS,    }print("GC调试标志:")print("="*50)for nameflag in debug_flags.items():print(f"{name:20} = {flag:08b} ({flag})")print("\n组合使用示例:")print("-"*50)# 常用组合combinations = {'基本调试'gc.DEBUG_STATS|gc.DEBUG_COLLECTABLE,'内存泄漏检测'gc.DEBUG_LEAK|gc.DEBUG_UNCOLLECTABLE,'完整调试'gc.DEBUG_STATS|gc.DEBUG_COLLECTABLE|gc.DEBUG_UNCOLLECTABLE|gc.DEBUG_INSTANCES,    }for namecombo in combinations.items():print(f"{name:20} = {combo:08b} ({combo})")# 设置并测试gc.set_debug(combo)# 创建一些垃圾class TestObject:def __init__(selfvalue):self.value = value# 创建循环引用a = TestObject(1)b = TestObject(2)a.ref = bb.ref = a# 删除引用del ab# 触发GCprint(f"  GC收集: {gc.collect()}")print()# 运行演示demonstrate_gc_debug_flags()

9. 高级主题与扩展

自定义GC策略

import gcimport threadingimport timeclass AdaptiveGCManager:"""自适应GC管理器"""def __init__(selfmin_threshold=100max_threshold=10000):self.min_threshold = min_thresholdself.max_threshold = max_thresholdself.current_threshold = 1000self.memory_history = []self.gc_times = []self.adaptive_mode = True# 启动监控线程self.monitor_thread = threading.Thread(target=self._monitor_memory,daemon=True        )self.monitor_thread.start()def _monitor_memory(self):"""监控内存使用并调整GC策略"""import psutilimport osprocess = psutil.Process(os.getpid())while self.adaptive_mode:# 获取内存使用memory_mb = process.memory_info().rss/1024/1024self.memory_history.append(memory_mb)# 保持最近100个记录if len(self.memory_history>100:self.memory_history.pop(0)# 分析内存趋势if len(self.memory_history>20:recent = self.memory_history[-10:]older = self.memory_history[-20:-10]recent_avg = sum(recent/len(recent)older_avg = sum(older/len(older)# 计算内存增长速率growth_rate = (recent_avg-older_avg/older_avg# 根据增长速率调整GC阈值if growth_rate>0.1:  # 内存快速增长# 降低阈值,更频繁GCself.current_threshold = max(self.min_threshold,int(self.current_threshold*0.8)                    )elif growth_rate<-0.05:  # 内存减少# 提高阈值,减少GC频率self.current_threshold = min(self.max_threshold,int(self.current_threshold*1.2)                    )# 应用新阈值gc.set_threshold(self.current_threshold,self.current_threshold//100,self.current_threshold//1000                )time.sleep(1)  # 每秒检查一次def profile_gc(selfoperation*args**kwargs):"""分析操作的GC性能"""gc.collect()  # 清理现有垃圾# 记录初始状态initial_count = gc.get_count()initial_stats = gc.get_stats()# 记录时间start_time = time.time()# 执行操作result = operation(*args**kwargs)# 记录结束时间end_time = time.time()# 手动触发GC以测量清理时间gc_start = time.time()collected = gc.collect()gc_end = time.time()# 收集统计final_count = gc.get_count()final_stats = gc.get_stats()# 计算指标operation_time = end_time-start_timegc_time = gc_end-gc_startprint(f"\n=== GC性能分析 ===")print(f"操作时间: {operation_time:.4f}秒")print(f"GC时间: {gc_time:.4f}秒")print(f"回收对象: {collected}")print(f"GC占比: {gc_time/operation_time*100:.2f}%")print(f"当前阈值: {self.current_threshold}")# 更新GC时间历史self.gc_times.append(gc_time)if len(self.gc_times>100:self.gc_times.pop(0)return resultdef optimize_for_workload(selfworkload_typeduration_seconds=10):"""为特定工作负载优化GC"""print(f"\n为 {workload_type} 工作负载优化GC...")# 保存原始设置original_threshold = gc.get_threshold()original_debug = gc.get_debug()try:if workload_type == 'bursty':# 突发性工作负载:降低阈值gc.set_threshold(5001010)gc.set_debug(0)print("配置:频繁小规模GC")elif workload_type == 'steady':# 稳定工作负载:提高阈值gc.set_threshold(50005050)gc.set_debug(0)print("配置:较少大规模GC")elif workload_type == 'memory_intensive':# 内存密集型:手动控制gc.disable()print("配置:禁用自动GC")elif workload_type == 'latency_sensitive':# 延迟敏感:最小化GC停顿gc.set_threshold(10000100100)gc.set_debug(gc.DEBUG_STATS)print("配置:最小化GC频率,监控统计")# 模拟工作负载self._simulate_workload(workload_typeduration_seconds)finally:# 恢复原始设置gc.set_threshold(*original_threshold)gc.set_debug(original_debug)print("恢复原始GC设置")def _simulate_workload(selfworkload_typeduration):"""模拟工作负载"""import randomend_time = time.time() +durationif workload_type == 'bursty':# 突发性:短时间内创建大量对象while time.time() <end_time:# 突发for in range(10000):data = [random.random() for in range(100)]time.sleep(0.1)elif workload_type == 'steady':# 稳定性:持续创建对象while time.time() <end_time:data = [random.random() for in range(100)]time.sleep(0.01)print(f"模拟 {workload_type} 工作负载完成")# 使用自适应GC管理器manager = AdaptiveGCManager()# 定义一个测试操作def create_lots_of_objects(count):"""创建大量对象"""objects = []for in range(count):obj = {'id'i,'data''x'*100,'nested': [for in range(10)]        }objects.append(obj)return objects# 分析操作manager.profile_gc(create_lots_of_objects10000)# 为特定工作负载优化manager.optimize_for_workload('bursty'5)# 停止自适应管理manager.adaptive_mode = False

10. 替代方案与扩展

第三方内存管理工具

工具用途特点与GC的关系
pympler内存分析对象大小跟踪,泄漏检测补充GC,提供更详细分析
objgraph对象引用图可视化对象引用关系基于GC数据生成图表
guppy3堆分析内存使用分析,对象统计与GC配合使用
memory_profiler内存分析逐行内存使用分析监控GC效果
tracemalloc内存分配跟踪Python内置内存跟踪提供GC之外的分配信息

替代GC策略

# 1. 手动内存管理(特定场景)import ctypesclass ManualMemoryBuffer:"""手动内存管理缓冲区"""def __init__(selfsize):# 使用ctypes直接分配内存self.buffer = (ctypes.c_char*size)()self.size = sizedef __del__(self):# 手动释放内存(实际上Python会处理)print(f"释放 {self.size} 字节缓冲区")# 在实际C扩展中需要手动释放# 2. 对象池模式class ObjectPool:"""对象池减少GC压力"""def __init__(selffactorymax_size=100):self.factory = factoryself.max_size = max_sizeself.pool = []def acquire(self):"""获取对象"""if self.pool:return self.pool.pop()return self.factory()def release(selfobj):"""释放对象回池"""if len(self.pool<self.max_size:self.pool.append(obj)# 3. 使用__slots__减少内存class OptimizedObject:"""使用__slots__优化内存"""__slots__ = ['x''y''z']  # 固定属性,减少内存使用def __init__(selfxyz):self.x = xself.y = yself.z = z# 比较内存使用import sysclass RegularObject:def __init__(selfxyz):self.x = xself.y = yself.z = z# 创建对象并比较大小regular = RegularObject(123)optimized = OptimizedObject(123)print(f"常规对象大小: {sys.getsizeof(regular)} 字节")print(f"优化对象大小: {sys.getsizeof(optimized)} 字节")

11. 企业级最佳实践

生产环境GC配置

import gcimport jsonimport loggingclass ProductionGCConfig:"""生产环境GC配置管理器"""def __init__(selfapp_name):self.app_name = app_nameself.logger = self._setup_logger()self.config = self._load_config()def_setup_logger(self):"""设置GC日志"""logger = logging.getLogger(f'{self.app_name}.gc')logger.setLevel(logging.INFO)handler = logging.FileHandler(f'{self.app_name}_gc.log')formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'        )handler.setFormatter(formatter)logger.addHandler(handler)return loggerdef _load_config(self):"""加载GC配置"""default_config = {'enabled'True,'threshold': [10001010],'debug_flags'0,'monitoring': {'enabled'True,'interval'60,  # 秒'metrics': ['collections''collected''uncollectable']            },'optimizations': {'disable_for_batches'True,'object_pooling'True,'use_slots'True            }        }try:with open(f'{self.app_name}_gc_config.json''r'as f:config = json.load(f)return {**default_config**config}except FileNotFoundError:return default_configdef apply_config(self):"""应用GC配置"""# 启用/禁用GCif self.config['enabled']:gc.enable()else:gc.disable()# 设置阈值thresholds = self.config['threshold']gc.set_threshold(*thresholds)# 设置调试标志gc.set_debug(self.config['debug_flags'])# 设置回调用于监控if self.config['monitoring']['enabled']:self._setup_monitoring()self.logger.info(f"GC配置已应用: {self.config}")def _setup_monitoring(self):"""设置GC监控"""import threadingdef monitor_gc():"""监控GC状态"""import timeinterval = self.config['monitoring']['interval']while True:try:# 收集GC统计stats = gc.get_stats()counts = gc.get_count()# 记录到日志self.logger.info(f"GC统计 - 计数: {counts}, "f"第0代: {stats[0]}, "f"第1代: {stats[1]}, "f"第2代: {stats[2]}"                    )# 检查内存泄漏if len(gc.garbage>0:self.logger.warning(f"检测到未回收垃圾: {len(gc.garbage)} 个对象"                        )time.sleep(interval)except Exception as e:self.logger.error(f"GC监控错误: {e}")time.sleep(interval)# 启动监控线程monitor_thread = threading.Thread(target=monitor_gcdaemon=True)monitor_thread.start()def optimize_application(self):"""优化应用程序内存使用"""optimizations = self.config['optimizations']if optimizations['disable_for_batches']:self.logger.info("启用批处理GC优化")class GCOptimizer:"""批处理GC优化器"""def __enter__(self):self.gc_was_enabled = gc.isenabled()if self.gc_was_enabled:gc.disable()return selfdef __exit__(selfexc_typeexc_valexc_tb):if self.gc_was_enabled:gc.enable()# 清理垃圾gc.collect()return GCOptimizer()return None# 生产环境使用示例def main():"""生产环境主函数"""# 初始化GC配置gc_config = ProductionGCConfig('my_web_app')gc_config.apply_config()# 使用GC优化器处理批处理with gc_config.optimize_application():# 执行批处理操作process_large_dataset()# 正常处理请求(GC自动管理)handle_requests()def process_large_dataset():"""处理大数据集"""# 模拟数据处理data = [for in range(1000000)]result = sum(data)print(f"处理结果: {result}")def handle_requests():"""处理请求"""# 模拟请求处理for in range(100):response = {'request_id'i'status''ok'}print(f"处理请求 {i}")if __name__ == "__main__":main()

总结

Python的垃圾回收系统是一个成熟、高效的内存管理机制,核心价值在于:

  1. 自动化内存管理:开发者无需手动分配和释放内存

  2. 防止内存泄漏:自动检测和回收不可达对象

  3. 高性能:引用计数提供即时回收,分代GC优化性能

  4. 可配置性:提供丰富的API进行调优和监控

技术特色

  • 引用计数与分代GC的混合策略

  • 三色标记法检测循环引用

  • 弱引用机制避免循环引用

  • 可配置的分代阈值和调试选项

安全警告

  • __del__方法可能导致对象复活和复杂情况

  • 循环引用中的__del__方法可能阻止对象回收

  • 永远避免在__del__方法中创建新引用

最佳实践

  1. 理解应用特点:根据应用类型调整GC策略

  2. 监控GC性能:使用gc.get_stats()gc.get_count()

  3. 优化内存使用:使用__slots__、对象池等减少GC压力

  4. 避免常见陷阱:小心使用__del__方法,管理循环引用

调优建议

  • Web服务:使用较频繁的GC(阈值700-1000)

  • 批处理:在批处理前后手动控制GC

  • 实时系统:考虑禁用GC或使用极大阈值

  • 内存敏感:使用对象池和内存视图

学习资源

  • 官方文档:https://docs.python.org/3/library/gc.html

  • Python源码:Modules/gcmodule.c(深入理解实现)

  • 《Python高级编程》:内存管理和优化章节

  • Python内存管理PEP文档

GC作为Python核心组件,其设计和实现经过了数十年的优化。虽然大多数情况下无需手动干预,但理解其工作原理对于编写高性能、可靠的Python应用程序至关重要。正确的GC调优可以使应用程序性能提升数倍,特别是在内存敏感的应用场景中。

最新文章

随机文章

基本 文件 流程 错误 SQL 调试
  1. 请求信息 : 2026-02-11 00:39:01 HTTP/2.0 GET : https://f.mffb.com.cn/a/474880.html
  2. 运行时间 : 0.107534s [ 吞吐率:9.30req/s ] 内存消耗:4,962.77kb 文件加载:140
  3. 缓存信息 : 0 reads,0 writes
  4. 会话信息 : SESSION_ID=3c780676e587fceb5bbe4cd6595f1ba2
  1. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/public/index.php ( 0.79 KB )
  2. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/autoload.php ( 0.17 KB )
  3. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_real.php ( 2.49 KB )
  4. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/platform_check.php ( 0.90 KB )
  5. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/ClassLoader.php ( 14.03 KB )
  6. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_static.php ( 4.90 KB )
  7. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper.php ( 8.34 KB )
  8. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/helper.php ( 2.19 KB )
  9. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/helper.php ( 1.47 KB )
  10. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/stubs/load_stubs.php ( 0.16 KB )
  11. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Exception.php ( 1.69 KB )
  12. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Facade.php ( 2.71 KB )
  13. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/deprecation-contracts/function.php ( 0.99 KB )
  14. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap.php ( 8.26 KB )
  15. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap80.php ( 9.78 KB )
  16. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/Resources/functions/dump.php ( 1.49 KB )
  17. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-dumper/src/helper.php ( 0.18 KB )
  18. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/VarDumper.php ( 4.30 KB )
  19. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/App.php ( 15.30 KB )
  20. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Container.php ( 15.76 KB )
  21. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/container/src/ContainerInterface.php ( 1.02 KB )
  22. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/provider.php ( 0.19 KB )
  23. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Http.php ( 6.04 KB )
  24. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Str.php ( 7.29 KB )
  25. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Env.php ( 4.68 KB )
  26. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/common.php ( 0.03 KB )
  27. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/helper.php ( 18.78 KB )
  28. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Config.php ( 5.54 KB )
  29. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/app.php ( 0.95 KB )
  30. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cache.php ( 0.78 KB )
  31. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/console.php ( 0.23 KB )
  32. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cookie.php ( 0.56 KB )
  33. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/database.php ( 2.48 KB )
  34. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Env.php ( 1.67 KB )
  35. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/filesystem.php ( 0.61 KB )
  36. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/lang.php ( 0.91 KB )
  37. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/log.php ( 1.35 KB )
  38. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/middleware.php ( 0.19 KB )
  39. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/route.php ( 1.89 KB )
  40. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/session.php ( 0.57 KB )
  41. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/trace.php ( 0.34 KB )
  42. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/view.php ( 0.82 KB )
  43. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/event.php ( 0.25 KB )
  44. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Event.php ( 7.67 KB )
  45. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/service.php ( 0.13 KB )
  46. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/AppService.php ( 0.26 KB )
  47. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Service.php ( 1.64 KB )
  48. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Lang.php ( 7.35 KB )
  49. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/lang/zh-cn.php ( 13.70 KB )
  50. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/Error.php ( 3.31 KB )
  51. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/RegisterService.php ( 1.33 KB )
  52. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/services.php ( 0.14 KB )
  53. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/PaginatorService.php ( 1.52 KB )
  54. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ValidateService.php ( 0.99 KB )
  55. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ModelService.php ( 2.04 KB )
  56. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Service.php ( 0.77 KB )
  57. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Middleware.php ( 6.72 KB )
  58. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/BootService.php ( 0.77 KB )
  59. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Paginator.php ( 11.86 KB )
  60. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/Validate.php ( 63.20 KB )
  61. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Model.php ( 23.55 KB )
  62. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Attribute.php ( 21.05 KB )
  63. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/AutoWriteData.php ( 4.21 KB )
  64. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Conversion.php ( 6.44 KB )
  65. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/DbConnect.php ( 5.16 KB )
  66. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/ModelEvent.php ( 2.33 KB )
  67. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/RelationShip.php ( 28.29 KB )
  68. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Arrayable.php ( 0.09 KB )
  69. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Jsonable.php ( 0.13 KB )
  70. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/contract/Modelable.php ( 0.09 KB )
  71. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Db.php ( 2.88 KB )
  72. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/DbManager.php ( 8.52 KB )
  73. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Log.php ( 6.28 KB )
  74. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Manager.php ( 3.92 KB )
  75. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerTrait.php ( 2.69 KB )
  76. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerInterface.php ( 2.71 KB )
  77. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cache.php ( 4.92 KB )
  78. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/simple-cache/src/CacheInterface.php ( 4.71 KB )
  79. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Arr.php ( 16.63 KB )
  80. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/driver/File.php ( 7.84 KB )
  81. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/Driver.php ( 9.03 KB )
  82. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/CacheHandlerInterface.php ( 1.99 KB )
  83. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/Request.php ( 0.09 KB )
  84. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Request.php ( 55.78 KB )
  85. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/middleware.php ( 0.25 KB )
  86. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Pipeline.php ( 2.61 KB )
  87. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/TraceDebug.php ( 3.40 KB )
  88. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/middleware/SessionInit.php ( 1.94 KB )
  89. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Session.php ( 1.80 KB )
  90. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/driver/File.php ( 6.27 KB )
  91. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/SessionHandlerInterface.php ( 0.87 KB )
  92. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/Store.php ( 7.12 KB )
  93. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Route.php ( 23.73 KB )
  94. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleName.php ( 5.75 KB )
  95. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Domain.php ( 2.53 KB )
  96. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleGroup.php ( 22.43 KB )
  97. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Rule.php ( 26.95 KB )
  98. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleItem.php ( 9.78 KB )
  99. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/route/app.php ( 1.72 KB )
  100. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Route.php ( 4.70 KB )
  101. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/dispatch/Controller.php ( 4.74 KB )
  102. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Dispatch.php ( 10.44 KB )
  103. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/controller/Index.php ( 4.81 KB )
  104. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/BaseController.php ( 2.05 KB )
  105. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/facade/Db.php ( 0.93 KB )
  106. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/connector/Mysql.php ( 5.44 KB )
  107. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/PDOConnection.php ( 52.47 KB )
  108. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Connection.php ( 8.39 KB )
  109. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/ConnectionInterface.php ( 4.57 KB )
  110. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/builder/Mysql.php ( 16.58 KB )
  111. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Builder.php ( 24.06 KB )
  112. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseBuilder.php ( 27.50 KB )
  113. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Query.php ( 15.71 KB )
  114. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseQuery.php ( 45.13 KB )
  115. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TimeFieldQuery.php ( 7.43 KB )
  116. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/AggregateQuery.php ( 3.26 KB )
  117. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ModelRelationQuery.php ( 20.07 KB )
  118. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ParamsBind.php ( 3.66 KB )
  119. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ResultOperation.php ( 7.01 KB )
  120. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/WhereQuery.php ( 19.37 KB )
  121. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/JoinAndViewQuery.php ( 7.11 KB )
  122. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TableFieldInfo.php ( 2.63 KB )
  123. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/Transaction.php ( 2.77 KB )
  124. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/driver/File.php ( 5.96 KB )
  125. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/LogHandlerInterface.php ( 0.86 KB )
  126. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/Channel.php ( 3.89 KB )
  127. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/event/LogRecord.php ( 1.02 KB )
  128. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/Collection.php ( 16.47 KB )
  129. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/View.php ( 1.70 KB )
  130. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/View.php ( 4.39 KB )
  131. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Response.php ( 8.81 KB )
  132. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/response/View.php ( 3.29 KB )
  133. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cookie.php ( 6.06 KB )
  134. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-view/src/Think.php ( 8.38 KB )
  135. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/TemplateHandlerInterface.php ( 1.60 KB )
  136. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/Template.php ( 46.61 KB )
  137. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/driver/File.php ( 2.41 KB )
  138. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/contract/DriverInterface.php ( 0.86 KB )
  139. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/runtime/temp/067d451b9a0c665040f3f1bdd3293d68.php ( 11.98 KB )
  140. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Html.php ( 4.42 KB )
  1. CONNECT:[ UseTime:0.000558s ] mysql:host=127.0.0.1;port=3306;dbname=f_mffb;charset=utf8mb4
  2. SHOW FULL COLUMNS FROM `fenlei` [ RunTime:0.000818s ]
  3. SELECT * FROM `fenlei` WHERE `fid` = 0 [ RunTime:0.005765s ]
  4. SELECT * FROM `fenlei` WHERE `fid` = 63 [ RunTime:0.001096s ]
  5. SHOW FULL COLUMNS FROM `set` [ RunTime:0.000755s ]
  6. SELECT * FROM `set` [ RunTime:0.000243s ]
  7. SHOW FULL COLUMNS FROM `article` [ RunTime:0.000799s ]
  8. SELECT * FROM `article` WHERE `id` = 474880 LIMIT 1 [ RunTime:0.004403s ]
  9. UPDATE `article` SET `lasttime` = 1770741542 WHERE `id` = 474880 [ RunTime:0.007655s ]
  10. SELECT * FROM `fenlei` WHERE `id` = 66 LIMIT 1 [ RunTime:0.000430s ]
  11. SELECT * FROM `article` WHERE `id` < 474880 ORDER BY `id` DESC LIMIT 1 [ RunTime:0.000547s ]
  12. SELECT * FROM `article` WHERE `id` > 474880 ORDER BY `id` ASC LIMIT 1 [ RunTime:0.000353s ]
  13. SELECT * FROM `article` WHERE `id` < 474880 ORDER BY `id` DESC LIMIT 10 [ RunTime:0.001737s ]
  14. SELECT * FROM `article` WHERE `id` < 474880 ORDER BY `id` DESC LIMIT 10,10 [ RunTime:0.000765s ]
  15. SELECT * FROM `article` WHERE `id` < 474880 ORDER BY `id` DESC LIMIT 20,10 [ RunTime:0.011539s ]
0.109117s