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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Tuning Your Java VM

Tuning Your Java VM

Mihai Dinca - Panaitescu user avatar by
Mihai Dinca - Panaitescu
·
Jan. 11, 12 · Interview
Like (2)
Save
Tweet
Share
48.64K Views

Join the DZone community and get the full member experience.

Join For Free

Every java application needs some tuning to meet your requirements.  Java VM has a lot of command line options, the most used of which are :

-Xms<n>[g|m|k]  -> The initial and minimum size of the Java heap
-Xmx<n>[g|m|k]  -> The maximum size of the Java heap

where the  Java Heap size is  the total size of the young generation space and old generation space. (we won't enter into detail here, see doc)

Because you know your server processor's type and memory size, most of the time you will put big values for those options even if there is no need to. Well I have 8GB of memory, so what's the big deal if I set an Xms of 4GB?  Anyway, the values are chosen just because we have used them on other projects of the same caliber and no OutOfMemory was raised. But in the real world it is good to know why you have to set the heap size to some value.

Some lesser-known options are about permanent generation space.This area is only used by the HotSpot VM itself to hold metadata, such as class data structures, interned strings, and so on:

-XX:PermSize=<n>[g|m|k]        -> The initial and minimum size of the permanent generation space.
-XX:MaxPermSize=<n>[g|m|k] -> The maximum size of the permanent generation space

If you use a lot of third party libraries , your perm space may become full and an OutOfMemory regarding permgen space will be given. So you will have to increase MaxPermSize , and because for Java 6 a default value is 64M, a new value of 128M will suffice.

 The general ideea to tune your VM is that when your application was used from some time, meaning the memory size is big enough, to do a full GC to get some statistics. You know your application process pid from java status process tool (jps) or from any another OS command (like ps in linux). You can generate a full GC using java command jmap:

jmap -histo:live <pid>

If we want to see some GC info in our application stdout (or log)  we need to start the application with some command line options :

-XX:+PrintGCDetails
-Xloggc:<filename>          -> to capture GC data in a file
-XX:+PrintGCDateStamps  -> to print the date time stamp when a GC is done

This will result in some output like :

2012-01-11T15:09:01.483+0200: [GC [PSYoungGen: 250408K->7194K(247872K)] 279260K->38759K(335296K), 0.0187385 secs] [Times: user=0.11 sys=0.00, real=0.02 secs]

Here we can see young generation and total footprint of memory before and after GC. 

There are some GC algorithms which can be used by a Java VM. The following command line options allow to use a specific GC algorithm:

-XX:+UseSerialGC -> single threaded, young generation, and old generation garbage collector (should be used only for small Java heap sizes such as -Xmx256m or smaller)
-XX:+UseParallelGC -> only the young generation space utilizes a multithreaded (parallel) garbage collector.The old generation space uses a single-threaded  garbage collector.
-XX:+UseParallelOldGC -> both young and old generations use multithread garbage collector.
-XX:+UseParNewGC -> enables a multithreaded, young generation garbage collector
-XX:+UseConcMarkSweepGC -> enables the VM’s mostly concurrent garbage collector. It also auto-enables -XX:+UseParNewGC (use if If you are not able to meet your application’s worst case latency requirements due to full garbage collection duration being too long)
-XX:+UseG1GC -> garbage first collector (default in java 7, can be also used in latest releases of java 6)

Practically by default, in Java 6 ParallelGC is used, and in Java 7 G1GC. Changing this algorithm must not be done in almost all cases.

When a jmap -histo:live command is issued, the following result will be seen:

[Full GC [PSYoungGen: 28393K->0K(321088K)] [ParOldGen: 101070K->107957K(204096K)] 129464K->107957K(525184K) [PSPermGen: 75396K->74999K(110208K)], 0.6012680 secs] [Times: user=1.68 sys=0.00, real=0.60 secs] 

Here we can see young generation, old generation and permanent generation memory before and after GC.  To know your application java heap size and permanent generation size you can use some general guidelines :

1) The initial and maximum Java heap size command line options, -Xms and -Xmx, should be set to a value between three and four times larger than the live data size of the old generation space (for  previous output xms must be between 3*107957K ~ 323M and 4*107957K ~ 431M)

2) The initial and maximum permanent generation size, -XX:PermSize and -XX:MaxPermSize, should be 1.2x to 1.5x larger than the live data size of the permanent generation space (for previous output permSize must be between 1.2*74999K ~ 90M and 1.5*74999K  ~ 112M)

You must have in mind only one thing:

The more data you collect, the better the estimate for a Java heap size starting point.

Virtual Machine Java (programming language) garbage collection

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Benefits and Challenges of Multi-Cloud Integration
  • Keep Your Application Secrets Secret
  • 4 Best dApp Frameworks for First-Time Ethereum Developers
  • How Agile Architecture Spikes Are Used in Shift-Left BDD

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: