- Undocumented Matlab - https://undocumentedmatlab.com -

Inter-Matlab data transfer with memcached

Posted By Yair Altman On August 27, 2014 | 2 Comments

Once again I welcome guest blogger Mark Mikofski [1]. Mark has written here last year about JGIT-Matlab integration [2] and earlier this year on JSON-Matlab integration [3]. Today, Mark shows how to use the well-known open-source memcached [4] library for data transfer between separate Matlab processes. Readers are also referred to Mark’s article [5] on his blog. I have already written about various ways to communicate between separate Matlab processes [6] – today’s article is an important complement to these methods.


Stringpool's memcached tutorial [7]The memcached [4] open-source library is great for sending objects from process to process. It is typically used to cache large data objects from databases or in a distributed environment, but we can also use it as a simple distributed shared memory system, which stores data in key-value pairs (as in a hashtable).
I was able to use memcached to set and retrieve objects from two different instances of Matlab. Both the Java and .NET variants of memcached can be used for this. memcached requires two components: server and client: The server comes precompiled, while the client needs to be compiled by us.
The memcached server was compiled by Trond Norbye from Couchbase (previously Membase and Northscale) for 32-bit [8] and 64-bit [9] Windows OS. See below [10] how to run it as a service using the Python PyWin32 package.
I compiled Windows binaries of memcached-client libraries for both the Java and .NET using Java-1.7 and Visual Studio 2010 and zipped them up here [11].
Note: additional (non-memcached) shared-memory implementations used in Matlab include Joshua Dillon’s sharedmatrix [12] (also see here [13]), and Kevin Stone’s SharedMemory [14] utilities, which use POSIX shared-memory and the Boost IPC library [15]. Rice University’s TreadMarks [16] library is another example of a shared-memory approach that has been used with Matlab, in the MATmarks [17] package (note that the current availability of MATmarks is unclear).


We use the Memcached server version 1.4.5 patched for 64-bit Windows [9], precompiled by Trond Norbye from Couchbase (previously Membase and Northscale).
For the Memcached .NET client, download the Memcached.ClientLibrary port of com.meetup.memcached [18] from SourceForge, and then compile using Microsoft Visual Studio 2010 C#, Release/AnyCPU configuration. Since the original project files were for MSVC-8, MSVC-10 starts a conversion wizard that first backed up the previous project files and then creates a new MSVC-10 project, including a new solution file. I then build the clientlib_2.0 project from the MSVC-10 IDE GUI using “build” from the menu, and voila: I got a new DLL.
Usage in Matlab is as follows:

  1. Start the memcached server:
    C:\> C:\path\to\memcached.exe -vv
  2. Do this on both MATLAB instances:
    asm = NET.addAssembly('C:\full\path\to\Memcached.ClientLibrary.dll');
    pool = Memcached.ClientLibrary.SockIOPool.GetInstance();
    mc = Memcached.ClientLibrary.MemcachedClient();
  3. On MATLAB instance #1:
  4. On MATLAB instance #2:
    >> myName = mc.Get('name')
    myName =


MATLAB and memcached also works perfectly with the original meetup.com Java version that the .NET version was ported from. The commands are exactly the same, but the Apache logger is not cleaned up for the non-bench/test sources (with isErrorEnabled(), etc. see jlog4 documentaion [19]), so you always get this error message:

log4j:WARN No appenders could be found for logger (com.meetup.memcached.SockIOPool).
log4j:WARN Please initialize the log4j system properly.

Because there are log calls in the MemcachedClient and SockIOPool files that never have BasicConfiguration.configure() or set any jlog4. like the appender hence the error. Of course they never meant for log messages to be displayed when deployed, so wrapping with isErrorEnabled() like the .NET version does would be better.
The Java package is called com.meetup.memcached. We should build it ourselves from the Github source [20]. The jar from the Maven repository [21] didn’t work (attempting to start the sock I/O pool raised several Java errors due to missing includes), and differs a bit from the GitHub repo.

