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?
For anyone else that runs into this. I having memory problems when creating lots of Cassandra.ISession
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With