Linux Internals

Garbage Collection and App Performance on a VPS

By NileVPS Team · Published · 5 min read

اقرأ المقال بالعربي

If your application is written in Java, Go, JavaScript, Python, C# or Ruby, you do not free memory yourself. A garbage collector (GC) finds objects the program can no longer reach and reclaims their memory. This removes whole classes of bugs, but it is not free: collection costs CPU time, can pause your program, and influences how much RAM the process holds. On a VPS with a fixed amount of memory and a few vCPUs, understanding the GC often explains mysterious latency spikes, slowly growing memory and out-of-memory kills. This article covers how collectors work, the symptoms of GC trouble, and practical settings for the most common runtimes.

How a garbage collector works

Most collectors follow the same basic idea:

  1. Find the roots: variables on thread stacks, globals and registers.
  2. Mark: follow every reference from the roots and mark each reachable object as live.
  3. Sweep or compact: reclaim everything not marked. Some collectors also move live objects together to reduce fragmentation.

Two refinements matter in practice:

  • Generational collection. Most objects die young, so the heap is split into a young area collected often and cheaply, and an old area collected rarely. Java, .NET, V8 (Node.js) and Python's cycle collector use generations.
  • Concurrent collection. Much of the marking runs alongside your program on other threads, so the "stop-the-world" pauses are short. Go's collector and modern Java collectors such as G1 and ZGC work this way.

Python is a special case: CPython frees most objects immediately through reference counting, and its generational collector exists mainly to clean up reference cycles.

Why GC affects performance

  • Pauses. Even concurrent collectors stop application threads briefly. With a badly sized heap, pauses become longer or more frequent and show up as p99 latency spikes.
  • CPU overhead. Marking competes with your request handling. On a 1-2 vCPU server, background GC threads take a meaningful share of compute. See threads vs processes for how this interacts with your worker count.
  • Memory headroom. Collectors are lazy by design: they let garbage accumulate before cleaning up. A process using 300 MB of live data may hold far more than that between collections.
  • Allocation rate. The more temporary objects your code creates per request, the more often the GC must run. Reducing allocations is frequently the most effective fix.

The container and VPS trap

A runtime that does not know its memory limit will size its heap from the total RAM it sees, or let it grow until the kernel's OOM killer ends the process. Two rules avoid most problems:

  • Leave room for the operating system, page cache and other services. On a 4 GB VPS running a database too, giving one app a 3.5 GB heap is asking for trouble.
  • Set an explicit limit so the GC works harder before memory runs out, instead of the kernel killing the process without warning.

Check whether the OOM killer has already struck:

journalctl -k | grep -i -E "out of memory|oom-kill"

Runtime-by-runtime settings

RuntimeKey settingsNotes
Java (JVM)-Xms, -Xmx, -XX:MaxRAMPercentage, -XX:+UseG1GC / -XX:+UseZGCG1 is the default in modern JDKs; set -Xmx below available RAM
GoGOGC, GOMEMLIMITGOMEMLIMIT (Go 1.19+) is a soft cap that makes the GC work harder near the limit
Node.js--max-old-space-size=<MB>Limits V8's old generation; default depends on version and system memory
.NETDOTNET_gcServer, DOTNET_GCHeapHardLimitWorkstation GC is lighter on small servers
Pythongc.freeze(), gc.set_threshold()Mostly reference counting; tuning rarely needed

Examples:

# Java: fixed 1 GB heap with G1
java -Xms1g -Xmx1g -XX:+UseG1GC -jar app.jar

# Go: soft limit at 800 MiB
GOMEMLIMIT=800MiB ./server

# Node.js: allow up to 1536 MB in the old generation
node --max-old-space-size=1536 server.js

Setting -Xms equal to -Xmx avoids heap resizing at runtime, which suits servers that run one long-lived app.

Measuring GC instead of guessing

Turn on GC logging before you change anything:

# Java 11+
java -Xlog:gc*:file=/var/log/app/gc.log:time,uptime -jar app.jar

# Go
GODEBUG=gctrace=1 ./server

# Node.js
node --trace-gc server.js

For a running JVM, jstat -gcutil <PID> 1000 prints heap occupancy and GC time every second. Look for three things: how often collections happen, how long pauses last, and whether old-generation usage keeps climbing after every full collection. A steady climb that never comes back down usually means a memory leak, not a tuning problem.

A practical checklist

  1. Measure live memory after a full collection; size the heap at roughly two to three times that, within the RAM you can spare.
  2. Set an explicit memory limit for the runtime.
  3. Leave memory for the OS, caches and other services on the same server.
  4. Enable GC logs and compare pause times before and after each change.
  5. Reduce allocations in hot code paths: reuse buffers, avoid needless string building, stream large responses.
  6. Avoid swap for latency-sensitive apps; a GC walking a heap that has been swapped out is very slow.

If profiling shows you simply need more memory, you can compare plan sizes on our pricing page.

FAQ

Does garbage collection make an app slower than manual memory management?

It adds some CPU and memory overhead, but modern collectors are efficient and the pauses are short for most web workloads. Poor allocation patterns and wrong heap sizes cause far more trouble than the existence of a GC itself.

Why does my app's memory never go down after a traffic spike?

Many runtimes keep memory they have already claimed from the operating system, expecting to reuse it. Seeing high resident memory after a spike is often normal. It is a concern only if memory keeps growing over time under similar load.

Should I call the garbage collector manually?

Usually not. Forcing collections, for example with System.gc() in Java, tends to waste CPU and can cause longer pauses. Let the collector decide, and adjust heap size and limits instead.

Related guides