Sunday, March 9, 2014

JVM Execution

Hi,

Very tough question? What really means Byte code? How it looks? What is JVM instruction?   Whether JVM uses OS if it has own instruction set and Execution Unit? How it works?

The answer is here? You have already gone through my previous Posts, Otherwise go through below Posts.

JVM Basic

Java Method Area

Java Heap Area

Java Stack

Now find out the above answers below

32 and 64 bit JVM

When Javac compiles Java Source file, it create class file which contains Byte code,  Byte code is nothing but included with JVM instruction Set.

Want to Read more about JVM instruction Set.

Wiki bytecode instruction

From Oracle

Java Ref 
 
The designers of Java chose to use a combination of compilation and interpretation. Programs written in Java are compiled into machine language, but it is a machine language for a computer that doesn't really exist. This so-called "virtual" computer is known as the Java Virtual Machine, or JVM. The machine language for the Java Virtual Machine is called Java bytecode. There is no reason why Java bytecode couldn't be used as the machine language of a real computer, rather than a virtual computer. But in fact the use of a virtual machine makes possible one of the main selling points of Java: the fact that it can actually be used on any computer. All that the computer needs is an interpreter for Java bytecode. Such an interpreter simulates the JVM in the same way that Virtual PC simulates a PC computer. (The term JVM is also used for the Java bytecode interpreter program that does the simulation, so we say that a computer needs a JVM in order to run Java programs. Technically, it would be more correct to say that the interpreter implements the JVM than to say that it is a JVM.)
Of course, a different Java bytecode interpreter is needed for each type of computer, but once a computer has a Java bytecode interpreter, it can run any Java bytecode program. And the same Java bytecode program can be run on any computer that has such an interpreter. This is one of the essential features of Java: the same compiled program can be run on many different types of computers.


 



The JVM/JRE uses Java bytecode as its instruction set and each JVM needs to be compiled on and be runnable on the native/local hardware (and therefore the local instruction set). This diagram from Wikipedia illustrates this well I think:

enter image description here

he JRE/JVM needs to be compiled for the specific hardware it runs on, though the Java bytecode definitions and interpretations by the JVM itself stay the same. As you point out, the Java bytecode can be seen as a kind of abstraction layer between the Java source code and the local machine/binary code. It does allow for a separation of concerns between the typical Java programmer and needing to know anything machine-specific, as almost all of that is handled by the JVM/JRE.

An ISA (Instruction Set Architecture) specifies the entire set of disciplines and techniques that apply to writing low-level software that runs directly on the CPU. It includes a set of opcodes, which are uncomposable direct CPU commands. The JVM recognizes its own set of bytecodes (i.e. 8-bit opcodes) that direct the JVM to carry out interpreter-primitive instructions. So, yes, the bytecode specification makes up part of the JVM's ISA.
The JVM iterates through the list of opcodes executing them one by one using its own memory to emulate hardware components (e.g. stack, register, main memory) and using primitive arithmetic and logic operations to emulate the ALU. These emulated components also make up the JVM's ISA. This is the basic construction of any interpreter, give or take. However, to improve the runtime of Java applications, the JVM compiles "hotspots" to machine-specific code for optimal performance. Hotspots are sections of the code that are ran frequently. This is known as "Just-In-Time" compilation, and can be done while the program is executing. This technique brings Java's performance a lot closer to that of compiled languages. JIT is used in the .NET framework as well.
Each operating system has its own JVM implementation, which may also vary by the device's ISA. For example, you may have a JVM written for Linux-Arm, Linux-x86, or Windows-x86. The JVM itself may be written in a platform-independent (sort of anyway) language like C, but its JIT compiler must support compilation to the device's instruction set.

The Java Virtual Machine (JVM) provides the entire environment for running a Java program. It integrates with the Operating System, loads classes, and runs programs. The Just-In-Time compiler (JIT) is just a small piece that can be disabled (-Xint) but when enabled, provides a useful improvement in performance. There have been implementations of the JVM that didn't include a JIT, and implementations that worked by pre-compiling Java to machine code just the same as traditional languages, such as C or C++.

  he key is the native language of the JVM: the Java bytecode. Any language can be compiled into bytecode which the JVM understands - all you need for this is a compiler emitting bytecode. From then on, there is no difference from the JVM's point of view. So much so that you can take a compiled Scala, Clojure, Jython etc. class file and decompile it (using e.g. JAD) into normal looking Java source code.
