Google

Jun 17, 2014

How do you monitor the JVM?

Since Java SE 5.0, the JRE provides a mean to manage and monitor the Java Virtual Machine. It comes in two flavors:



The JVM has built-in instrumentation that enables you to monitor and manage it using Java Management eXtension (JMX). You can also monitor instrumented applications with JMX. To start a Java application with the management agent for local monitoring, set the following JVM argument when you run your application.

$JAVA_HOME/bin/java -Dcom.sun.management.jmxremote  MyApp
 
To start the JConsole:

$JAVA_HOME/bin/jconsole

The other is a Simple Network Management Protocol (SNMP) agent that builds upon the management and monitoring API of the Java SE platform, and exposes the same information through SNMP.

Proactive application monitoring is vital to detect and respond to problems - before an end user is even aware that a problem exists, especially for revenue-generating production environments. it's also important to be able to gather metrics about performance and resource consumption, especially for long-running applications like websites. While you can get some information from the operating system (such as CPU and memory usage), you will often need much more detailed information.




Problem
Symptom
Tool
Insufficient memory
Java.lang.OutOfMemoryError indicating out of heap space, perm gen space or native swap memory space.

jhat
Memory leaks
Frequent garbage collection and growing use of memory indicated by saw tooth shaped graph. This can cause the application to slow down.
jconsole, jstat, jmap, JAMon, VisualVM, and commercial tools such as Wily's Introscope or YourKit Java Profiler.

Deadlocked threads waiting for each other, endlessly looping threads, and racing threads ending up waiting for other long running thread to release the monitor (known as thread contention).
Threads blocked on object monitors or java.util.concurrent locks. Deadlocked threads can cause a part or the whole application to become unresponsive.

Looping threads can cause the application to hang.

Thread contention issues can adversely affect performance and scalability.
jconsole, jstack, and jmap, VisualVM, and commercial tools such as Wily's Introscope or YourKit Java Profiler.
JVM abruptly going down and other network fault monitoring.
Causing an application or service to be down.
tools such as Wily's Introscope, capable of monitoring the JVM environment and emitting SNMP traps to an IBM's Tivoli console to raise alarms.

network switches and routers, with built-in SNMP capabilities to send traps when network faults occur.


Many organizations use commercial tools like Wily's Introscope, Tivoli Monitoring, HP SiteMonitor, etc and SNMP trap handling tools like Nagios to provide quality of service (QoS) on their mission critical applications.

Labels: ,

Jun 15, 2014

Converting Java byte code to source code, flavours of JVM, and JVM runtime arguments

In the Java, JDK, JRE, JIT, and JVM basics post we looked at:
  • Why use Java? 
  • Is Java 100% object oriented? 
  • What does Java comprise of? 
  • What is the main difference between the Java platform and the other software platforms? 
  • How would you differentiate JDK, JRE, JVM, and JIT?

Q. Is it possible to convert byte code into source code?
A. Yes. A Java decompiler is a computer program capable of reversing the work done by a compiler. In essence, it can convert back the byte code (i.e. the .class file) into the source code (i.e the .java file). There are many decompilers that exist today, but the most widely used JD - Java Decompiler is available both as stand-alone GUI program and as an eclipse plug-in.

Q. How do you protect your class files from illegally decompiled?
A. If you want to protect your Java class files from being decompiled, you can take a look at a Java obfuscator tool like yGuard or ProGuard, otherwise you will have to kiss your intellectual property good bye.

Q. When would you use a decompiler?
A.
  • When you have *.class files and you do not have access to the source code (*.java files). For example, some vendors do not ship the source code for java class files or you accidentally lost (e.g deleted) your source code, in which case you can use the Java decompiler to reconstruct the source file.
  • Another scenario is that if you generated your .class files from another language like a groovy script, using the groovyc command, you may want to use a Java decompiler to inspect the Java source code for the groovy generated class files to debug or get a better understanding of groovy integration with Java.
  • To ensure that your code is adequately obfuscated before releasing it into the public domain.
  • Fixing and debugging .class files when developers are slow to respond to questions that need immediate answers. To learn both Java and how the Java VM works. 

Q. What are the two flavors of JVM?
A.  Client mode is suited for short lived programs like stand-alone GUI applications and applets. Specially tuned to reduce application start-up time and memory footprint, making it well suited for client applications. For example:

c:\> java -client  MyProgram


Server mode is suited for long running server applications, which can be active for weeks or months at a time. Specially tuned to maximize peak operating speed. The fastest possible operating speed is more important than fast start-up time or smaller runtime memory footprint.

c:\> java -server  MyProgram


Q. How do you know in which mode your JVM is running?
A.

c:\> java -version 


java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)


Note: -client or -server must be the first argument or option of the JVM.

Q. What are the two different bits of JVM? What is the major limitation of 32 bit JVM?
A.  JVMs are available in 32 bits (-d32 JVM argument) and 64 bits (-d64 JVM argument). 64-bit JVMs are typically only available from JDK 5.0 onwards. It is recommended that the 32-bit be used as the default JVM and 64-bit used if more memory is needed than what can be allocated to a 32-bit JVM. The Sun Java VM cannot grow beyond ~2GB on a 32bit server machine even if you install more than 2GB of RAM into your server. It is recommended that a 64bit OS with larger memory hardware is used when larger heap sizes are required. For example, >4GB is assigned to the JVM and used for deployments of >250 concurrent or >2500 casual users.

Q. What are some of the JVM arguments you have used in your projects?
A.  To set a system property that can be retrieved using System.getPropety("name"); 

$java -Dname=value MyApp





