Friday, January 02, 2009

fahd.blog in 2008

Happy 2009!
I'd like to wish everyone a great start to an even greater new year!

During 2008, I posted 44 new entries on fahd.blog, which is 52% more than in 2007. In addition to more posts, I am thrilled that I have more readers from all over the world too! Thanks for reading!

Top 5 posts of 2008:

I'm going to be writing a lot more this year, so stay tuned for more great techie tips, tricks and hacks! :)

Tuesday, December 23, 2008

OutOfMemoryError: PermGen space

Recently, I found a problem with our system in which it ran out of PermGen space under heavy usage. The exception from the logs is:
java.lang.OutOfMemoryError: PermGen space
        at java.lang.String.intern(Native Method)

PermGen Space
The memory a jvm uses is split up into three "generations": young (eden), tenured, and permanent. PermGen refers to the permanent generation which holds meta-data describing user classes and "interned" strings. If you have a large code base and intern lots of strings, more permgen space is used.

Interning Strings
You can intern strings using String.intern() which, from the javadocs:

Returns a canonical representation for the string object.
A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
Interned strings are stored in permgen space, so if you interned all your strings, you would eventually run out of memory. In our application, I found that we were interning string representations of timestamps which wasn't necessary! You should only intern strings which are used frequently.

Increasing PermGen Space
The maximum size is typically 64MB. You can double it by starting your JVM with the following system properties:
-XX:MaxPermSize=128m -XX:PermSize=128m

Using JConsole to Monitor Memory
An easy way to see how much memory your JVM is using is to use jconsole. First, you need to enable monitoring on your JVM by starting it up with the following system properties:
-Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=<port>
Next start jconsole and point it at this JVM using the host and port (or pid). Select the Memory tab and then choose the Memory Pool "PS Perm Gen" chart. You should now see the usage of your PermGen space as shown in the screenshot below:

Saturday, December 13, 2008

XP Uptime Record

My computer has been up for 151 days!

This is the XP machine I use every day at work and even log on from home, after work and on weekends. I'm surprised to see that it hasn't needed a reboot for so long.

Sadly, there's a powerdown in the building tonight...

Finding Uptime on XP
Use the systeminfo command and the look for "System Up Time"

C:\> systeminfo | find "System Up"
  System Up Time: 151 Days, 9 Hours, 45 Minutes, 8 Seconds
Finding Uptime on Solaris or Linux
Use the uptime command:
sharfah@starship:~> uptime
  5:46pm  up 2 day(s), 7:30, 1 user, load average: 3.79, 5.55, 5.41
You can also find out when your system booted up, by using who -b:
sharfah@starship:~> who -b
 system boot Dec 10 11:16

Saturday, November 22, 2008

UNIX Fork Bomb :(){ :|:& };:

Whatever you do, please do NOT enter this code in your Unix terminal:
:(){ :|:& };:
This is one of the most elegant examples of a fork bomb, which works by creating a large number of processes very quickly in order to saturate the operating system's process table. Each process uses up CPU time and memory and so the system becomes unresponsive and is quickly brought to its knees! This is a form of denial-of-service attack.

So how does this command work? It might be easier to understand if I re-wrote it like this instead:

bomb()
{
   bomb | bomb &
}
bomb
You declare a function called bomb which calls itself recursively and pipes the output to another call of itself. The & puts the function call in the background so that the new child processes can never die. The semi-colon marks the end of the function and the final "bomb" launches the attack (by calling the function the first time).

You can only stop a fork bomb by destroying all instances of it. It is a difficult task to use another program to kill it because that would mean creating another process which the system may not have enough space for. The only guaranteed way of curing a fork bomb is to reboot.

Windows Fork bomb Example

:s
start %0
%0|%0
goto :s

Eclipse Code Templates

This post is out-of-date! For the latest version, go here: "Useful Eclipse Templates for Faster Coding".

In this post, I will share some of my Eclipse Java Code Templates.

Templates are simply "magic words" or shortcuts to standard blocks of code or text. They are very handy because once you have them setup you don't have to waste time writing boilerplate code any more! An example of a pre-defined template in Eclipse is sysout which expands to System.out.println();. All you have to do is type sysout followed by Ctrl+Space to insert the statement in your java source file.

To see what templates are defined in Eclipse:

  • Open your Preferences dialog by going to Windows > Preferences
  • On the navigation tree on the left, go to Java > Editor > Templates
  • You will see a list of pre-defined templates
  • You can add new ones by pressing the "New..." button
Here is a list of templates I have created:

1. logger

Name: logger
Context: Java statements
Description: create new Logger
Pattern:

${:import(org.apache.log4j.Logger)}
private static final Logger logger =
       Logger.getLogger(${enclosing_type}.class);
2. logd

Name: logd
Context: Java statements
Description: logger debug
Pattern:

if(logger.isDebugEnabled())
     logger.debug(${word_selection}${});${cursor}
3. logi

Name: logi
Context: Java statements
Description: logger info
Pattern:

logger.info(${word_selection}${});${cursor}
4. logerr

Name: logerr
Context: Java statements
Description: logger error
Pattern:

logger.error(${errorMessage}, ${e});
5. readfile

Name: readfile
Context: Java
Description: read text from file
Pattern:

${:import(java.io.BufferedReader,
          java.io.FileNotFoundException,
          java.io.FileReader,
          java.io.IOException)}
BufferedReader in = null;
try {
   in = new BufferedReader(new FileReader(${fileName}));
   String line;
   while ((line = in.readLine()) != null) {
      ${process}
   }
}
catch (FileNotFoundException e) {
   logger.error(e) ;
}
catch (IOException e) {
   logger.error(e) ;
} finally {
   if(in != null) in.close();
}
${cursor}
6. for (iterate over map)

Name: for
Context: Java statements
Description: iterate over map
Pattern:

for(Map.Entry<${key:argType(map,0)},${value:argType(map,1)}> entry :
                    ${map:var(java.util.Map)}.entrySet()) {
    ${key} key = entry.getKey();
    ${value} value = entry.getValue();
    ${cursor}
}
Do YOU have any useful templates? If so, share them in the comments!