You can find more details about this in the following articles / threads:
I am not aware of any fundamental changes in the Java 5 or 6 JVMs which would have made it possible or easier for (code compiled from) other languages to run on it. In my understanding the JVM 1.4 was more or less as capable in that respect as JVM 6 (there may be differences though; I am not a JVM expert). It was just that people started to develop other languages and/or bytecode compilers in the first half of the decade, and the results started to appear (and become wider known) around 2006 when Java6 was published.
However, all these JVM versions share some limitations: the JVM is statically typed by nature, and up to release 7, did not support dynamic languages. This has changed with the introduction of invokedynamic, a new bytecode instruction which enables method invocation relying on dynamic type checking.

Java Virtual Machine Figure Two


System.out.println("Hello world!"); 
 
can be converted to below byte code
 
   0 getstatic #6 <Field java.lang.System.out Ljava/io/PrintStream;>
   3 ldc #1 <String "Hello world!"> 
   5 invokevirtual #7 <Method java.io.PrintStream.println(Ljava/lang/String;)V> 
   8 return  

Take example java source Code
void test() {
    int i;
    for (i = 0; i < 100; i++) {
        ;    // Loop body is empty
    }
}
 
Converts to below byte code

0   iconst_0       // Push int constant 0
1   istore_1       // Store into local variable 1 (i=0)
2   goto 8         // First time through don't increment
5   iinc 1 1       // Increment local variable 1 by 1 (i++)
8   iload_1        // Push local variable 1 (i)
9   bipush 100     // Push int constant 100
11  if_icmplt 5    // Compare and loop if less than (i < 100)
14  return         // Return void when done
 
For For loop main instruction set is

5   iinc 1 1       // Increment local variable 1 by 1 (i++)
8   iload_1        // Push local variable 1 (i)
9   bipush 100     // Push int constant 100
11  if_icmplt 5    // Compare and loop if less than (i < 100)
 
And then it converts to machine/OS specific instruction

Read Out More about this

Read Out Wiki about Java Byt ecode
 

Saturday, March 8, 2014

Java Stack


Yes, Java is Stack based Architecture Virtual Machine,

A virtual machine (VM) is a high level abstraction on top of the native operating system, that emulates a physical machine. Here, we are talking about process virtual machines and not system virtual machines. A virtual machine enables the same platform to run on multiple operating systems and hardware architectures. The Interpreters for Java and Python can be taken as examples, where the code is compiled into their VM specific bytecode. The same can be seen in the Microsoft .Net architecture, where code is compiled into intermediate language for the CLR (Common Language Runtime).
What should a virtual machine generally implement? It should emulate the operations carried out by a physical CPU and thus should ideally encompass the following concepts:
  • Compilation of source language into VM specific bytecode
  • Data structures to contains instructions and operands (the data the instructions process)
  • A call stack for function call operations
  • An ‘Instruction Pointer’ (IP) pointing to the next instruction to execute
  • A virtual ‘CPU’ – the instruction dispatcher that
    • Fetches the next instruction (addressed by the instruction pointer)
    • Decodes the operands
    • Executes the instruction
There are basically two main ways to implement a virtual machine: Stack based, and Register based. Examples of stack based VM’s are the Java Virtual Machine, the .Net CLR, and is the widely used method for implementing virtual machines. Examples of register based virtual machines are the Lua VM, and the Dalvik VM (which we will discuss shortly). The difference between the two approaches is in the mechanism used for storing and retrieving operands and their results.
Stack Based Virtual Machines
A stack based virtual machine implements the general features described as needed by a virtual machine in the points above, but the memory structure where the operands are stored is a stack data structure. Operations are carried out by popping data from the stack, processing them and pushing in back the results in LIFO (Last in First Out) fashion. In a stack based virtual machine, the operation of adding two numbers would usually be carried out in the following manner (where 20, 7, and ‘result’ are the operands):
stackAdd
  1. POP 20
  2. POP 7
  3. ADD 20, 7, result
  4. PUSH result
