python-memory-safe-scripts

python-memory-safe-scripts is a skill for Claude Code from terrylica/cc-skills. It costs 197 tokens per session (2,794 once invoked), scanned A, original, MIT.

A set of Python patterns for keeping long-running scripts within a systemd MemoryMax limit, which caps how much memory a service may use. It covers garbage collection, releasing HTTP and data-frame resources, connection reuse, and returning freed memory to the operating system.

In plain words
What is it for?
Use it when writing or repairing long-running Python workers, data-processing scripts, and scheduled services that must stay within a memory limit.
Why use it?
Python can release objects while the process still holds the underlying memory, causing its memory use to grow and eventually triggering an out-of-memory failure.

Skill for Claude Code

Written for Claude Code: allowed-tools in frontmatter.

Part of the devops-tools plugin — 25 skills shipped together , and of cc-skills

Good fit Use it when writing or repairing long-running Python workers, data-processing scripts, and scheduled services that must stay within a memory limit.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/terrylica/cc-skills/python-memory-safe-scripts
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Any agent
npx skills add terrylica/cc-skills --skill python-memory-safe-scripts
Clone the repo
git clone --depth 1 https://github.com/terrylica/cc-skills

Made for: Claude Code.

Or install devops-tools, the plugin that ships this one along with the rest of its 25 skills.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for python-memory-safe-scripts

README.md
[![agentmods](https://agentmods.dev/badge/skills/terrylica/cc-skills/python-memory-safe-scripts/github.svg)](https://agentmods.dev/skills/terrylica/cc-skills/python-memory-safe-scripts)
Your own site
<a href="https://agentmods.dev/skills/terrylica/cc-skills/python-memory-safe-scripts"><img src="https://agentmods.dev/badge/skills/terrylica/cc-skills/python-memory-safe-scripts/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for python-memory-safe-scripts

Your own site · 80×15
<a href="https://agentmods.dev/skills/terrylica/cc-skills/python-memory-safe-scripts"><img src="https://agentmods.dev/badge/skills/terrylica/cc-skills/python-memory-safe-scripts.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 197 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 2,794 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 1 finding. A grade says what 26 rules found in the file — not that it is safe. Third-party audits
  • NVIDIA SkillSpector warn 7 Sept 2026
SkillSpector: 2 findings, up to high

These are SkillSpector’s own severities. On a checked sample its high-severity flags on skills were ~96% false positives — a documented command, a public API, a “never do X” rule — so we show them as a caution to read, not a verdict. Why →

  • high YARA Match · line 70
    YARA rule matched a known malware signature (reverse shell, backdoor, ransomware, C2 framework, or info stealer).
    Fix: Remove the malware payload or compromised file entirely. Investigate how it entered the skill and audit all other artifacts for additional indicators of compromise.
  • high Rogue Agent · line 257
    Skill modifies its own code, configuration, or behavior at runtime. Self-modification enables an agent to escalate privileges, disable safety constraints, or install persistent backdoors.
    Fix: Prevent the skill from modifying its own code, SKILL.md, or configuration files. Treat skill files as read-only at runtime.
How audits are shown
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00197 $0.02794
Opus 5.5 $0.00079 $0.01118
Sonnet 5.5 $0.00039 $0.00559
Haiku 4.5 $0.00020 $0.00279

Measured 3mo ago against content hash 72317a0b541c, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, from the pricing page.

Security

Grade A, and why

python-memory-safe-scripts scanned grade A with 1 finding against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 3mo ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Makes network callslowCapability

Not a fault in itself. Listed so you know the mod talks to something, and to what.

resp = requests.get(url, timeout=60)
plugins/devops-tools/skills/python-memory-safe-scripts/SKILL.md · 260 lines

How it starts

The opening of the file, as written. The whole thing — 260 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Memory-Safe Python Script Patterns

Battle-tested patterns for keeping Python scripts alive under systemd MemoryMax constraints. Extracted from repair_direct_parquet.py (24-worker parallel repair) and exness_tick_cache_seeder.py (10-symbol daily seeder) after 5 OOM optimization cycles on a 62 GB GPU workstation.

Core insight: Python's garbage collector frees objects, but the C allocator (glibc ptmalloc2) does NOT return freed pages to the OS. Without explicit malloc_trim(0), RSS only grows — even after del and gc.collect(). mimalloc with MIMALLOC_PURGE_DELAY helps but explicit purge is faster.

Self-Evolving Skill: This skill improves through use. If instructions are wrong, parameters drifted, or a workaround was needed — fix this file immediately, don't defer. Only update for real, reproducible issues.

The 7 Patterns

1. Cached Allocator Purge

The most important pattern. Cache the ctypes library handle on first call so subsequent purges are a single FFI invocation with zero allocation overhead.

import ctypes
import gc
import sys

_purge_lib = None
_purge_method = None  # "mimalloc" | "glibc" | "none"

def _force_allocator_purge():
    """Force mimalloc/glibc to return freed pages to the OS."""
    global _purge_lib, _purge_method

    if sys.platform != "linux":
        return

    if _purge_method is None:
        try:
            _purge_lib = ctypes.CDLL("libmimalloc.so.2")
            _purge_method = "mimalloc"
        except OSError:
            try:
                _purge_lib = ctypes.CDLL("libc.so.6")
                _purge_method = "glibc"
            except OSError:
                _purge_method = "none"

    if _purge_method == "mimalloc":
        _purge_lib.mi_collect(ctypes.c_bool(True))
    elif _purge_method == "glibc":
        _purge_lib.malloc_trim(0)

def _force_gc():
    """Python GC + allocator purge. Call every 50 iterations + between work units."""
    gc.collect()
    _force_allocator_purge()

Read the full file on GitHub · 260 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 3mo ago First seen · 260 lines · 197 tokens per session scan A 72317a0b541c

Subscribe to this mod's changes

python-memory-safe-scripts is a skill published in the GitHub repository terrylica/cc-skills (5 stars, last pushed today), licensed MIT. It adds 197 tokens to every session and 2,794 once invoked, about $0.0008 per session on Opus 5.5. A static security scan graded it A with 1 finding (makes network calls). No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-05.

Related

python-memory-safe-scripts compared with skills from other repositories

python-memory-safe-scripts next to 6 skills from other repositories
ModTypeStarsLast pushLicenceScanTokensWorks with
python-memory-safe-scriptsterrylica/cc-skillsSkill5todayMITA197Claude Code
py-debugberekvolgyipeter/dotclaudeSkill23d agoMITA54Claude Code, Codex
ai-ml-developmenttravisjneuman/.claudeSkill116todayMITA43Claude Code
agent-python-experttravisjneuman/.claudeSkill116todayMITA37Claude Code
pypi-ops0xDarkMatter/claude-modsSkill8612d agoMITA74Claude Code
python-performanceseaworld008/Commonly-used-high-value-skillsSkill756d agoMITA38Claude Code, Codex
python-typing-ops0xDarkMatter/claude-modsSkill8612d agoMITA44Claude Code