Wednesday, July 23, 2014

The Java Virtual Machine (JVM)



What is the Java Virtual Machine (JVM), and what it does?

First of all, you should read this article about Java.

Java is a high-level language, which means that is closer to our natural language than machine code, the set of instructions a computer uses to construct and run a program.

Java programming language was designed at SUN, by James Goslin, to allow programmers to write code that could run on any platform without the need for rewriting or recompilation for each separate platform.

A Java virtual machine makes that possible, by defining an abstract -- not a real -- machine or processor that translates the Java code to instructions that a specific platform understands. It interprets compiled Java binary code (bytecode) and translates it for a computer's processor (or "hardware platform") so that it can perform a Java program's instructions.

A Java Virtual Machine is an implementation of the Java Virtual Machine Specification, which is created and reviewed by the Java Community Process (JCP).  The Specification defines an instruction set, a set of registers, a stack, a "garbage heap", and a method area.


JVM architecture
In a Java program, the source code is compiled to Java bytecode, which is verified, interpreted or JIT-compiled for the native architecture.
A JVM can either interpret the bytecode one instruction at a time (mapping it to a real processor instruction) or the bytecode can be fully compiled for the real processor using what is called a just-in-time (JIT) compiler, which normally achieves a greater execution speed.
The Java APIs and JVM together make up the Java Runtime Environment (JRE).

When a Java project is built, the source code (*.java source files) is translated to Java bytecode (*.class files), taking high-level code one step closer to the machine code. The bytecode is a collection of compact instructions, easier for a machine to interpret.
When running a Java application on a computer, cellphone, or any other Java-enabled platform, the Java bytecode is passed to the Java Virtual Machine. The interpreter in the JVM usually starts compiling the entire bytecode at runtime, following the principles of just-in-time (JIT) compilation.


JVM advantages
The main advantage in using a JVM is the compatibility. Since applications run in a virtual machine instead of directly on the hardware, the developer can program and build the application once, and executed it on every device that has a Java Virtual Machine. This principle has given birth to the Java slogan: “Write once, run everywhere.”

But there are other benefits.
One of the most important is the relative security of Java programs, as a result of the Java Virtual Machine. A program running in a virtual machine is far less likely to disrupt the user’s operating system, or corrupt data files, if errors occur. Also, JVM has an efficient way to achieve memory protection.


Heap
The heap is the area of memory used by the JVM. The heap is divided into generations:
 - Young Generation: stores short-lived objects that are created and immediately garbage collected.
 - Old Generation (or Tenured Generation): objects that persist longer are move to this area.

The Permanent Generation (permgen) was removed in Java 8. It was used for class definitions and associated metadata. Permanent generation was not part of the heap. Originally there was no permanent generation, and objects and classes were stored together in the same area. But as class unloading occurs much more rarely than objects are collected, moving class structures to a specific area allowed significant performance improvements.

For more information on this subject, please read the JVM Memory Structure article. (to be created)


Most known JVMs
Creator
Name
Description
Oracle
Hotspot (*)
Originally developed by Animorphic, a company that was acquired by Sun in 1997. Sun was acquired by Oracle in 2009.
It is a Java virtual machine for desktops and servers, maintained and distributed by Oracle. It features techniques such as just-in-time compilation and adaptive optimization designed to improve performance.
Oracle
JRockit (*)
It was originally developed by Appeal, a Swedish company that was acquired by BEA in 2002. BEA became part of Oracle in 2008.
This JVM was created with the intention of being used for  optimized for large applications requiring long running tasks, a lot of memory and a scalable environment.
IBM
J9
It is the basis of multiple IBM Java offerings, including WebSphere and the IBM JDK.
This JVM aimed at portability to different platforms, from mobile phones to zSeries mainframes.

(*) Hotspot and JRockit will be integrated in a single JVM release.  For more on this issue, read this article from “Henrik on Java” blog: https://blogs.oracle.com/henrik/entry/oracles_jvm_strategy


References
 - Oracle’s Hotspot JVM: http://en.wikipedia.org/wiki/HotSpot
 - Oracle’s JRockit JVM: http://en.wikipedia.org/wiki/JRockit
 - Java home: http://java.com
 - Oracle’s Java Tech page: http://www.oracle.com/technetwork/java/index.html
 - Java and JVM specifications: http://docs.oracle.com/javase/specs/

Friday, November 8, 2013

Checking installed packages (libraries) in RH Linux... with RPM

Before you start installing applications in Linux, as in any other system, it is essential to check the existence of all prerequisites.

Some times these prerequisites are system packages (libraries) that applications use. Usually I don't need to install them, because this is done by a System Administrators team. When I do install them, then the validation is automatically done... right? But when the installation is done by the Sys Admin team, I end up having to run the validation. A little bit of additional work, but it is always usefull.


So, how to make this validation, and check if all packages that are a requisite for your applications are available in the server? If using Red Hat Linux, just use RPM command (Red Hat Package Manager). This will list all the installed packages . 

Relevant parameters:
-q (query)
-a (all)
--qf (query format)

A simple (full) list would be obtained by querying all packages:
rpm -qa
  
