I downloaded all the messages and checked their attachment . They are filling RAM. I am called that code in threads. I am try to use client.Dispose() and GC.Collect() but is is not helped :((
using (var client = new ImapClient(hostname, true))
{
if (client.Connect( /* optional, use parameters here */ ))
{
// connection successful
if (client.Login(login, pass))
{
// login successful
FolderCollection folders = client.Folders;
int i = 0;
foreach (Folder myfolder in folders)
{
var messages = client.Folders[i].Search("ALL");
i++;
foreach (var message in messages)
{
var attachments = message.Attachments;
if (attachments.Count() > 0)
if (!Directory.Exists(folder + @"\" + login))
{
DirectoryInfo di = Directory.CreateDirectory(folder + @"\" + login);// Try to create the directory.
}
foreach (var attachment in attachments)
{
attachment.Download();
attachment.Save(folder + @"\" + login);
}
}
GC.Collect();
}
}
}
client.Disconnect();
client.Dispose();
}
You should never call
GC.Collect()
until you defenitly know what are you doing. And it won't help because when your program takes a lot of RAM CLR runs GC itself. And if you see that memory consumption is growing, it seems that you have unmanaged resources leak.I edited my code based on ImapX project: there is almost no resources that should be manually disposed. So the only thing can be done here is interning of path. You should also intern login and password as well, because there is no other strings creation by your code. With several improvements is will look like this: