Archive | System RSS feed for this section

Memory leak is possible in Java

I previously thought that if I pick Java for programming, I don’t need to worry about allocating and free up memory as in C++. Certainly, Java memory management does help us to minimize the chance of memory leak. However, it does not eliminate it. It is confusing right? The key is the definition of memory leak. If memory leak means an object allocated in the heap which has no direct reference, then Java does a good job to tackle this concern via object reachability (not reference counting). However, memory leak can be generalized to include the case that an object in memory that is not used but somehow being referenced and causing garbage collector not able to clean it. When will this happen in Java? A long lifecycle object holds a reference to a short lifecycle object. Below is the list of possible fixes:

  1. Explicitly remove the reference to the short life cycle object
  2. Shorten the life cycle of the anchor
  3. Weaken the reference

You can use the methods below to tell whether your system has memory leak:

  1. Examine heap memory status after several GCs. If you see the memory usage is trending up even after GCs, then you may have memory leak.
  2. OutOfMemoryError – easy indicator
  3. Performance decline – It may be the overhead of  page swapping. This will take place as memory is in demand.

Reference

Plugging memory leak with weak reference

Leave a comment Continue Reading →

Linux Commands

Grep
Pattern matching against a set of files

  • grep [options] “pattern” [filename or wildcard]
  • options:
    • -n : show line number
    • -v: negative result
    • -l: list out the matched filenames
    • -i: case-insensitive
    • -x: exact match
    • -r: recursively over the directory.
  • grep does not support + and ? and | operators. Use egrep instead.
  • Regular expression for pattern (reference visits here)
    • /^Mary/ : beginning of the line
    • /Mary$/ : end of the line
    • /[a-z]a.e/ : single character in the set of a-z and following with any single character, then character e: eg. have
    • /[^a-z]a/: any character not in the set of a-z: eg. Mary
    • /abc|abb/: alternation of patterns – like OR
    • /A+B*C?D/: + means >=1, * means >=0, ? means 0 or 1
    • /a{5} b{,6} c{4,8}/ : 5 of a, 0-6 of b, 4-8 of c
  • Use with other utilities:
    • tail -n8 a_file | grep “boo” (now grep can use to search a portion of a file)
    • find | grep “hello” (search on the dynamic result from find command)
    • find . -exec grep “boo” {} \;  (find any file recursively from the current directory with the pattern matched with “boo”. {} is to feed the grep with all the filenames obtained from the find and \; is to end the command)
    • grep “\([a-z]\)\1″ a_file

Find
The find program is a search utility, mostly found on Unix-like platforms. It searches through one or more directory tree(s) of a filesystem, locating files based on some user-specified criteria. Further, find allows the user to specify an action to be taken on each matched file. Thus, it is an extremely powerful program for applying actions to many files. It also supports regexp matching.

  • find /var/ftp/mp3 -name “*.mp3″ -type f -exec chmod 744 {} \;

The above command has several things to learn:

  • find from the directory “/var/ftp/mp3″ and its subdirectories with filename matching pattern of “*.mp3″.
  • just look at the name of regular file for pattern matching excluding directories, special files, pipes, symbolic links.
  • “-exec chmod 744 {} \;” – all the matched filenames will be feeded into an action for execution. This action can be any command like grep or chmod. The example above will change their permission to 744.

find ~jsmith -exec grep “LOG” ‘{}’ /dev/null \; -print
/home/jsmith/scripts/errpt.sh:cp $LOG $FIXEDLOGNAME
/home/jsmith/scripts/errpt.sh:cat $LOG
/home/jsmith/scripts/title:USER=$LOGNAME

The commands below achieve the same thing:

find /tmp -exec grep -H “search string” ‘{}’ \; -print

grep -R /tmp “search string”

A little shell programming - bash
Now we know how to how the command “find” and “grep” works. It is time we unleash their power in shell script.

#!/bin/bash
if [ $# = 1 ]
then
{
    ps -ax | fgrep -i $1;
}
else
{
    ps -ax;
}
fi;

The program above named “px” that checks to see whether only one parameter is passed in via “$#=1″. If yes, ps command generates a detailed listing of all processes running on the system and then the output is piped into fgrep with first parameter as the pattern to strip out irrelevant information.

 search ()
{
    if [ $# = 1 ]
    then
    {
        for i in `find . -path ‘./dev’ -prune -o -print 2> /dev/null`
        do
        {
            fgrep -i $1 $i > /dev/null 2>&1;
            if [ $? = 0 ]
            then
            {
                echo $i
            } fi;
        } done;
    }
    else
    {
        echo “Wrong number of arguments!”
    } fi;
}

This is a shell function instead of a script. Functions are basically shell scripts loaded into the shell’s memory (usually via your .profile file when you log in), and are therefore available as commands without having to put the executable file on your $PATH. More importantly, they execute in your current shell, without starting a child or subshell process.

  • find . -path ‘./dev’ -prune -o -print 2> /dev/null 
  • This command is used to generate a list of all files in the current directory and any subdirectories, with the exception of any directory called dev. We generally want to avoid looking in any dev directory because it is traditionally where device files and other special files are kept, so we “prune” that subtree from the search. Performing a search on special files can produce some interesting results, but it’s definitely not recommended. We print out any other filenames we come across, and we redirect any errors to /dev/null because we don’t really want them and they would only confuse matters.
  • We execute the find statement by placing it in backquotes, which have a special meaning in bash. In effect the expression in the backquotes evaluates to the output of the command when it’s executed, so if the find command printed out the name of three files as: “file1 file2 file3″ the for loop would effectively be: for i in “file1 file2 file3″
  • Inside the loop we call: fgrep -i $1 $i > /dev/null 2>&1;
  • we call fgrep for a case-insensitive search (the “i” option) using the pattern passed on the command line ($1) on the currently active filename ($i), and we ignore everything it prints out.
  • The normal action for fgrep is to print out the line of text that matches the search pattern, but we’re not interested in that here. All we want is the name of the file that contains the search pattern.

A bit of Linux I/O redirection (reference)

  • 1 = standard output
  • 2 = error output
  • > is overwrite to
  • >> is append to
  • 2>&1 : “redirect standard error (2) to the same place as standard outout (1)
  • cmd 2>&1 1>outfile.txt (2 points to the address of 1 that is stdout, then 1 points to outfile.txt). So, error message goes to stdout and standard output goes to outfile.txt
  • cmd 1>outfile.txt 2>&1 (1 points to outfile.txt, then 2 points to the address that 1 is pointing that is outfile.txt as well. Now both standard output and error go to outfile.txt – combine!)
  • /dev/null: discard area
Leave a comment Continue Reading →