Our Meeho!™ Blog brings you general news, tech stuff, tips, inspiration and more in relation to the Meeho!™ platform.

Sign up now for free!

Author archive

» Adjusting Apache Tomcat 6.x JVM memory footprint

Posted by Anders Østergaard Jensen on 5/5 2010 at 12:26 PM
We have previously covered how to properly adjust the JVM memory  size in the JBoss Application Server. For Apache Tomcat, the widely used Java EE web and application container, this requires a slightly different configuration. The configuration below applies to UNIX compatible systems only, but Windows should be very similar.
1. Open the bin/ directory of your Tomcat installation folder and create a new file called setenv.sh.
2. Edit the file and add the following two lines:
#!/bin/sh
export JAVA_OPTS=”-server -Xmx1024m”
The above line runs the JVM in a server configuration and sets the maximum memory usage to 1024 MB. Likewise for 512 MB it would be -Xmx512m.
3. Close your editor and make the file executable:
chmod +x setenv.sh
4. Finally, run setenv.sh every time before you run startup.sh from <tomcat-dir>/bin/. This will adjust the maximum memory footprint used by the JVM for Tomcat accordingly.
Remember that you can also add in other JVM options into this script as well.

We have previously covered how to properly adjust the JVM memory  size in the JBoss Application Server. For Apache Tomcat, the widely used Java EE web and application container, this requires a slightly different configuration. The configuration below applies to UNIX compatible systems only, but Windows should be very similar.

1. Open the bin/ directory of your Tomcat installation folder and create a new file called setenv.sh.

2. Edit the file and add the following two lines:

#!/bin/sh
export JAVA_OPTS="-server -Xmx1024m"

The above line runs the JVM in a server configuration and sets the maximum memory usage to 1024 MB. Likewise for 512 MB it would be -Xmx512m.

3. Close your editor and make the file executable:

chmod +x setenv.sh

4. Finally, run setenv.sh every time before you run startup.sh from <tomcat-dir>/bin/. This will adjust the maximum memory footprint used by the JVM for Tomcat accordingly.

Remember that you can also add in other JVM options into this script as well. Best of luck with your configuration endeavours.

» Unix: master your job control

Posted by Anders Østergaard Jensen on 4/28 2010 at 2:52 PM

I spend a lot of my time in front of my laptop in the terminal. When Terminal.app finally was equipped with tabbed terminals, it suddenly became a lot easier to execute several processes within the same window without reaching out for the mouse. But, drawing on the Unix tradition of Mac OS X, there exists a simpler and faster alternative for switching between applications with the keyboard.

Every basic Unix shell provides a concept called job control which allows for switching back and forth between your applications simultaneously from within a single shell session. This text based multitasking functionality was implemented way back before Microsoft developed the Program Manager for Windows 3.x, and it certainly demonstrates why Unix was invented inherently as a multiuser, multitasking system. Let’s take a quick tour of the basics of job control using my favourite shell zsh (bash might apply as well, but the syntax for C derivatives might be different):