To set the JVM mode: -client or -server

$ java -server MyApp

To set the classpath: -cp or -classpath

$java -cp library.jar MyApp

To set minimum and maximum heap sizes:

$java -Xms1024 -Xmx1024 MyApp


To set garbage collection options:

$ java -Xincgc MyApp


To enable assertion:

$java -ea



In the next post

  • How to monitor the JVM?

Labels: ,

Jun 14, 2014

Why use Java? Is Java 100% object oriented? What does Java comprise of? What is the main difference between the Java platform and the other software platforms? How would you differentiate JDK, JRE, JVM, and JIT?

Q. Why use Java?
A. One needs to use the best tool for the job, whether that tool is Java or not. When choosing a technology to solve your business problems, you need to consider many factors like development cost, infrastructure cost, ongoing support cost, robustness, flexibility, security, performance, etc.

Java provides client technologies, server technologies, and integration technologies to solve small scale to very large scale business problems.
  • Firstly, Java is a proven and  matured technology used in many mission critical projects, and  there are millions of developers world wide,  thousands of frameworks, tools, and libraries for it, and millions of sites, blogs, and books to find relevant information.
  • The emergence of open-source technologies has truly made Java a powerful competitor in the server and integration technology space. You can always find a proven framework that best solves your business problems.
  • The platform also comes with a rich set of APIs. This means developers spend less time writing support libraries, and more time developing content for their applications.
  • Built-in support for multi-threading, socket communication, and automatic memory management (i.e. automatic garbage collection). 

Q. Is Java 100% object oriented?
A. No. Is Java 100% OO? if yes why? if not, why not.


Q. What does Java comprise of?
A. Java comprises three components:

  • Java language comprising the syntax and semantics for programming has 3 editions – Java SE (Standard Edition), Java ME (Micro Edition), and Java EE (Enterprise Edition).

  • Java API (Application Programming Interface) is nothing but a set of classes and interfaces that come with the JDK. All these classes are written using the Java language and contains a library of methods for common programming tasks like manipulating strings and data structures, networking, file transfer, etc. The source *.java files are in the src.zip archive and the executable *.class files are in the rt.jar archive. There are 3 types of APIs available in Java technology.

Firstly, the official Java core API (i.e. src.zip and rt.jar) that is part of the JDK download. Secondly the optional Java APIs that can be downloaded separately. The specifications of the API is defined according to the Java Specification Request (JSR). For example, JSR 270 is the Java SE 6, JSR 168 is the portlet specification and so on. Finally, the third party libraries, which are unofficial APIs that are developed by third parties and and can be downloaded from the owner website. For example, http://www.apache.org/ where a number of handy APIs like Apache Commons, Apache Logging, and many more can be downloaded. Spring Framework, Hibernate, Struts, etc to name a few more.


Best Practice: It is a best practice to reuse proven and well tested APIs from Java Core, Java Optional, and third party APIs from Apache, Spring foundation, etc. It is also highly recommended to have these API documentation handy while coding to reuse appropriate well tested methods where possible without having to write your own.






  • Java Virtual Machine (JVM) is where all the classes written using the Java language and the API run on. The Java classes and API are platform independent but the JVM is not platform independent. You will find different downloads of the JVM for each Operating System (OS).


Q. What is the main difference between the Java platform and the other software platforms?
A. The Java platform is a software only platform, which runs on top of other hardware based platforms like Unix, Windows, etc.

  • Java source code is compiled into byte code that is understood by the JVM. Byte codes are the machine language of the JVM. The source code is easy for humans to understand. This enables a programmer to write programs. The byte code is very difficult for humans to understand. The first four bytes of every Java class (i.e the byte code) file are specified to be 0xCAFEBABE, a magic number that can help tools quickly differentiate likely class files from non class files.

  • Java Virtual Machine (JVM) – is a software that can be ported onto various hardware platforms. JVM interprets Java programs that have been compiled into byte code and usually stored in a “*.class” file. Byte code can be run on any computer that has Java interpreter (i.e the JVM). The JVM converts the byte code into platform (i.e. OS) specific executable machine language – a language understood by computers (i.e. binary 0s and 1s). The machine language is almost impossible for humans to understand. Every OS has its own machine language. 

Q. How would you differentiate JDK, JRE, JVM, and JIT?
A.There is no better way to get the big picture than a diagram.



JDK: You can download a copy of the Java Development Kit (JDK). Be sure to download a copy that is appropriate for your operating system and processor. The JDK contains the compiler (i.e. javac) and other programs and libraries (i.e. jar files) useful for Java development.



JRE: Java Runtime Environment is an implementation of the JVM. The JDK typically includes the Java Runtime Environment (JRE) which contains the virtual machine and other dependencies to run Java applications. The JRE is also available as a separate download if you have no interest in development and just want to be able to run Java applications. Remember to download a copy that is appropriate for your operating system and processor.



JIT: A JIT is a code generator that converts Java byte code into native machine code. Java programs invoked with a JIT generally run much faster than when the byte code is executed by the interpreter. The JIT compiler is a standard tool that is part of the JVM and invoked whenever you use the Java interpreter command. You can disable the JIT compiler using the -Djava.compiler=NONE option to the Java VM. You might want to disable the JIT compiler if you are running the Java VM in remote debug mode, or if you want to see source line numbers instead of the label (Compiled Code) in your Java stack traces. 

  • Is it possible to convert byte code into source code?
  • What are the 2 flavors of JVM?
  • What are the 2 bits of JVM?
  • What are some of the JVM arguments you have used in your project?
  • How do you monitor the JVMs?

Labels: ,