Recently my curiosity towards learning something new , bumped me into a framework called Kilim. Ya that's right , not sure how many of you have heard about it.
Goal of this framework - to introduce Actor based concurrency to java ala. making the model similar to Erlang and Scala
Why do we need this switch from thread based model to Actor based model when it comes to concurrency - thread based model depends on using shared memory when it comes to communicating between threads. Actor based model takes a slightly different approach of using Mailboxes to communicate.
So whats the advantage ? - well when coded against Actor based model we don't need to worry about thread locks or use any kind of synchronization blocks. Actors are guaranteed to run on multi-core processors. Since there is no limitation or a dependency on shared memory actors can be scheduled on any core or a processor. Improves the overall performance and the scalability of system.
How to use Kilim? sample code - here
Also Read
Java development 2.0: Ultra-lightweight Java web services with Gretty
Instrumentation - Querying the memory usage of a Java object
10 things you didn't know about java performance monitoring
5 things you didn't know about java.util.concurrent
The Clean Coder
Friday, December 09, 2011
Thursday, December 08, 2011
Querying the memory usage of a Java object
Creating the instrumentation agent class - would work with Jdk 5 and above
The JVM will pass to our method an implementation of the Instrumentation interface, defined in java.lang.instrument. In turn, this interface defines the method getObjectSize(). So for example, if we want to measure the memory usage of an instance of SomeClass, our agent code would look as follows:
create manifest.txt with
Premain-Class: mypackage.MyAgent
execute this to create a jar -
jar -cmf manifest.txt agent.jar mypackage/MyAgent.class
Run the application with the agent -
java -javaagent:agent.jar -cp . com.mypackage.Main
Accessing the Instrumentation object from within our application -
Now, provided the agent is included in the JVM command line parameters as above, then from anywhere in our application we can call MyAgent.getObjectSize() to query the memory size of an object created by our Java application
Note that the getObjectSize() method does not include the memory used by other objects referenced by the object passed in.
Next -- Memory footprint of java datatypes
The JVM will pass to our method an implementation of the Instrumentation interface, defined in java.lang.instrument. In turn, this interface defines the method getObjectSize(). So for example, if we want to measure the memory usage of an instance of SomeClass, our agent code would look as follows:
import java.lang.instrument.*;
import com.somepackage.SomeClass;
public class MyAgent {
public static void premain(String args, Instrumentation inst) {
SomeClass obj = new SomeClass();
long size = inst.getObjectSize(obj);
System.out.println("Bytes used by object: " + size);
}
}
Package the agent into a jar -create manifest.txt with
Premain-Class: mypackage.MyAgent
execute this to create a jar -
jar -cmf manifest.txt agent.jar mypackage/MyAgent.class
Run the application with the agent -
java -javaagent:agent.jar -cp . com.mypackage.Main
Accessing the Instrumentation object from within our application -
public class MyAgent {
private static volatile Instrumentation globalInstr;
public static void premain(String args, Instrumentation inst) {
globalInstr = inst;
}
public static long getObjectSize(Object obj) {
if (globalInstr == null)
throw new IllegalStateException("Agent not initted");
return globalInstr.getObjectSize(obj);
}
}
Now, provided the agent is included in the JVM command line parameters as above, then from anywhere in our application we can call MyAgent.getObjectSize() to query the memory size of an object created by our Java application
Note that the getObjectSize() method does not include the memory used by other objects referenced by the object passed in.
Next -- Memory footprint of java datatypes
Also Read
Java development 2.0: Ultra-lightweight Java web services with Gretty
An actor framework for Java concurrency
Java Garbage Collection explained
Java collection performance - Chart view
The Clean Coder
Hello world with gretty
Gretty is a simple web framework for both building web servers and clients. Built on top of netty, it supports NIO style http server, asynchronous http client. It also supports both websocket server and client.
It's designed to be light weight and run as a standalone embedded solution.
It's written in Groovy++. But you can use it with pure Groovy, Scala or even Java.
Do bear with me for not being a fancy writer -
Sample code demonstrating a quick web-service implementation
Also Read
An actor framework for Java concurrency
Instrumentation - Querying the memory usage of a Java object
10 things you didn't know about java performance monitoring
The Clean Coder
Thursday, November 24, 2011
Tortoise svn for linux
A very useful tortoise svn replacement for developers using Linux but just cant live without a graphical svn client
http://www.krishnashasankar.com/2008/11/tortoise-svn-in-linux-ubuntu-alternatives-here/
http://www.krishnashasankar.com/2008/11/tortoise-svn-in-linux-ubuntu-alternatives-here/
Wednesday, October 19, 2011
Spring MVC and Spring WS in the same app - how to's
Hosting Spring MVC controller and Spring-WS service in the same web application -
This is fundamentally useful when developing an application which has its own view and additionally needs to host a bunch of services to its clients. So how do we do this ?
1. web.xml of the web app needs to have 2 set of dispatcher servlets configured
<servlet>
<servlet-name>mvcControllerServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>soapServiceServlet</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Map these servlets to different URL's
<servlet-mapping>
<servlet-name>soapServiceServlet</servlet-name>
<url-pattern>/appname/service/update</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>soapServiceServlet</servlet-name>
<url-pattern>*.wsdl</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>mvcControllerServlet</servlet-name>
<url-pattern>/app-name/mvc/*</url-pattern>
</servlet-mapping>
2. Define 2 spring context XML's mvcControllerServlet-servlet.xml and soapServiceServlet-servlet.xml
mvcControllerServlet-servlet.xml - Define all the mvc controllers in this one
<!-- URL mapping definitions -->
<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="**/info">InfoController</prop>
</props>
</property>
</bean>
<bean id="InfoController" class="com.test.web.InfoController" >
<property name="successView">
<value>mvc/success</value>
</property>
</bean>
soapServiceServlet-servlet.xml - Define messageReceiver etc in this one to facilitate spring SOAP message handling
<bean id="messageReceiver" class="org.springframework.ws.soap.server.SoapMessageDispatcher"/>
<bean id="payloadMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
<property name="endpointMap">
<map>
<entry key="{http://www.testapp.com/service}UpdateRequest" value-ref="endpoint" />
</map>
</property>
</bean>
Service implementation goes into the endpoint class and mvc implementation can be written in InfoController.
Simple and neat.
This is fundamentally useful when developing an application which has its own view and additionally needs to host a bunch of services to its clients. So how do we do this ?
1. web.xml of the web app needs to have 2 set of dispatcher servlets configured
<servlet>
<servlet-name>mvcControllerServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>soapServiceServlet</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Map these servlets to different URL's
<servlet-mapping>
<servlet-name>soapServiceServlet</servlet-name>
<url-pattern>/appname/service/update</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>soapServiceServlet</servlet-name>
<url-pattern>*.wsdl</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>mvcControllerServlet</servlet-name>
<url-pattern>/app-name/mvc/*</url-pattern>
</servlet-mapping>
2. Define 2 spring context XML's mvcControllerServlet-servlet.xml and soapServiceServlet-servlet.xml
mvcControllerServlet-servlet.xml - Define all the mvc controllers in this one
<!-- URL mapping definitions -->
<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="**/info">InfoController</prop>
</props>
</property>
</bean>
<bean id="InfoController" class="com.test.web.InfoController" >
<property name="successView">
<value>mvc/success</value>
</property>
</bean>
soapServiceServlet-servlet.xml - Define messageReceiver etc in this one to facilitate spring SOAP message handling
<bean id="messageReceiver" class="org.springframework.ws.soap.server.SoapMessageDispatcher"/>
<bean id="payloadMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
<property name="endpointMap">
<map>
<entry key="{http://www.testapp.com/service}UpdateRequest" value-ref="endpoint" />
</map>
</property>
</bean>
Service implementation goes into the endpoint class and mvc implementation can be written in InfoController.
Simple and neat.
Monday, October 17, 2011
Wednesday, October 05, 2011
How to get C like performance in Java
- The JVM does implicit bounds checking on array access and updates. This has a small overhead - you can unsafely eliminate this (and open yourself to buffer overflows and other problems) using the the Unsafe class or direct buffers.
- Use memory-minimized collections to reduce memory usage.
- You can use Direct memory to store data how you wish (this is what BigMemory uses).
- Use blocking IO in NIO (which is the default for a Channel) - don't use Selectors unless you need them.
- Most systems can handle 1K-10K threads efficiently. Scalability beyond 10K users/server doesn't buy you anything in the real world since the server resources will be consumed servicing 10k concurrent users.
- -XX:+UseCompressedStrings use byte[] instead of char[] for strings which don't need 16-bit characters - this saves memory but is 5%-10% slower.
- To reduce string space usage, you can use your own Text type which wraps a byte[], or get your text data from ByteBuffer, CharBuffer or use Unsafe or -XX:+UseCompressedStrings.
- To start the JVM faster, load fewer libraries.
- Use primitives instead of primitive wrapper objects.
Subscribe to:
Posts (Atom)
