Some tips on java.lang.OutOfMemoryError: unable to create new native thread

Thats right, the dreaded, java.lang.OutOfMemoryError: unable to create new native thread. I fixed one of these the other day. And I will present to you how I did it. The cause for such errors are usually Thread Leaks. A Thread Leak is where some ass hat is creating threads which never die. Another reason for such errors is the limits an Operating System can place on the number of threads per process. If hit this limit, you would ge the same error.
Read the rest of this entry »

Debugging vs Writing shit

Great quote I saw today:

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
-- Brian W. Kernighan

Ma homie Kernighan! Nigga wrote the book on C. Incidentally not many people know this about ma homie, he used to be known on the streetz as… ah shit, I promised him I would keep his days back in da hood a secret.
Read the rest of this entry »

Mother F**k the ScheduledExecutorService!

Thats right! Motherfuck this service.

Deep hidden in the javadoc of this magnificent class is this gem “If any execution of the task encounters an exception, subsequent executions are suppressed.”. In other words, if your runnable task has any fuckups, your task will no longer be run. What sucks about this, is there is no clear indication that there was a fuckup in the task. No warning, just silenty the task gets canceled leading you to say: “Dude! where my task!?”
Read the rest of this entry »

JDK Thread Pool Configuration

Introduction

In any non-trivial application of somewhat significant size you will need Executors to background processing or asynchronous processing, task splitting etc. This is where the JDK executors framework comes in.

Read the rest of this entry »

Identifying which Java Thread is consuming most CPU

I didn’t come up with this. I was shown how to do this by an esteemed college at work.

Introduction

Most (if not all) productive systems doing anything important will use more than 1 java thread. And when something goes crazy and your cpu usage is on 100%, it is hard to identify which thread(s) is/are causing this. Or so I thought. Until someone smarter than me showed me how it can be done. And here I will show you how to do it and you too can amaze your family and friends with your geek skillz.

Read the rest of this entry »

An Improved RMI Tutorial with Eclipse

Introduction

There are/have-been heaps of remoting frameworks in java, but RMI being part of the JRE/JDK and therefore having no external dependencies is my personal preference for remoting with java. Its main drawback: the wire protocol is not web-friendly and therefore difficult to go through firewalls (although it is possible). But if used behind the firewall, it makes for an excellent way to do distributed computing using only the JDK (OMG! only the JDK?! no Spring? or JMS?!). It does have several killer features that are found in very few (if any) remoting frameworks: callbacks and remote classloading. In this tutorial, you will see remote classloading. Ever since JRE 5.0, you don’t need to compile stubs (meaning you don’t need an extra compile-time step to get RMI working). I am guessing someone decided to do away with those and use the jdk dynamic proxies.

Read the rest of this entry »

Simulating dropped packets (aka crappy internets) with iptables

Disclaimer #1

I am not an iptables expert. This tip will work best on your local linux development box where you probably have no iptables rules setup. Do not play around with this shit on production machines, unless you know what you are doing. Speak with your friendly sysadmins before doing this on any machine for which they feel responsible.

Disclaimer #2

According to this tip, do not drop more than 14% of the packets otherwise you will cause all tcp sockets to stall.

Introduction

Why would you want to do this? sometimes people (aka customers) might complain that your super-duper application that they use via the Internets (aka a series of tubes) is slow. One reason could be a dodgy internet connection and/or packet loss. See this great article about how packet-loss affects web applications and how to drop packets with a microwave oven.

And now finally…

To simulate a dropped packets with iptables, you can use the following commands (as root):

# for randomly dropping 10% of incoming packets:
iptables -A INPUT -m statistic --mode random --probability 0.1 -j DROP

# and for dropping 10% of outgoing packets:
iptables -A OUTPUT -m statistic --mode random --probability 0.1 -j DROP

Once done, you can use the following for removing these packet-drops:

# for the incoming packets:
iptables -D INPUT -m statistic --mode random --probability 0.1 -j DROP

# and for the outgoing packets
iptables -D OUTPUT -m statistic --mode random --probability 0.1 -j DROP

Auto Install the SUN JDK on Linux

Sometimes (yes it does happen), you need to install the JDK from SUN automatically (without having to answer yes/no to the license). Why? For example in a Kick Start script to auto-install the JDK.

And so to do this, here a bash & perl script for this exact purpose. This script works only with the .bin install package that you download from SUN’s website.

#!/bin/bash

jdkBinFile=$1

echo $1

# just spew out the agreement
perl -p -i -e 's/^more/cat/g' $1

# set the 'agreed' value for jdk 1.5
perl -p -i -e 's/\s+agreed=$/agreed=1/g' $1

# set the agree value for jdk 1.6
perl -p -i -e 's/`agree`/yes/g' $1

# do not call the register_jdk method
perl -p -i -e 's/\s+register_JDK/#register_JDK/g' $1

# and now run the installation
bash $1

How to

Create a new script file called autoinstall.sh containing the above script. and run it as follows:

$ cd /install/to/directory
$ autoinstall.sh /my/downloads/jdk-6u16-linux-x64.bin

Ruby FIX Message Viewer 1.0

Today I released my Ruby FIX Message Viewer 1.0. This is a command line based FIX message viewer programmed in Ruby. I made it command line because most, if not all, of the FIX logs that I have at work are on a server when I have only ssh access. Rather than copy them over and use some GUI tools, I find it quicker just to use this tool on the server.

scp2here: scp command to here

I usually need to scp stuff from/over-to servers on a regular basis. And here is a function in .bashrc that I use:

#function to get the scp path to here
function scp2here
{
    user=`whoami`
    server=`hostname`
    pathToHere=`pwd`

     output=$user"@"$server":"$pathToHere

    if [ ! -z "$1" ]
    then
        output=$output"/"$1
    fi

    echo $output
}

This function prints out the scp command argument to copy a file/directory to a location.

Examples

Then to copy something to a target server where I have scp2here, I can cd to the target directory and do this:

srasul@some.cool.host ~ $ scp2here
srasul@some.cool.host.com:/home/srasul
...
srasul@another.cooler.host ~ $ scp bigassfile.txt srasul@some.cool.host.com:/home/srasul

Or if I want to copy a file from a location that has scp2here:

srasul@some.cool.host ~ $ scp2here somefile.txt
srasul@some.cool.host.com:/home/srasul/somefile.txt
...
srasul@another.cooler.host ~ $ scp srasul@some.cool.host.com:/home/srasul/somefile.txt .