Py-Memory-Optimizer:Python 内存泄漏检测 - Openclaw Skills
作者:互联网
2026-04-14
什么是 py-memory-optimizer?
py-memory-optimizer 是 Openclaw Skills 生态系统中的一个专用工具,旨在简化 Python 内存管理。通过利用 AST 模块进行静态分析,它可以在不执行代码的情况下识别潜在的内存泄漏、低效的对象创建以及不当的生成器使用。这使其成为开发者在开发阶段提高应用程序性能和资源效率的宝贵资产。
该技能通过提供具体的、可操作的建议,在原始代码与高性能执行之间架起了一座桥梁。无论您是在处理大型数据集还是长期运行的 Web 服务,整合 Openclaw Skills 库中的这一工具都能确保您的代码保持精简高效。
下载入口:https://github.com/openclaw/skills/tree/main/skills/martinforsulu/neo-py-memory-optimizer
安装与下载
1. ClawHub CLI
从源直接安装技能的最快方式。
npx clawhub@latest install neo-py-memory-optimizer
2. 手动安装
将技能文件夹复制到以下位置之一
全局模式~/.openclaw/skills/
工作区
/skills/
优先级:工作区 > 本地 > 内置
3. 提示词安装
将此提示词复制到 OpenClaw 即可自动安装。
请帮我使用 Clawhub 安装 neo-py-memory-optimizer。如果尚未安装 Clawhub,请先安装(npm i -g clawhub)。
py-memory-optimizer 应用场景
- 识别复杂 Python 项目中由未关闭的文件句柄或循环引用引起的内存泄漏。
- 通过将列表推导式转换为节省内存的生成器表达式,优化大规模数据处理脚本。
- 审计旧代码库,以确保符合现代 Python 内存最佳实践。
- 通过 Openclaw Skills CLI 接口将自动化内存分析集成到 CI/CD 流水线中。
- 该工具将 Python 源文件解析为抽象语法树 (AST),以便在不执行代码的情况下映射出代码结构。
- 它会针对已知内存密集型模式(如不必要的全局变量积累)数据库对树进行扫描。
- 检测引擎会识别潜在的资源泄漏,例如未关闭的上下文管理器或阻止垃圾回收的持久对象。
- 该技能会生成一份详尽的报告,详细说明发现的问题、严重程度以及包含预估节省空间的特定代码优化建议。
py-memory-optimizer 配置指南
要开始使用 Openclaw Skills 库中的此工具,请通过 npm 全局安装:
npm install --global openclaw-skill-py-memory-optimizer
首次执行时,该工具会自动处理其 Python 后端的安装,包括 pydantic、rich 和 astroid 等依赖项。
py-memory-optimizer 数据架构与分类体系
该技能将发现的问题组织成结构化报告,以帮助开发者优先处理修复。数据分类如下:
| 数据点 | 描述 |
|---|---|
| 内存问题摘要 | 按严重程度(严重、高、中、低)分类的问题高层统计。 |
| 详细发现 | 映射到检测模式和代码示例的特定文件路径和行号。 |
| 内存影响评估 | 针对每项建议计算出的潜在内存节省预测。 |
| 统计数据 | 分析的对象总数和总计可回收的潜在内存。 |
name: py-memory-optimizer
description: Automatically analyzes Python code and suggests memory usage optimizations for improved performance
py-memory-optimizer Skill
Overview
This skill provides static analysis of Python code to identify memory-intensive patterns, potential memory leaks, and optimization opportunities. It generates actionable suggestions with estimated memory savings and best practice recommendations.
Core Capabilities
- Static Code Analysis: Parses Python source files using the AST module to analyze code structure without execution
- Pattern Detection: Identifies common memory-intensive patterns (large list comprehensions, unnecessary object creation, improper generator usage)
- Leak Detection: Finds potential memory leaks from circular references, unclosed resources, and global variable accumulation
- Optimization Suggestions: Provides specific, actionable recommendations with estimated memory impact
- Framework Support: Handles Python 3.8+ and common frameworks (Django, Flask, FastAPI patterns)
- CLI Interface: Command-line tool for integration into development workflows
- Report Generation: Creates detailed analysis reports in multiple formats (JSON, markdown, plain text)
Dependencies
System Requirements
- Python 3.8+
- pip package manager
Python Packages (installed automatically via package.json)
- astroid (optional, for enhanced AST analysis)
- tabulate (for formatted output)
- rich (for colored terminal output)
- click (for CLI interface)
- pydantic (for data validation)
- python-slugify (for report naming)
Installation
npm install --global openclaw-skill-py-memory-optimizer
The skill will automatically install Python dependencies on first run.
Usage
CLI Interface
# Analyze a single file
py-memory-optimizer analyze path/to/script.py
# Analyze entire directory
py-memory-optimizer analyze ./my_project --recursive
# Generate detailed report
py-memory-optimizer analyze script.py --format json --output report.json
# Show optimization suggestions with memory estimates
py-memory-optimizer analyze script.py --show-suggestions --estimate-savings
# Exclude specific patterns
py-memory-optimizer analyze . --exclude "*.test.py" "*/migrations/*"
# Version info
py-memory-optimizer --version
Integration with OpenClaw Agents
The skill can be called directly by OpenClaw agents:
Analyze this Python code for memory optimizations:
The agent will invoke the analyzer and return structured suggestions.
Output Format
The analyzer produces:
- Memory Issue Summary: Count of issues by severity (critical, high, medium, low)
- Detailed Findings: For each issue:
- File and line number
- Issue type and description
- Memory impact estimate
- Specific optimization suggestion with code example
- Overall Statistics: Total memory potentially saved, number of objects analyzed
- Best Practices Checklist: Compliance with Python memory optimization guidelines
Examples
Example 1: Large List Comprehension
Input:
# Bad: Creates entire list in memory
result = [process(item) for item in huge_dataset]
Output:
Line 15: Medium memory issue
Pattern: Large list comprehension
Current memory: ~O(N) for full list
Suggestion: Use generator expression
Optimized: (process(item) for item in huge_dataset)
Estimated savings: 70-90% for large datasets
Example 2: Unclosed File Handles
Input:
f = open('data.txt', 'r')
data = f.read()
# Missing f.close()
Output:
Line 8: High memory/resource issue
Pattern: Unclosed file handle
Risk: File descriptor leak, memory not freed
Suggestion: Use context manager
Optimized: with open('data.txt', 'r') as f:
data = f.read()
Estimated savings: Prevents descriptor accumulation
Out of Scope
- Runtime profiling (use memory_profiler for that)
- C extension analysis
- Database or external service optimization
- Automatic code modification (this skill only suggests)
- Mixed-language project optimization
Files
This skill provides:
SKILL.md: This documentationpackage.json: NPM package definitionREADME.md: Quick start guidescripts/main.py: CLI entry pointscripts/analyzer.py: Core analysis enginescripts/optimizer.py: Suggestion generatorscripts/utils.py: AST utilities and helpersreferences/memory_patterns.md: Pattern referencereferences/best_practices.md: Best practices guideassets/sample_code/: Example files for testing
License
MIT
Support
For issues and feature requests, visit: https://github.com/openclaw/skills/issues
相关推荐
专题
+ 收藏
+ 收藏
+ 收藏
+ 收藏
+ 收藏
+ 收藏
最新数据
相关文章
Substack 文章格式化工具:优化时事通讯工作流 - Openclaw Skills
股票行情获取器:实时市场数据与定价 - Openclaw Skills
Polymarket 交易量分析:流动性与趋势追踪 - Openclaw Skills
Polymarket 情绪分析:预测群体心理 - Openclaw Skills
Polymarket 仓位计算器:最优交易规模 - Openclaw Skills
Polymarket 新闻影响力:分析事件关联性 - Openclaw Skills
Polymarket 市场对比:分析预测数据 - Openclaw Skills
Polymarket 选举赔率:实时政治市场追踪 - Openclaw Skills
Polymarket 跟单交易机器人:自动化巨鲸追踪 - Openclaw Skills
Polymarket 套利扫描器:实时预测市场套利 - Openclaw Skills
AI精选
