DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Memory Optimization and Utilization in Java 25 LTS: Practical Best Practices
  • Optimizing Java Applications for Arm64 in the Cloud
  • Memory Leak Due To Mutable Keys in Java Collections
  • Advanced Java Garbage Collection Concepts: Weak References, Finalization, and Memory Leaks

Trending

  • Evaluating SOC Effectiveness Using Detection Coverage and Response Metrics
  • Genkit Middleware: Intercept, Extend, and Harden your Gen AI Pipelines
  • Hallucination Has Real Consequences — Lessons From Building AI Systems
  • Stop Using Python for Your GenAI Apps, Use Go and Genkit Instead
  1. DZone
  2. Coding
  3. Java
  4. How Much Memory Does a Java Thread Take?

How Much Memory Does a Java Thread Take?

Learn more about Java thread memory consumption.

By 
Petr Bouda user avatar
Petr Bouda
DZone Core CORE ·
Updated Jun. 10, 19 · Analysis
Likes (26)
Comment
Save
Tweet
Share
87.1K Views

Join the DZone community and get the full member experience.

Join For Free

Update: 

JVM does not aggressively size committed memory to the number of threads * 1MB, that's a wrong assumption based on the wrong NMT reporting where in Java 8 committed memory is automatically set to reserved memory. See https://bugs.openjdk.java.net/browse/JDK-8191369

The committed memory is defined by the stack depth. Thanks to Thomas Stuefe to point to this fact in comments. 


A memory, which is taken by all Java threads, is a significant part of the total memory consumption of your application. There are a few techniques on how to limit the number of created threads, depending on whether your application is CPU-bound or IO-bound. If your application is rather IO-bound, you will very likely need to create a thread pool with a significant number of threads which can be bound to some IO operations (in blocked/waiting state, reading from DB, sending HTTP request).

However, if your app rather spends time on some computing task, you can, for instance, use HTTP server (e.g. Netty) with a lower number of threads and save a lot of memory. Let's look at an example of how much memory we need to sacrifice to create a new thread.

Thread memory contains stack frames, local variables, method parameters, ... and a thread size can is configured with defaults this way (in kilobytes):

$ java -XX:+PrintFlagsFinal -version | grep ThreadStackSize  
     intx CompilerThreadStackSize    = 1024  {pd product} {default}
     intx ThreadStackSize            = 1024  {pd product} {default}
     intx VMThreadStackSize          = 1024  {pd product} {default}


Thread Memory Consumption on Java 8

$ java -XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=summary /
-XX:+PrintNMTStatistics -version
openjdk version "1.8.0_212"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_212-b03)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.212-b03, mixed mode)

Native Memory Tracking:

Total: reserved=6621614KB, committed=545166KB
-                 Java Heap (reserved=5079040KB, committed=317440KB)
                            (mmap: reserved=5079040KB, committed=317440KB) 

-                     Class (reserved=1066074KB, committed=13786KB)
                            (classes #345)
                            (malloc=9306KB #126) 
                            (mmap: reserved=1056768KB, committed=4480KB) 

-                    Thread (reserved=19553KB, committed=19553KB)
                            (thread #19)
                            (stack: reserved=19472KB, committed=19472KB)
                            (malloc=59KB #105) 
                            (arena=22KB #34)


We can see two types of memory:

  • Reserved — the size which is guaranteed to be available by a host's OS (but still not allocated and cannot be accessed by JVM) — it's just a promise
  • Committed — already taken, accessible, and allocated by JVM

In a section Thread, we can spot the same number in Reserved and Committed memory, which is very close to a number of threads * 1MB. The reason is that JVM aggressively allocates the maximum available memory for threads from the very beginning.

Thread Memory Consumption on Java 11

$ java -XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=summary /
-XX:+PrintNMTStatistics -version 
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.2+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.2+9, mixed mode)

Native Memory Tracking:

Total: reserved=6643041KB, committed=397465KB
-                 Java Heap (reserved=5079040KB, committed=317440KB)
                            (mmap: reserved=5079040KB, committed=317440KB) 

-                     Class (reserved=1056864KB, committed=4576KB)
                            (classes #426)
                            (  instance classes #364, array classes #62)
                            (malloc=96KB #455) 
                            (mmap: reserved=1056768KB, committed=4480KB) 
                            (  Metadata:   )
                            (    reserved=8192KB, committed=4096KB)
                            (    used=2849KB)
                            (    free=1247KB)
                            (    waste=0KB =0,00%)
                            (  Class space:)
                            (    reserved=1048576KB, committed=384KB)
                            (    used=270KB)
                            (    free=114KB)
                            (    waste=0KB =0,00%)

-                    Thread (reserved=15461KB, committed=613KB)
                            (thread #15)
                            (stack: reserved=15392KB, committed=544KB)
                            (malloc=52KB #84) 
                            (arena=18KB #28)


You may notice that we are saving a lot of memory just because we are using Java 11, which no longer aggressively allocates up to Reserved Memory at the time of thread creation. Of course, this is just java -version command, but if you try it out, you will definitely notice a big improvement.

Thank you for reading my article and please let some comments below. If you like being notified about new posts, then start following me on Twitter: @p_bouda. 

Memory (storage engine) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Memory Optimization and Utilization in Java 25 LTS: Practical Best Practices
  • Optimizing Java Applications for Arm64 in the Cloud
  • Memory Leak Due To Mutable Keys in Java Collections
  • Advanced Java Garbage Collection Concepts: Weak References, Finalization, and Memory Leaks

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook