惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
C
Check Point Blog
博客园_首页
B
Blog
D
Docker
U
Unit 42
量子位
I
InfoQ
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
GbyAI
GbyAI
L
LangChain Blog
云风的 BLOG
云风的 BLOG
博客园 - Franky
美团技术团队
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
月光博客
月光博客
Vercel News
Vercel News
Recent Announcements
Recent Announcements
雷峰网
雷峰网
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Google DeepMind News
Google DeepMind News

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Your Java Container Is Lying to You About Its Memory
Schiff Heimlich · 2026-06-17 · via DEV Community
Cover image for Your Java Container Is Lying to You About Its Memory

Schiff Heimlich

The part of memory Java doesn't tell you about

Java doesn't just use heap. The JVM also allocates:

  • Metaspace — class metadata, loaded by the JVM itself
  • Code cache — JIT-compiled native code
  • Thread stacks — each thread gets its own
  • Direct byte buffers (NIO) — allocated off-heap by many libraries
  • Internal JVM bookkeeping

This is called native memory, and it's invisible to your usual heap monitoring. When your container hits its cgroup memory limit, the kernel doesn't care how much heap you have left — it kills the process when the total RSS exceeds the limit.

A 512MB container running a JVM with 256MB heap can easily OOM at around 350–400MB total RSS, because metaspace, code cache, and buffers have already eaten into the headroom you didn't know you needed.

The fix nobody explains properly

The old way: -Xms256m -Xmx256m. Fixed heap size, ignores container limits.

The better way:

-XX:MaxRAMPercentage=75.0

This tells the JVM to size the heap as a percentage of the container's actual memory limit, not some fixed number. If your container has 512MB, the heap gets roughly 384MB. The remaining ~128MB is left for native memory, JIT overhead, and everything else the JVM allocates outside the heap.

For most workloads, 75% is a reasonable starting point. If you're running into native memory pressure (you'll see it in jcmd VM.native_memory), dial it down to 70%.

A few other flags worth knowing:

# Pre-touch heap pages at startup instead of on first access
-XX:+AlwaysPreTouch

# Cap metaspace growth so it can't run away
-XX:MaxMetaspaceSize=256m

AlwaysPreTouch is a tradeoff — it increases startup time but prevents those surprise OOMs when a traffic spike touches cold heap pages for the first time.

How to actually see what's happening

Heap usage comes from your app, but native memory is opaque by default. Enable native memory tracking:

-XX:NativeMemoryTracking=detail

Then query it at runtime:

jcmd <pid> VM.native_memory summary

Output looks like:

Native Memory Tracking:
Total: reserved=618MB, committed=412MB
- Heap         : 256MB reserved, 180MB committed
- Class        :  45MB reserved,  38MB committed
- Thread       :  12MB reserved,  12MB committed
- Code         :  28MB reserved,  22MB committed
- Internal     :   8MB reserved,   8MB committed

That's the total picture. Watch the "committed" column under Heap against the overall RSS — if RSS is consistently 100–150MB above committed heap, that's native overhead you need to account for when sizing your container.

The short version

Your container limit needs to cover heap plus native memory. If you only tune the heap, you're flying blind. Switch to -XX:MaxRAMPercentage, enable NativeMemoryTracking so you can actually see what's being used, and you'll stop getting OOMs when heap looks fine.

It's a 15-minute change and it eliminates one of those "but the monitoring said we had headroom" incidents that show up at 2am.