$ /c/Program\ Files/Java/jdk1.7.0_55/bin/javac.exe -verbose -classpath ../lib/log4j.jar -sourcepath com/meetup/memcached/ -source 1.6 -target 1.6 -d ../bin/ com/meetup/memcached/*.java
$ /c/Program\ Files/Java/jdk1.7.0_55/bin/jar.exe -cvf MeetupMemcached.jar com/

To use in Matlab, first, start the memcached server, then start sock I/O pools and memcached clients on both Matlab instances:

>> javaaddpath('C:\full\path\to\MeetupMemcached.jar');
>> pool = com.meetup.memcached.SockIOPool.getInstance();
>> pool.setServers({''});
>> pool.initialize;
>> mc = com.meetup.memcached.MemcachedClient;
>> mc.set('name','mark'); % on 1st instance/process
>> myName = mc.get('name') % on 2nd instance/process
myName =

Other clients

  • xmemcached [22] – Github:killme2008, Google Code project and Maven repo
  • The Xmemcached client works well. It is the most recently updated. There are jar files on the releases page of the GitHub repo. Here is an example from the 2.0.0 release from this April. The example is from the google.code wiki User Guide in English.

    >> javaaddpath('C:\full\path\to\xmemcached-2.0.0.jar');
    >> addr = net.rubyeye.xmemcached.utils.AddrUtil.getAddresses('localhost:11211')
    addr =
    >> builder = net.rubyeye.xmemcached.XMemcachedClientBuilder(addr)
    builder =
    >> mc = builder.build()
    log4j:WARN No appenders could be found for logger (net.rubyeye.xmemcached.XMemcachedClient).
    log4j:WARN Please initialize the log4j system properly.
    mc =
    >> mc.set('name',0,'mark')
    ans =
    >> myName = mc.get('name')
    myName =
  • Enyim [23] – NuGet and Github:enyim
    I couldn’t get this assembly to load as built, I always got an “Strong Name Validation Failed” error, perhaps because I am on a 64-bit machine, but using MSVC express.
  • spymemcached [24] – Github:dustin, Google Code project and Maven repo
    I also couldn’t get this to work. Even though I could make an array of Java socket objects using InetSocket the connection was always refused even though the memcached server was operating on the specified port.

memcached server as Windows service

This Gist [25] uses Python PyWin32 win32service [26] to install, remove, configure, start and stop the memcached server.

Categories: Guest bloggers, Low risk of breaking in future versions

2 Comments (Open | Close)

2 Comments To "Inter-Matlab data transfer with memcached"

#1 Comment By Mark Mikofski On August 27, 2014 @ 15:31

In order to get rid of that message and output logging messages to a file configure the Apache.org log4j logger yourself by creating a [33] with the default layout and then configuring the [34].

>> fileAppender_withDefaultLayout = org.apache.log4j.FileAppender(org.apache.log4j.PatternLayout,'memcached.log')
fileAppender_withDefaultLayout =
>> org.apache.log4j.BasicConfigurator.configure(fileAppender_withDefaultLayout)

To get rid of the logger message for xmemcached you will have to download [35] and add slf4j-api-X.Y.Z.jar, slf4j-simple-X.Y.Z.jar and slf4j-log4j-X.Y.Z.jar to your path first, where X.Y.Z is the SLF4J version, 1.7.7 as of 2014-08-27. Then configure log4j as above for com.meetup.memcached.

#2 Comment By Mark Mikofski On August 27, 2014 @ 16:23

Turns out that the newer schooner/whalin client is even faster than the old whalin (meetup.com) client, and they say it’s faster than both spymemcached (from couchbase/membase) and xmemcached.

The missing libraries to make this run are in the lib folder of the [36] which are the release page of Greg Whalin’s Github Memcached-Java-Client repo.

I was able to build this using jdk-7 by extracting the file, making a new bin folder at the same folder as lib and src, then navigating to src\main\java and executing the following:

C:\> "C:\Program Files\Java\jdk1.7.0_55\bin\javac.exe" -verbose -cp ..\..\..\lib\commons-pool-1.5.6.jar;..\..\..\lib\slf4j-api-1.6.1.jar;..\..\..\lib\slf4j-simple-1.6.1.jar -sourcepath com\schooner\MemCached\ -source 1.6 -target 1.6 -d ..\..\..\bin com\whalin\MemCached\*.java com\schooner\MemCached\*.java com\schooner\MemCached\command\*.java

Then back up to the new bin folder an executing this:

c:\> "C:\Program Files\java\jdk1.7.0_55\bin\jar.exe" -cvf Memcached-Java-Client.jar com\

It works almost exactly like the old meetup.com client, but the package is named “com.whalin.MemCached” instead. Also now you will also have to add “commons-pool-1.5.6.jar”, “slf4j-api-1.6.1.jar” and “slf4j-simple-1.6.1.jar” to your MATLAB Java classpath using javaaddpath().

Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/inter-matlab-data-transfer-with-memcached

URLs in this post:

[1] Mark Mikofski: http://poquitopicante.blogspot.com

[2] JGIT-Matlab integration: http://undocumentedmatlab.com/blog/jgit-matlab-integration/

[3] JSON-Matlab integration: http://undocumentedmatlab.com/blog/json-matlab-integration

[4] memcached: http://memcached.org

[5] Mark’s article: http://poquitopicante.blogspot.co.il/2014/08/intro-memcached-memcached.html

[6] various ways to communicate between separate Matlab processes: http://undocumentedmatlab.com/blog/explicit-multi-threading-in-matlab-part4#spawning

[7] Image: http://stringpool.com/memcached-tutorials

[8] 32-bit: http://downloads.northscale.com/memcached-1.4.5-x86.zip

[9] 64-bit: http://downloads.northscale.com/memcached-1.4.5-amd64.zip

[10] below: #windows_service

[11] here: https://dl.dropboxusercontent.com/u/19049582/memcached.zip

[12] sharedmatrix: http://www.mathworks.com/matlabcentral/fileexchange/28572-sharedmatrix

[13] here: http://smlv.cc.gatech.edu/2010/08/27/shared-memory-in-matlab/

[14] SharedMemory: http://bengal.missouri.edu/~kes25c/SharedMemory-Windows.zip

[15] Boost IPC library: http://boost.org/doc/libs/1_55_0/doc/html/interprocess/managed_memory_segments.html

[16] TreadMarks: http://infoscience.epfl.ch/record/55784/files/keleher94treadmark.pdf

[17] MATmarks: http://polaris.cs.uiuc.edu/matmarks/

[18] Memcached.ClientLibrary port of com.meetup.memcached: http://sourceforge.net/projects/memcacheddotnet/

[19] jlog4 documentaion: https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Category.html

[20] Github source: https://github.com/gwhalin/Memcached-Java-Client

[21] Maven repository: http://mvnrepository.com/artifact/com.whalin/Memcached-Java-Client

[22] xmemcached: https://code.google.com/p/xmemcached/

[23] Enyim: https://www.nuget.org/packages/EnyimMemcached/

[24] spymemcached: https://code.google.com/p/spymemcached/

[25] Gist: https://gist.github.com/mikofski/e1e1b953cdf42f6170d4#file-memcached_service-py

[26] win32service: http://starship.python.net/~skippy/win32/Downloads.html

[27] Serializing/deserializing Matlab data : https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data

[28] Simulink Data Dictionary : https://undocumentedmatlab.com/articles/simulink-data-dictionary

[29] Accessing plot brushed data : https://undocumentedmatlab.com/articles/accessing-plot-brushed-data

[30] Sparse data math info : https://undocumentedmatlab.com/articles/sparse-data-math-info

[31] Additional license data : https://undocumentedmatlab.com/articles/additional-license-data

[32] Controlling plot data-tips : https://undocumentedmatlab.com/articles/controlling-plot-data-tips

[33] : http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/FileAppender.html

[34] : http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/BasicConfigurator.html

[35] : http://www.slf4j.org

[36] : https://github.com/gwhalin/Memcached-Java-Client/releases/tag/Memcached-Java-Client-3.0.0

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.