Skip to main content

JVM internals


JRE has JVM, Garbage collector, JIT compiler

JVM - young generation, old generation, permanent generation (metaspace).

Garbage collector - Serial, Multithreaded, CMS, G1

Serial
          - single threaded stop the world young generation collector
          - single threaded stop the world old generation collector

Throughput/Parallel/Multithreaded -
         - Multithreaded stop the world young generation collector
         - Single/multi threaded stop the world old generation collector

CMS
       - Server class machine default collector
       - Multithreaded young & old generation collector
       - 2 GB
       - 2 Virtual CPU's
       - One exception is on Windows 32 bit the default is the serial collector.

G1
     - Supported as of Java HotSpot 7 update 4
     - Region based multithreaded stop the world young generation collector
     - Combination of a mostly concurrent and stop the world old generation collector

JIT Compiler
     - Takes Java bytecode and generates native code for underlying platform
     - Huge performance improvement realized from JIT compilation
     * Choices
     - Client - rapid startup
     - Server - highly
     - Tiered - best of both, enabled via -XX:+TieredCompilation (default for Java 8)

VM Runtime
 java -XX:+PrintFlagsFinal

Heap Layout
Eden    - new objects are allocated here, when its full or exhausted minor GC occurs. Then objects are copied to "From Survivor" space and to "To Survivor" space.
From Survivor   - objects move to "To Survivor space"
To Survivor      - move to "Old Generation" space after several minor GC's

Old Generation  - Objects are collected during full GC, its longer and
for older/longer-living objects

Perfmanent Generation (not heap)

jvisualvm + Visual GC + Memory Pools

Operating Systems Performance Metrics to Monitor
CPU usage, including user CPU, system CPU and idle time
Virtual memory usage
Process behavior, especially context switching and CPU scheduling and thread migraiton
Disk I/O, if Disk I/O is involved with the application
Network I/O, if network traffic is involved with the application

Tools: vmstat 5, mpstat, vm_stat, top, monitor

Lock Contention
An app that spends more than 5% of its available clock cycles in involuntary context switching is likely suffering from lock contention.
Can be as much as 80K clock cycles


CPU Scheduler Run Queue
App thread on queue, if app threads larger than vCPU's then it builds up in run queue. If double the run queue will degrade performance

How to correct:
Add additional processors
Add additional systems
Bring back the load

vmstat 5



JVM Performance metrics to monitor

Garbage collection is the by far most intrusive activity upon the application.
 --XX:+PrintGCDetails
 --XX:+PrintGCDateStamps or --XX:+PrintGCTimeStamps
 --Xloggc if you'd like to direct output to a log file

VisualVM's VisualGC to observe garbage collection activity.

Monitoring app exe time and stopped time
Identify when jvm stopped all threads to gc
  --XX:+PrintGCApplicationStoppedTime
  --XX:+PrintGCApplicationConcurrentTime

Others to consider but not necessary
JIT compilation
--XX:+PrintCompilation
--XX:+PrintInlining

Fine tuning JVM heap space sizes
--XX:+PrintTenuringDistribution
--XX:+PrintAdaptiveSizePolicy (parallel gc or g1 only)

Remote monitor:
jstatd daemon
acts as a agent
requires a security manager and security policy file
user creds of jstatd should be of same as app

App metrics to monitor
response time
app level jmx mbeans
jdbc connections

Eden:
Object allocation occurs in Eden and once it becomes full a minor GC occurs, and objects are moved to From Survivor space and To Survivor space.
Once objects are lived for some period of time they are promoted to Old generation.
And once Old generation is filled a GC is required.

Parallel GC
Both young and old generation GC is stop the world events.
Default GC for server class machines (2 vcpu and 2 gb)
--XX:+UseParallelGC - default GC in Java 6 through Java 7 Update 3.
* Is a multi-threaded young generation GC and single threaded old generation GC.
* Full GC takes longer than minor GC.

--XX:+UseParallelOldGC - default in Java 7 Update 4 and later.
* Is a multi-threaded young generation GC and multi threaded old generation GC.


Note: PermGen is also collected on full GC.

** Reduce full GC collections as much as possible.

Promotion Failure
young to old generation.

CMS GC
End of minor GC Eden is empty
When Old Gen cross some threshold major GC occurs
Multi-threaded Young and concurrent Old Gen.
Some phases are single threaded, stop the world.
Initial-mark phase, stop the world and single threaded.
Concurrent marking phase, concurrent and multi-threaded.
Pre-cleaning phase - concurrent and multi-threaded.
Remark phase - stop the world and multi-threaded.
Concurrent sweeping phase, concurrent and multi-threaded.
Concurrent collection does not compact old gen.
Stop the world, single threaded old gen compaction occurs if concurrent cycle not keeping up, or old gen is too fragmented for promoted object to fit, accomplished via a single threaded, stop the world, full gc.

