Collections.sublist() not serializable

Ran into a strange serialization issue when trying to add a Java class to memcached…

Caused by: java.util.RandomAccessSubList

The problem was with a class member that was initialized with Collections.sublist() method. This method returns an instance of java.util.RandomAccessSubList which is not serializable.

The solution was to wrap initialize the list as an ArrayList.

new ArrayList<String>(biglist.subList(2, 4))

Installing memcached on Mac OS X

Easiest way to install memcached on Mac OS X is using MacPorts (port)

According to the MacPorts website…

The MacPorts Project is an open-source community initiative to design an easy-to-use system for compiling, installing, and upgrading either command-line, X11 or Aqua based open-source software on the Mac OS X operating system. To that end we provide the command-line driven MacPorts software package under a BSD License, and through it easy access to thousands of ports that greatly simplify the task of compiling and installing open-source software on your Mac.

The easiest way to install MacPorts on a Mac OS X system is by downloading the dmg for Lion, Snow Leopard or Leopard and running the system’s Installer by double-clicking on the pkg contained therein, following the on-screen instructions until completion.

Now, to install memcached, type the following command

sudo port install memcached

Memcached Server can be started by running the following command

memcached -vv

To run memcached as a daemon

memcached -d

These instructions have been tested on Mac OS X 10.6.8 (Snow Leopard)

Using Memcached with Java on Windows

Windows Memcached software is available on this page:

Download the Win32 binary zip file. Unzip the file (memcached.exe) in a directory of choice and then do the following to install memcached as a service.

c:/bin/memcached.exe  -d install

At this point the service is installed, it can started or stopped using the following commands

c:/bin/memcached.exe -d  start
c:/bin/memcached.exe -d  stop

By default, the memcached service has a limit of 64MB. If that is not enough, you can increase by tweaking the Windows Registry. Open the following entry.

HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/memcached Server

Locate the entry called ImagePath. To increase the limit to 128MB set the value to…

“c:/bin/memcached.exe” -d runservice -m 512

A good Java client for memcached is spymemcached. Download the jar file from here:

// Get a memcached client connected to the local server
MemcachedClient c = new MemcachedClient(AddrUtil.getAddresses(""));
// Store a value (async) for one hour
c.set("someKey", 3600, someObject);
// Retrieve a value (synchronously).
Object myObject= c.get("someKey");