Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cassandra c# driver memory leak

Using cassandra .net driver we are facing the following issue: When inserting a lot of rows with parameterized INSERTs, the application memory usage continuously grows:

class Program
{
    static Cluster cluster = Cluster.Builder()
        .AddContactPoints(ConfigurationManager.AppSettings["address"])
        .Build();
    static Session session = cluster
        .Connect(ConfigurationManager.AppSettings["keyspace"]);
    static int counter = 0;

    static void Main(string[] args)
    {
        for (int i = 0; i < 50; i++)
        {
            new Thread(() =>
            {
                while (true)
                {
                    new Person()
                    {
                        Name = Interlocked.Increment(ref counter).ToString(),
                        ID = Guid.NewGuid(),
                        Data = new byte[4096],
                    }.Save(session);
                }
            }).Start();
        }

        Console.ReadLine();
    }
}

class Person
{
    public Guid ID
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }

    public byte[] Data
    {
        get;
        set;
    }

    public void Save(Session session)
    {
        Stopwatch w = Stopwatch.StartNew();

        session.Execute(session.Prepare(
            "INSERT INTO Person(id, name, data) VALUES(?, ?, ?);")
            .Bind(this.ID, this.Name, this.Data));

        Console.WriteLine("{1} saved in {0} ms",
            w.Elapsed.TotalMilliseconds, this.Name);
    }
}

According to the created memory dump the managed heap contains a huge amount of small byte arrays (most of them being generation 2), which could be traced back to the cassandra driver's byte conversion methods (InvConvert*) in the internal TypeInterpreter class.

Do you have any advice or ideas how we could get rid of this issue?

like image 284
user2743662 Avatar asked Sep 03 '13 16:09

user2743662


1 Answers

For anyone else that runs into this. I having memory problems when creating lots of Cassandra.ISessions even though I was disposing it correctly via a using statement. Changing my code to reuse a single ISession seems to have fixed it. I don't know if this is the best solution.

like image 179
Nogwater Avatar answered Oct 05 '22 00:10

Nogwater