Archive

Archive for December, 2010

Find open files in linux using lsof

December 29th, 2010 3 comments

Deleting a file that has been opened by another process in linux does not free up disk space. Running the df or du commands will indicate conflicting results. Closing / killing the process that opened the files will release the space on the disk. The lsof command can help you track, say the top ten open files in your OS sorted by disk space. If you ever run into trouble with large open files, use the following command

Top ten open files:
lsof / | awk ‘{if($7 > 1048576) print $7/1048576 “MB” ” ” $9 }’ | sort -n -u | tail

Output:

3.8054MB /usr/lib/libgtk-x11-2.0.so.0.2200.0
4.28024MB /usr/share/icons/hicolor/icon-theme.cache
8.17912MB /usr/lib/locale/locale-archive
8.86022MB /var/lib/apt/lists/lk.archive.ubuntu.com_ubuntu_dists_maverick_main_binary-i386_Packages
11.4047MB /usr/lib/flashplugin-installer/libflashplayer.so
14.6893MB /usr/lib/firefox-3.6.10/libxul.so
15.6504MB /var/cache/apt/pkgcache.bin
27.4744MB /var/lib/apt/lists/lk.archive.ubuntu.com_ubuntu_dists_maverick_universe_binary-i386_Packages
34.6615MB /usr/share/icons/gnome/icon-theme.cache
44.1719MB /home/user/.mozilla/firefox/tnrqzpro.default/urlclassifier3.sqlite

You can also lookup open files based on pid / port number. I hope the script saves you some time, should you ever find yourself in this situation.





Categories: General Tags: , ,

Jettison – A JSON streaming parser that uses Stax

December 26th, 2010 No comments

Jettison is a library that provides a streaming parser for the JSON data format. Reading that sentence again makes you want to compare it to Stax, the streaming parser for XML. Jettison provides implementation classes that inherit from the same interfaces that a caller would use for XML reading / writing. Diving into some code reveals how JSON is written using this API…

    public void go()
    {
        try
        {
            MappedNamespaceConvention con = new MappedNamespaceConvention();
            XMLStreamWriter writer = new MappedXMLStreamWriter(con, new PrintWriter(System.out));
            writer.writeStartDocument();
            writer.writeStartElement("alice");
            writer.writeCharacters("bob");
            writer.writeEndElement();
            writer.writeEndDocument();
            writer.flush();
            writer.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

Output: {“alice”:”bob”}

Notice that the XMLStreamWriter is the one in the javax.xml.stream package. The underlying data format that is written using this interface is abstracted. You could use the XMLStreamWriter to write a XML document for example. A XMLStreamReader can be used in the same way to read a XML or JSON document.

Categories: java Tags: , , ,