Usually, I also want to know if the package is 32 or 64 bit. So I add a query format to the output, that it includes the package name, version and bit value: 
rpm -qa --qf '%{NAME}-%{VERSION} (%{ARCH})\n' | sort


The result would be something like this:

acl-2.2.39(x86_64)
acpid-1.0.4(x86_64)
alchemist-1.0.36(x86_64)
alsa-lib-1.0.17(i386)
alsa-lib-1.0.17(x86_64)
alsa-lib-devel-1.0.17(x86_64)
alsa-utils-1.0.17(x86_64)
(…)


Now you have all the information needed for this validation. Either check it on the screen, or redirect the output to a file and use it to make the validations you need.

Saturday, June 29, 2013

Getting system information from Linux


 Some tips on how to extract system information on a Linux server:


Which version of Linux?

To learn which Linux OS you're running, type this command:
$ cat /proc/version
Linux version 2.6.18-164.el5 (mockbuild@x86-003.build.bos.redhat.com) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)) #1 SMP Tue Aug 18 15:51:48 EDT 2009

This will give some clues, but can be misleading. For instance, from this output you can see that “Red Hat 4.1” is mentioned, but the string “el5” clearly identifies that this is a Red Hat 5 distribution.

In a Red Hat distribution, the easiest way is to check the redhat-release file:
$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 5.4 (Tikanga)

So, this way there’s no doubt: this is a RHEL 5.4.


Which OS architecture type: 32-bit or 64-bit?

To find out if you have a 32-bit or 64-bit OS, use this command:
$ uname -i
x86_64

This will give the answer on-the-fly. But you can check more details:
$ uname -a
Linux vkdsso04.telecom.pt 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux

With these outputs, you’ll be able to identify which OS architecture you have:
  • x86_64 = 64-bit
  • ia64 = 64-bit
  • i386 = 32-bit


Which hardware?

The fastest way to check the server hardware is to use the examples below.

For CPU info, check the contents of cpuinfo file:
$ cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 15
model name      : Intel(R) Xeon(R) CPU           X5660  @ 2.80GHz
stepping        : 1
cpu MHz         : 2793.000
cache size      : 12288 KB
fpu             : yes
fpu_exception   : yes
cpuid level     : 11
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 lm constant_tsc up ida nonstop_tsc pni cx16 lahf_lm
bogomips        : 5586.00
clflush size    : 64
cache_alignment : 64
address sizes   : 36 bits physical, 48 bits virtual
power management: [8]

For RAM info, check the contents of meminfo file:
$ cat /proc/meminfo
MemTotal:      3866476 kB
MemFree:        394056 kB
Buffers:        222236 kB
Cached:        1146980 kB
SwapCached:     202056 kB
Active:        2897996 kB
Inactive:       334572 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:      3866476 kB
LowFree:        394056 kB
SwapTotal:     2064376 kB
SwapFree:      1747252 kB
Dirty:              80 kB
Writeback:           0 kB
AnonPages:     1848948 kB
Mapped:          22968 kB
Slab:           211028 kB
PageTables:      10652 kB
NFS_Unstable:        0 kB
Bounce:              0 kB
CommitLimit:   3997612 kB
Committed_AS:  2449840 kB
VmallocTotal: 34359738367 kB
VmallocUsed:      2164 kB
VmallocChunk: 34359735687 kB
HugePages_Total:     0
HugePages_Free:      0
HugePages_Rsvd:      0
Hugepagesize:     2048 kB

For swap info, check the contents of swaps file:
$ cat swaps
Filename                                Type            Size    Used    Priority
/dev/mapper/VolGroup00-LogVol01         partition       2064376 317124  -1

For filesystems info, use the df command:
$ df -h
Filesystem                    Size  Used  Avail  Use%  Mounted on
/dev/mapper/vg_base-lv_root   28G   4.8G    22G   19%  /
tmpfs                        814M   176K   814M    1%  /dev/shm
/dev/sda1                    485M   120M   340M   27%  /boot


If you’re still not satisfied with the information provide, you can also use additional commands.

The top command:
$ top
top - 19:54:00 up 164 days,  7:53,  1 user,  load average: 0.00, 0.00, 0.00
Tasks:  87 total,   2 running,  85 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.3%si,  0.0%st
Mem:   3866476k total,  3482464k used,   384012k free,   222236k buffers
Swap:  2064376k total,   317124k used,  1747252k free,  1147024k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
    1 root      15   0 10348  260  204 S  0.0  0.0   0:10.36 init
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 migration/0
    3 root      34  19     0    0    0 S  0.0  0.0   0:00.22 ksoftirqd/0
    4 root      10  -5     0    0    0 S  0.0  0.0  18:49.94 events/0
    5 root      13  -5     0    0    0 S  0.0  0.0   0:00.00 khelper
   36 root      11  -5     0    0    0 S  0.0  0.0   0:00.00 kthread
   40 root      10  -5     0    0    0 S  0.0  0.0   0:01.53 kblockd/0
   41 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 kacpid
  100 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 cqueue/0


The free command (-m flag for megabytes):
$ free -m
             total       used       free     shared    buffers     cached
Mem:          3775       3400        375          0        217       1120
-/+ buffers/cache:       2063       1712
Swap:         2015        309       1706