---
title: "Tech Tip #74: Close what you open"
description: "Leaked connections keep burning resources after your code has finished."
url: https://greencode.ai/tech-tips/74-close-what-you-open/
---

# Tech Tip #74: Close what you open

**Leaked connections keep burning resources after your code has finished.**

### The problem

File handles, database connections and sockets that are never closed stay open until something else cleans up, holding memory and server capacity and eventually forcing restarts.

### The fix you can use today

- Use with in Python, try-with-resources in Java, using in C#, defer in Go.
- Reuse connections through a pool instead of opening one per request.
- Watch open-connection counts in production.

### Tools that help

- [GreenCode Constitution skill](https://greencode-constitution.org/?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode) (GreenCode researchers): open-source energy principles and a catalogue of 37 energy anti-patterns that an AI coding agent can follow to find and fix waste like this
- [SonarQube](https://docs.sonarsource.com/sonarqube-server/?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode): static analysis for code smells, bugs and security issues, with quality gates in CI

### How GreenCode helps

GreenCode helps tackle this by scoring a codebase for quality, security and green software patterns in one pass, so a leak like this comes out as a ranked defect alongside the bugs and security findings. [More on this](https://greencode.ai/feature/sustainability-led-code-quality-assessment/).

### Tags

#memory#databases#python#java

### Sources

- [GreenCode Constitution skill, energy anti-patterns: H3 resource leaks: deterministic closure with try-with-resources, context managers, defer](https://greencode-constitution.org/skill.md?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode)
- [CISQ, Automated Source Code Green Measure: ASCPEM-PRF-13 data access without connection pooling; ASCRM-CWE-772 missing release of resource](https://www.it-cisq.org/wiki/cisq-automated-source-code-green-measure/?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode)

[All GreenCode Tech Tips](https://greencode.ai/tech-tips/)

#### Key Facts

- Tip **#74**
- For **Developers**
- Category **Code efficiency**
- Format **short video**
![GreenCode](https://greencode.ai/wp-content/uploads/2024/12/greencode-logo-website-small-colours-2.webp)

Want this checked on your own system? Talk to the GreenCode team.

[Get in touch](https://greencode.ai/contact/)
