Posts

Moving to Medium!

Ever since medium.com came, most of the people I know started to write blog posts in medium. So, I also wanted to try it out and see how it works!

My medium.com page is https://medium.com/@chrishantha/ and I already wrote one story in the last year.

I really liked the editor in Medium and I don't have to worry how my story will look when I publish it. This is the main problem I have with blogger. I have to "preview" my post to make sure it looks fine. Especially when I have code snippets. This is not really a problem of the Blogger platform. It's a problem as I use a third-party syntax highlighter.

I'm really disappointed that I didn't spend time to write more posts. My last blog post on Blogger was more than a year ago! There were many personal reasons for not writing blog posts. Anyway, now I want to start writing again and I will continue to write on Medium. I'm also planning to link my Medium stories from this blog.

Thank you for reading! :)




Benchmarking Java Locks with Counters

Image
These days I am analyzing some Java Flight Recordings from taken from WSO2 API Manager performance tests and I found out that main processing threads were in "BLOCKED" state in some situations.

The threads were mainly blocked due to "synchronized" methods in Java. Synchronizing the methods in a critical section of request processing causes bottlenecks and it has an impact on the throughput and overall latency.

Then I was thinking whether we could avoid synchronizing the whole method. The main problem with synchronized is that only one thread can run that critical section. When it comes to consumer/producer scenarios, we may need to give read access to data in some threads and write access to a thread to edit data exclusively. Java provides ReadWriteLock for these kinds of scenarios.

Java 8 provides another kind of lock named StampedLock. The StampedLock provides an alternative way to the standard ReadWriteLock and it also supports optimistic reads. I'm not goi…

Specifying a custom Event Settings file for Java Flight Recorder

Image
When you are using Java Flight Recorder (JFR), the JFR will use an event settings file to check which event types to record.

By default in JFR, there are two settings, "default" and "profile". The default setting is recommended for Continuous Recordings as it has very low overhead (typically less than 1% overhead). The profile setting has more events and useful when profiling the application.

As mentioned in my previous blog post regarding Java Flight Recorder Continuous Recordings, we use following arguments to do a Continuous Recording.

-XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:FlightRecorderOptions=defaultrecording=true,disk=true,repository=./tmp,dumponexit=true,dumponexitpath=./


Note: According to the Oracle documentation on "java" command, we should be able to specify "settings" parameter to the -XX:FlightRecorderOptions. However, the settings parameter has no effect when used with the -XX:FlightRecorderOptions and the default set…

Running Java in a Script

Recently I wanted to invoke a JMX operation in a WSO2 server from a script. I was first thinking of writing a simple Java class with a main method and run it as an executable jar file.

Then I found that we can run a script with the command "jrunscript".

I also found an example of interacting with a JMX MBean from Javascript. However this example didn't work with Java 8.

The reason is that from Java 8, the JavaScript Engine for the JVM is Oracle Nashorn. In Java 7, the JavaScript Engine is based on Mozilla Rhino.

Let's see the difference:

Following script works with Java 7, but not with Java 8


packages = new JavaImporter(java.lang, java.lang.reflect); with (packages) { // create Java String array of 5 elements var a = Array.newInstance(String, 5); // Accessing elements and length access is by usual Java syntax a[0] = "scripting is great!"; print(a.length); print('\n'); print(a[0]); print('\n'); }

Following is the…

Java Mixed-Mode Flame Graphs

Recently, I wrote a blog post on "Java CPU Flame Graphs". That blog post shows how to use Linux perf_events to generate Java Mixed-Mode Flame Graphs.

With Java Profilers, we can get information about Java process only. However with Java Mixed-Mode Flame Graphs, we can see how much CPU time is spent in Java methods, system libraries and the kernel. Mixed-mode means that the Flame Graph shows profile information from both system code paths and Java code paths.

This is sometimes very important to identify performance issues. For more information, see Java in Flames and New JVM Option Enables Generation of Mixed-Mode Flame Graphs

In this blog post, I'm using a sample high cpu consuming Java Program with Java 8 Update 60.

$ mvn -version Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T17:27:37+05:30) Maven home: /usr/local/apache-maven/apache-maven-3.3.3 Java version: 1.8.0_60, vendor: Oracle Corporation Java home: /usr/lib/jvm/jdk1.8.0_60/jre Default l…