1. Start a zsh session and open a text file with your favourite $EDITOR, e.g. vim:
meeho:[aj] % vim doc1.txt
2. Now vim opens up the file doc1.txt. Now, say you want to briefly look for other files containing a specific pattern without exiting vim or opening a new terminal — what is the trick? Easy as pie: press control-Z, and your current process is put to the background of your shell:
[1]  + 22023 suspended  vim doc1.txt
meeho:[aj] %
[1]  + 22023 suspended  vim doc1.txt
meeho:[aj] %
3. Now you can grep for the relevant content and finally return to your vim session:
meeho:[aj] %  grep -ir ‘important pattern’ *
meeho:[aj] %  fg
4. Now, you are back in vim. ‘fg’ simply means ‘foreground’ and pushes your recently ‘backgrounded’ process of yours into the foreground of the shell session. Now, what if you want to background more than one process in the same shell session? Easy, control-Z out of the shell, open a new vim instance (vim doc2.txt) and background that process with control-Z as well. Now you can list the available jobs with the zsh built-in ‘jobs’ command:
meeho:[aj] % jobs
[1]  - suspended  vim doc1.txt
[2]  + suspended  vim doc2.txt
5. Finally, you can now swap back and forth between the applications using %<ID> (ID being the number in the squared brackets, e.g. %1 for ‘vim doc1.txt’ and %2 for ‘vim doc2.txt’):
meeho:[aj] % %1
The syntax for job control varies a bit from shell to shell, but the concept is basically the same. It is particularly useful if you are working on a remote machine through a slow network SSH connection, or if you simply have no graphical environment (e.g. X11) available.
There are, however, alternatives to the simple, but elegant Unix job control: GNU Screen is one of them. In terms of multitasking it is much more powerful, since it allows you to shuffle back and forth between several terminal sessions (thus, Screen is described as a terminal multiplexer rather than a single shell process). Some people even describe it as a window manager for the console, and it is truly powerful. But for now, I will save that walkthrough for next time.

1. Start a zsh session and open a text file with your favourite $EDITOR, e.g. vim (or the standard editor, ed):

meeho:[aj] % vim doc1.txt

2. Now vim opens up the file doc1.txt. Now, say you want to briefly look for other files containing a specific pattern without exiting vim or opening a new terminal — what is the trick? Easy as pie: press control-Z, and your current process is suspended to the background of your shell (process suspension also implies that the process execution is paused):

[1]  + 22023 suspended  vim doc1.txt
meeho:[aj] %

3. Now you can grep for the relevant content and finally return to your vim session:

meeho:[aj] %  grep -ir 'important pattern' *
meeho:[aj] %  fg

4. Now, you are back in vim. ‘fg’ simply means ‘foreground’ and pushes your recently ‘backgrounded’ process of yours into the foreground of the shell session. Now, what if you want to background more than one process in the same shell session? Easy, control-Z out of the shell, open a new vim instance (vim doc2.txt) and background that process with control-Z as well. Now you can list the available jobs with the zsh built-in ‘jobs’ command:

meeho:[aj] % jobs
[1]  - suspended  vim doc1.txt
[2]  + suspended  vim doc2.txt

5. Finally, you can now swap back and forth between the applications using %<ID> (ID being the number in the squared brackets, e.g. %1 for ‘vim doc1.txt’ and %2 for ‘vim doc2.txt’):

meeho:[aj] % %1

The syntax for job control varies a bit from shell to shell, but the concept is basically the same. It is particularly useful if you are working on a remote machine through a slow network SSH connection, or if you simply have no graphical environment (e.g. X11) available.

There are, however, alternatives to the simple, but elegant Unix job control: GNU Screen is one of them. In terms of multitasking it is much more powerful, since it allows you to shuffle back and forth between several terminal sessions (thus, Screen is described as a terminal multiplexer rather than a single shell process). Some people even describe it as a window manager for the console, and it is truly powerful. But for now, I will save that walkthrough for next time.

» Time Zone Conversion with java.util.Date

Posted by Anders Østergaard Jensen on 3/9 2010 at 3:54 AM

When building global web sites it is often necessary to display timestamps within the user’s own time zone. Ruby on Rails contains built-in functionality for this, whereas Java requires a more manual approach. In the following I will describe how such time zone conversion (and mapping) is easily implemented in Java.

The following method creates a time zone conversion from a standard java.util.Date (which defaults to the machine’s local time zone) to a specific time zone:

public static String formatDanishDate(Date d, String tz) {
DateFormat fmt = new SimpleDateFormat(”d/MM/yyyy ‘kl.’ HH:mm Z”);
TimeZone zone = getTimeZone(tz);
fmt.setTimeZone(zone);
return fmt.format(d);
}
public static String formatUSDate(Date d, String tz) {
DateFormat fmt = new SimpleDateFormat(”EEE, d/MM/yyyy ‘at’ KK:mm a Z”);
TimeZone zone = getTimeZone(tz);
logger.error(”+++ Chosen tz was: ” + zone.getDisplayName());
fmt.setTimeZone(zone);
logger.error(”LOL FORMAT: ” + fmt.format(d));
return fmt.format(d);
}
public static String formatUSDate(Date d, String tz) {

    DateFormat fmt = new SimpleDateFormat("EEE, d/MM/yyyy 'at' KK:mm a Z");

    TimeZone zone = getTimeZone(tz);

    fmt.setTimeZone(zone);

    return fmt.format(d);

  }

