Thursday, July 9, 2009

JVM Internals Series -Part 1

A lot of java developers tend to be unaware about the basics of the internals of the JVM. This series aims to look at the internals of the JVM and explain in a simple way
    What is the Java Virtual Machine
    How does the JVM function


What is the Java Virtual Machine ?

When you talk of the JVM there are three aspects which we speak of

  1. Specification
  2. Concrete Implementation
  3. Runtime

The specification is a concept, Concrete implementations which exist on different platforms and are available from different vendors like IBM, Sun etc are either all software or a mix of hardware and software. A Runtime instance hosts a single running java application.

A runtime instance has one function and that is to run one java application. Whenever a java application runs, a runtime instance is born. Thus the number of runtime instances on a machine are equal to the number of applications which are running. The JVM starts with the invocation of a main() method which needs to be public, static, void and takes a String array as an argument. The main method thus serves as the initial thread for the running application. This can in turn spawn other threads. Inside the VM, threads come in two flavors, daemon and non-daemon. A daemon thread is a thread which is normally used by the VM itself, like the thread which runs the garbage collection. However an application can mark any thread it creates as a daemon. A java application continues to execute until there are any non-daemon threads alive(parallely the VM runtime instance continues to exist). When all non-daemon threads of an application exit, the virtual machine instance exits.

JVM Architecture


The VM needs memory to store a lot of information, like the bytecode, program arguments, objects which have been instantiated etc. Some of the information stored in the memory is shared across all application threads, while other information may be unique to individual threads.

Each instance of a JVM has one method area and a heap. These areas are shared by all threads running within the instance.
The information regarding the type is loaded from the class files when the JVM loads a class file. This information is stored in the method area. All the objects which are instantiated placed on the heap.
As each new thread is created, it is assigned its own pc register(program counter) and java stack. If the thread is executing a java method (not a native method), the counter in the pc register points to the next instruction to execute. The java stack comprises of frames. A frame consists of the state of one method invocation. When a thread invokes a method, a new frame is pushed on the thread’s stack. On completion of the method execution, the VM pops and discards the frame. No other thread can access another threads pc register or java stack.

This gives a brief overview of the java virtual machine’s architecture. In the next post,I will cover data types and the class loading subsystem.


Wednesday, July 8, 2009

Groovy Class for Analyzing java code

A lot of times when i sit down to review code, there are some basic things like the size of the class, the total lines, the maximum width of a line etc, which can help in figuring out bad smells in code. In order to automate the task i wrote a small groovy class which can be run on a directory to figure out these metrices. Although there are lots of tools available, the simplicity and the customization which can be done to a small utility class far outweigh the advantages.

class CodeAnalyzer {
int lineCount
int maxLineWidth
int widestLineNumber
int totalChars
int lineSize
def List findJavaFiles(File parentDir){
def files=new ArrayList()
parentDir.eachFileRecurse{file ->
if(file.isFile() && file.name.endsWith(".java"))
files.add(file)
}
return files
}
def analyzeFile(File javaFile){
javaFile.eachLine{line ->
measureLine(line)
}
println "fileName =" + javaFile.name
println "lineCount =" + lineCount
println "maxLineWidth ="+maxLineWidth
println "widestLineNumber ="+widestLineNumber
println "totalChars ="+totalChars
}
def measureLine(String line){
lineCount++
lineSize=line.length()
totalChars +=lineSize
recordWidestLine(lineSize)
}
def recordWidestLine(int lineSize){
if(lineSize >maxLineWidth){
maxLineWidth =lineSize
widestLineNumber=lineCount
}
}
def getMeanLineWidth(){
return totalChars/lineCount
}
}
The semantics of usage are pretty simple. I will show the groovy way of invoking it
CodeAnalyzer codeAnalyzer=new CodeAnalyzer()
codeAnalyzer.findJavaFiles(new File("D:/MCubeRTV2.0/Source/java/com/velozglobal/mcube/agents")).each{file ->
codeAnalyzer.analyzeFile(file)
}

Monday, July 6, 2009

Eclipse templates for auto code generation

Working with an IDE tends to make life quiet simple. However we still do not leverage the full potential of using an IDE like Eclipse. During development, i have found creating a few templates really helps to speed up things. Here are the few most common ones i use which are not part of the standard templates. 1) Log4J Logging
a) Create Logger
${:import(org.apache.log4j.Logger)}
private static final Logger logger = Logger.getLogger(${enclosing_type}.class);
b) LogInfo
logger.info(${word_selection}${});${cursor}
c) LogError
logger.error(${errorMessage}, ${e});
d) LogDebug
if(logger.isDebugEnabled())
logger.debug(${word_selection}${});${cursor}
2) Constants
a) Const
private static final ${type} ${name} = new ${type} ${cursor};
3) Map Iteration
a) MapIter
for(Map.Entry entry :
${map:var(java.util.Map)}.entrySet()) {
${key} key = entry.getKey();
${value} value = entry.getValue();
${cursor}
}
4) JUnit
a) Test Creation
public void test${name}() throws Exception {
${cursor}
}

Tuesday, June 30, 2009

Automate Unit Test Execution

A lot of times, when doing TDD, we make small refactorings and then have to manually go ahead and run the tests to figure out whether the changes we made are consistent and have not broken the tests. Wouldn't it be good if the tests were run on each Save to ascertain that we have not broken any of the tests. This also lets us know which changes are the culprit. Using eclipse we can automate running the unit tests on each change and save we make. Following are the steps needed in order to accomplish this.
  1. Create a main class
import junit.framework.TestSuite;
import junit.textui.TestRunner;
public class TestBootstrap
{
public static void main(String[] args)
{
TestSuite testSuite=new TestSuite();
testSuite.addTestSuite(TestClass.class);
TestRunner.run(testSuite);
}
}

2. Create an ant build file

<project>
<target name="eclipse-test-runner">
<java classname="TestBootstrap" classpath="bin:libs/junit.jar"> <java> </target>
</project>

3. Create a Builder in eclipse
Open project properties: Right click project -> Propertie. Create new ant builder step after the Java Builder in eclipse: Builders -> new -> Ant Builder. Give your builder a nice name.
Main: Select the build file and the base directory
Targets: Select your ANT target for: “After Clean”, “Manual Build”, and “Auto Build” (most important) Build Options: Check “Specify working set” and select all of your source folders.
And thats all, now every time you save a file, eclipse will run the ant target specified in your build file, which incidentally calls your TestBootstrap's main and invoked all the tests.


Monday, June 15, 2009

Reading an RSS feed in groovy


Here's a small script to read RSS feeds from any site using groovy. You can customize it to read from multiple feeds by feeding the url's from a file etc.

def url='http://feeds.dzone.com/dzone/frontpage'
def channel=new XmlParser().parse(url).channel[0]
println channel.title.text()
println channel.link.text()
println channel.description.text()
println '\nStories:\n---------'
def items=channel.item
for(item in items){
println item.title.text()
println item.link.text()
println item.description.text()
println'-------'
}

The emperor and me beaching

The Devil next door

Kaiser The Emperor