Because of the PUSH and POP operations, four lines of instructions is needed to carry out an addition operation. An advantage of the stack based model is that the operands are addressed implicitly by the stack pointer (SP in above image). This means that the Virtual machine does not need to know the operand addresses explicitly, as calling the stack pointer will give (Pop) the next operand. In stack based VM’s, all the arithmetic and logic operations are carried out via Pushing and Popping the operands and results in the stack.
Register Based Virtual Machines
In the register based implementation of a virtual machine, the data structure where the operands are stored is based on the registers of the CPU. There is no PUSH or POP operations here, but the instructions need to contain the addresses (the registers) of the operands. That is, the operands for the instructions are explicitly addressed in the instruction, unlike the stack based model where we had a stack pointer to point to the operand. For example, if an addition operation is to be carried out in a register based virtual machine, the instruction would more or less be as follows:
registerAdd
  1. ADD R1, R2, R3 ;        # Add contents of R1 and R2, store result in R3
As I mentioned earlier, there is no POP or PUSH operations, so the instruction for adding is just one line. But unlike the stack, we need to explicitly mention the addresses of the operands as R1, R2, and R3. The advantage here is that the overhead of pushing to and popping from a stack is non-existent, and instructions in a register based VM execute faster within the instruction dispatch loop.
Another advantage of the register based model is that it allows for some optimizations that cannot be done in the stack based approach. One such instance is when there are common sub expressions in the code, the register model can calculate it once and store the result in a register for future use when the sub expression comes up again, which reduces the cost of recalculating the expression.
The problem with a register based model is that the average register instruction is larger than an average stack instruction, as we need to specify the operand addresses explicitly. Whereas the instructions for a stack machine is short due to the stack pointer, the respective register machine instructions need to contain operand locations, and results in larger register code compared to stack code

 Now the question can be Why JVM is stack based VM

There are a few attributes of a stack-based VM that fit in well with Java's design goals:
  1. A stack-based design makes very few assumptions about the target hardware (registers, CPU features), so it's easy to implement a VM on a wide variety of hardware.
  2. Since the operands for instructions are largely implicit, the object code will tend to be smaller. This is important if you're going to be downloading the code over a slow network link.
Going with a register-based scheme probably means that Dalvik's code generator doesn't have to work as hard to produce performant code. Running on an extremely register-rich or register-poor architecture would probably handicap Dalvik, but that's not the usual target - ARM is a very middle-of-the-road architecture.

Now Read out full Detail about Java Stack:

Java Stack

And Read out what is Native Stack

Native Stack 

JVM Method Area

1. Understand What is Method Area


https://www.artima.com/insidejvm/ed2/jvm5.html

2. View Example


http://www.youtube.com/watch?v=a5GzF2fSSCE

3. Advance

1. Using verbose command to under 

Step 1: Create the Java source file say 
public abstract class JVM1 implements A {

    static int a=7;
    final static int b=7;
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
System.out.println("Hi");    }
    void test(int g)
    {
        a=g;
    }
}


Step 2:  Compile the above file 'javac JVM1.java'

Step 3: Run the below command javap -verbose JVM1

All the Detail will be displayed about class file.

JVM Heap Area


Concept :


https://www.artima.com/insidejvm/ed2/jvm6.html

Example:


http://www.youtube.com/watch?v=c-A7ZzxjWUI

Advance:


1. Generating Class file

JavaCompiler jc=ToolProvider.getSystemJavaCompiler();
 StandardJavaFileManager fileManager = jc.getStandardFileManager(null,null,null);
System.out.println(fileManager);
boolean a=Compiler.compileClass(.class);
System.out.println(a);

2. http://javaeesupportpatterns.blogspot.in/2011/11/outofmemoryerror-java-heap-space.html

3. Memory Tunning

http://www.ashishsharma.me/2011/08/java-heap-space.html

Tuesday, March 4, 2014

JAAS , J_security_check, JSP Security

JVM Internal Understanding

1. JVM Overview

Java Virtual Machine (JVM) is an abstract computing machine. Java Runtime Environment (JRE) is an implementation of the JVM. Java Development Kit (JDK) contains JRE along with various development tools like Java libraries, Java source compilers, Java debuggers, bundling and deployment tools.

JVM becomes an instance of JRE at runtime of a java program. It is widely known as a runtime interpreter. The Java virtual machine (JVM) is the cornerstone on top of which the Java technology is built upon. It is the component of the Java technology responsible for its hardware and platform independence. JVM largely helps in the abstraction of inner implementation from the programmers who make use of libraries for their programmes from JDK