--XX:+UseConcMarkSweepGC - explicitely use this.
multi threaded minor gc, conc old gen 70% space.

G1 GC
-Quite diff
-Rather than having a physical GC spaces such as eden, survivor and old gen, G1 divides one large --contiguous space into many fixed size regions.
-Regions are designated as an eden, survivor and old region.
-Two additional types of regions: available/unused regions and humongous regions for large objects.
- conc old gen gc, all but on phase 'remark' is stop the world, but very very quick.
  - initial mark phase
  - root region scanning
  - conc marking phase
  - remark phase stop the world
  - cleanup phase, stop the world
  -Xms -Xmx min and max heap size.
  --XX:MaxGCPauseMillis defaults to 200 MS - not a gurantee
 --Xmn, --XX:NewSize/-XX:MaxNewSize --XX:SurvivorRatio


Metrics

Throughput
how to measure? transaction or message per second?
What is the expected throughput?
How long do u need to sustain that expected throughput
What's considered unacceptable throughput

Latency
Round trip time? http request/res
Expected latency

Memory/footprint
Footprint (how much memory can use)
How much memory can be used by java app?
how much ram available?


Determine memory footprint
 - Determine Live data size? (heap)
 - -XX:+UseParallelOldGC and collect GC statistics --XX:+PrintGCDetails and -XX:+PrintGCDateStamps
 If u know max RAM and # jvms running on the system
  then -Xms3g -Xmx3g then 1g for OS, use 80% ram.

OutOfMemoryError
coz of young, old or perm

Live data size?
Use VisualVM or JConsole for Full GC.
* Occupancy of Old gen space after full GC

ParOldGen: 420M->290M(640M) -- Live data size
PSPermGen: 32M->32M(64M) -- Live data size
Worst case latency - seconds to run gc


Determine Java Heap Size
-Xms and -Xmx to 3x to 4x the live data size.
-Xmn young gen to 1x to 1.5x the live data size.
old gen to 2x to 3x the live data size.
In other words, young gen should be about 1/3rd or 1/4th of -Xms or -Xmx

Example:
Live data size : 512m
-Xms2g -Xmx2g -Xmn768m

-XX:PermSize & -XX:MaxPermSize to about 1.2x to 1.5x the max perm gen size.


----- Sample Appliance ----

-Xms1024m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=256m -Dcom.sun.management.jmxremote -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/appdirectorLongevityError.dump

vCPU -  2
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 45
model name : Intel(R) Xeon(R) CPU E5-2680 0 @ 2.70GHz
stepping : 7
cpu MHz : 2700.000
cache size : 20480 KB
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 popcnt aes hypervisor lahf_lm ida arat epb pln pts dts
bogomips : 5400.00
clflush size : 64
cache_alignment : 64
address sizes : 40 bits physical, 48 bits virtual
power management:


-----------------------











Comments

Popular posts from this blog

JPA 2 new feature @ElementCollection explained

@ElementCollection is new annotation introduced in JPA 2.0, This will help us get rid of One-Many and Many-One shitty syntax. Example 1: Stores list of Strings in an Entity @Entity public class Users implements Serializable {     private static final long serialVersionUID = 1L;     @Id     @GeneratedValue(strategy = GenerationType.AUTO)     private Long id;     @ElementCollection     private List<String> certifications = new ArrayList <String> ();     public Long getId() {         return id;     }     public void setId(Long id) {         this.id = id;     }     public List <String> getCertifications() {         return certifications;     }     pub...

Validating CSV Files

What is CsvValidator ?   A Java framework which validates any CSV files something similar to XML validation using XSD. Why should I use this ?   You don't have to use this and in fact its easy to write something your own and also checkout its source code for reference. Why did I write this ?   Some of our projects integrate with third party application which exchanges information in CSV files so I thought of writing a generic validator which can be hooked in multiple projects or can be used by QA for integration testing. What is the license clause ?   GNU GPL v2 Are there any JUnit test cases for me checkout ?  Yes,  source How to integrate in my existing project ? Just add the Jar which can be downloaded from here  CsvValidator.jar  and you are good. Instantiate  CsvValidator c onstructor which takes these 3 arguements          // filename is the the file to be validated and here ...

Reuse JPA Entities as DTO

Note : Major design advantages of JPA Entities are they can detached and used across tiers and networks and later can by merged. Checkout this new way of querying entities in JPA 2.0 String ql = " SELECT new prepclass2.Employee (e.firstname, e.lastname) FROM Employee e "; List<Employee> dtos = em.createQuery(ql).getResultList(); The above query loads all Employee entities but with subset of data i.e. firstname, lastname. Employee entity looks like this. @Entity @Table(name="emp") public class Employee implements Serializable {     private static final long serialVersionUID = 1L;     @Id     @GeneratedValue(strategy = GenerationType.AUTO)     private Long id;     @Column     private String firstname;     @Column     private String lastname;     @Column     private String username;     @Column ...