The above method depends on the following support function that searches through the Java library of supported time zones (with lower case matching).  If no match is found, getDefault() is returned. According to the SDK Javadoc, the default time zone depends on your system locale (if you are in Copenhagen, the standard time zone will be Europe/Copenhagen).

  public static TimeZone getTimeZone(String tz) {

    TimeZone retVal;

    for (String id: TimeZone.getAvailableIDs()) {

      if (id.toLowerCase().contains(tz.toLowerCase())) {

        retVal = TimeZone.getTimeZone(id);

        return retVal;

      }

    }

    return TimeZone.getDefault();

  }

Using the lowercase match search is especially useful if you are using time zones across different applications or frameworks with the same database. Fx. Ruby on Rails only stores the selected session time zone with the last identifier (e.g. ‘Copenhagen’ rather than ‘Europe/Copenhagen’), which makes it impossible to implement a one-to-one mapping between the two environments. Using getTimeZone(”Sydney”) or getTimeZone(”Copenhagen”) will thus return the Java time zone “Australia/Sydney” and ”Europe/Copenhagen” respectively.

Finally, the SimpleDateFormat lets you format the now converted Date representation to a localized format. Thus, the user can now also choose from different date formats in your application. This Javadoc link clarifies the different formatting options. For instance, a proper date representation for the Danish calendar would be:

DateFormat fmt = new SimpleDateFormat("d/MM/yyyy 'kl.' HH:mm Z");

Please let us know if you have further efficient methods for time zone conversion in Java and across different platforms.

» Adjusting Development Memory Usage for JBoss 5.x

Posted by Anders Østergaard Jensen on 1/20 2010 at 7:07 AM

JBoss is an extremely popular Java EE application server. But, as excellent it may be, it definitely sucks up a lot of memory – also when in development mode. If you have plenty of memory (more than 2 gigabytes, I would say), you can speed up the waiting time for performing so-called hot redeploys. Hot redeployment happens, for instance, when you redeploy a packaged application EAR file from within Netbeans, which is responsible for propagating the EAR chunk to the correct directory. Reloading EAR files from scratch, especially if you have a lot of JPA/Hibernate related configuration directives, can be a lengthy process and increasing the memory available to JBoss can speed up this process significantly.

What we will do is simply to increase the JVM (Java Virtual Machine) memory on JBoss. This is done in the file <jboss-dir>/bin/run.conf. Find the line containing the following:

#
# Specify options to pass to the Java VM.
#

if [ "x$JAVA_OPTS" = "x" ]; then
   JAVA_OPTS="-Xms256m -Xmx1512m -XX:MaxPermSize=256m -Dorg.jboss.resolver.warning=true -Dsun.rmi.dgc.cli
ent.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000"
fi

The two parameters, -Xms256m and -Xmx1512m specify the minimum (lower) and maximum (upper) JVM memory consumption limits. Normally, the maximum (-XmxXXXm) is set to 512, but in case you have enough memory, you can easily increase this to e.g. 1024 MB (-Xmx1024m) or 2048 MB (-Xmx2048m). That way, you can easily cut off a lot of the time unwillingly spent on waiting for your EAR files to redeploy during development.

#
# Specify options to pass to the Java VM.
#
if [ "x$JAVA_OPTS" = "x" ]; then
JAVA_OPTS=”-Xms256m -Xmx1512m -XX:MaxPermSize=256m -Dorg.jboss.resolver.warning=true -Dsun.rmi.dgc.cli
ent.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000″
fi