Like a real computing machine, JVM has an instruction set and manipulates various memory areas at run time. Thus for different hardware platforms one has corresponding implementation of JVM available as vendor supplied JREs. It is common to implement a programming language using a virtual machine. Historicaly the best-known virtual machine may be the P-Code machine of UCSD Pascal.

A Java virtual machine instruction consists of an opcode specifying the operation to be performed, followed by zero or more operands embodying values to be operated upon. From the point of view of a compiler, the Java Virtual Machine (JVM)is just another processor with an instruction set, Java byte code, for which code can be generated. Life cycle is as follows, source code to byte code to be interpreted by the JRE and gets converted to the platform specific executable ones.
 
The Java Virtual Machine (JVM) executes the Java programs bytecode (.class file). The bytecode is generated after the compilation of the program by the Java compiler. The Java Virtual Machine is the software program and data structures that is on the top of the hardware. The Java virtual machine is called "virtual" because it is an abstract computer (that runs compiled programs) defined by a specification. The Java Virtual Machine is the abstraction between the compiled Java program and used hardware and operating system. The JVM is part of both the JDK and the JRE. It is the 'engine' that executes java bytecodes, performs garbage collection and does just-in-time compilation. The JVM for different systems is not same. JVM forms the Java Runtime Environment (JRE) for the program execution. Each operating system and CPU architecture requires a different JRE. The JRE comprises a set of base classes, which are an implementation of the base Java API, as well as a JVM.

Sun’s JVM

Sun’s implementations of the Java virtual machine (JVM) is itself called as JRE. Sun’s JRE is available as a separate application and also available as part of JDK. Sun’s Java Development Tool Kit (JDK) comes with utility tools for byte code compilation “javac”. Then execution of the byte codes through java programs using “java” and many more utilities found in the binary directory of JDK. ‘java’ tools forks the JRE. Implementation of JVMs are also actively released by other companies besides Sun Micro Systems.

JVM for other languages

A JVM can also be used to implement programming languages other than Java. For example, Ada source code can be compiled to Java bytecode, which may then be executed by a Java virtual machine (JVM). That is, any language with functionality that can be expressed in terms of a valid class file can be hosted by the Java virtual machine (JVM). Attracted by a generally available, machine-independent platform, implementers of other languages are turning to the Java virtual machine (JVM) as a delivery vehicle for their languages. PHP with Quercus is such an example.

Just-in-time Compiler (JIT)

JIT is the part of the Java Virtual Machine (JVM) that is used to speed up the execution time. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.



 The JDK (Java Development Kit) is used for developing java applications. The JDK includes JRE, set of API classes, Java compiler, Webstart and additional files needed to write Java  applications. The JDK (Java Development Kit) contains software development tools which are used to compile and run the Java program. Both JDK and JRE contains the JVM.
The above picture helps you to understand the difference between JDK, JRE, JVM

 OpenJDK is the open-source implementation of the Java SE 7 JSR (JSR 336). Now there is almost no difference between the Oracle JDK and the OpenJDK. Last year, Oracle took this decision : Moving to OpenJDK as the official Java SE 7 Reference Implementation

 Java Virtual Machine :




JVM Memory Area

 

Heap memory

The heap memory is the runtime data area from which the Java VM allocates memory for all class instances and arrays. The heap may be of a fixed or variable size. The garbage collector is an automatic memory management system that reclaims heap memory for objects.
  • Eden Space: The pool from which memory is initially allocated for most objects.
  • Survivor Space: The pool containing objects that have survived the garbage collection of the Eden space.
  • Tenured Generation: The pool containing objects that have existed for some time in the survivor space.

Non-heap memory

Non-heap memory includes a method area shared among all threads and memory required for the internal processing or optimization for the Java VM. It stores per-class structures such as a runtime constant pool, field and method data, and the code for methods and constructors. The method area is logically part of the heap but, depending on the implementation, a Java VM may not garbage collect or compact it. Like the heap memory, the method area may be of a fixed or variable size. The memory for the method area does not need to be contiguous.
  • Permanent Generation: The pool containing all the reflective data of the virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas.
  • Code Cache: The HotSpot Java VM also includes a code cache, containing memory that is used for compilation and storage of native code.