Handle leak in AlphaVSS?

Oct 11, 2012 at 2:39 AM

I am running the alphashadow sample in a loop to create 1000 snapshots. I have added a while loop around the code that creates snapshot. I am seeing that the thread count, handle count and memory (private working set) keep increasing.

At the end of 70 loops:

Memory =296 MB

Handles = 2100

Threads = 90

 

The process finally dies.

I am trying to understand which code is leaking so much handles / memory. Will appreciate any help...

 

Oct 12, 2012 at 8:31 AM
Edited Oct 12, 2012 at 8:33 AM

One of the issues is inside Error.cpp -> WaitCheckAndReleaseVssAsyncOperation function in AlphaVSS sources. There is no Release() for pAsync at the end.

We also got huge memory leak when taking snapshots. After releasing pAsync everything looks good.

Oct 12, 2012 at 9:52 PM

Thanks!

Just fixed it yesterday. Wrapped the IVssAsync in CComPtr in each function (DoSnapshotSet, GatherWriterMetadata, etc.) before calling WaitCheckAndReleaseVssAsyncOperation.

Coordinator
Nov 9, 2012 at 5:09 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Oct 22, 2013 at 1:01 PM
This issue is still reproducible with latest code changes (v1.2) and with current dev sources.
Seams like the memory is consumed in GatherWriterMetadata() and never gets released.
The attached zip contains a sample app that demonstrates this behavior.

Can you please take a look on this? I have no idea what might be the cause of this memory leak.
I have the app which is running as Win Service and uses AlphaVSS. At some time it crashes with OutOfMemoryException(). The investigation led me to this discussion thread.

Thanks.
Nov 30, 2013 at 10:37 AM
Edited Nov 30, 2013 at 11:17 AM
Same problem here. The issue is also reproducible with v1.1 (.NET2).
I tried to release pAsync and I also tried to wrap IVssAsync in CComPtr (GatherWriterMetadata) without success.

@anaghab, OBogomazov: Could you paste a code diff of your VssBackupComponents.cpp and/or error.cpp? That would be great.

Thanks
Coordinator
Dec 1, 2013 at 12:45 PM
I'm actually having some problems finding the cause of this issue. I wrote a very short C++ program cotnaining the following code:
#define CHECK(hr) { if (FAILED(hr)) { printf("Error: %x\n", hr); return -1; } }

int _tmain(int argc, _TCHAR* argv[])
{ 
   CoInitialize(NULL);
   IVssBackupComponents *pBackup;
   CreateVssBackupComponents(&pBackup);

   CHECK(pBackup->InitializeForBackup(0));

   IVssAsync *pAsync;
   CHECK(pBackup->GatherWriterMetadata(&pAsync));
   CHECK(pAsync->Wait());   
   pAsync->Release(); 

   pBackup->FreeWriterMetadata();   
   int refCnt = pBackup->Release();

   printf("Refcnt after release: %i\n", refCnt);

   return 0;
}
I would have expected it to print 0 as the reference count after release, but it prints 5, which means that for some reason there are still 5 references to this object and its memory is not getting freed. This problem only occurs after calling GatherWriterMetadata. I suspect this may be the cause of the memory leak. I will post a question on some other forums and see if someone can explain this.
Coordinator
Dec 3, 2013 at 8:10 PM
I posted a question at msdn forums and it seems that the resources are not released when releasing the objects holding them, but slowly at some point later by the VSS service somehow.

If you run your example program, with an eternal loop after all the calls to the VSS service, that prints the memory usage like once every 20 seconds or so, and you let it run for a long time, you will actually see the memory usage decrease. So from what I can determine this is not a bug in AlphaVSS, but rather the behavior of the underlying VSS components.

The fact that your service crashes with an OutOfMemoryException seems kind of strange to me though. How often is it creating snapshots really? It seems that this behavior should not cause an out of memory situation under normal usage. So I'm wondering if there is a problem somewhere else in the code maybe?
Dec 4, 2013 at 8:59 PM
Thank you for the fast support!

I've made myself crazy for nothing :) With a break of 5 minutes it works like a charm.

Thanks again
Jan 29, 2014 at 9:43 AM
Hi,
Want to thank you for your efforts.
You're right at some point windows service makes to many calls to AlphaVSS with initialization of VSS underlying components.
Seems like a refactoring is needed in my code.

Thanks!
Aug 11, 2014 at 12:56 PM
Hi,
If this thread is still alive I just need some consultation from someone.
Here is the simplest code my windows service is using.
public static List<Guid> ListAllWriters()
{
    var guids = new List<Guid>();
    IVssImplementation vss = VssUtils.LoadImplementation();
    using (IVssBackupComponents components = vss.CreateVssBackupComponents())
    {
        components.InitializeForBackup(null);
        components.GatherWriterMetadata();

        try
        {
            IList<IVssExamineWriterMetadata> metadata = components.WriterMetadata;
            guids.AddRange(metadata.Select(t => t.WriterId));
        }
        finally
        {
            components.FreeWriterMetadata();
        }
    }
    return guids;
}
I've read decaf answer and msdn thread which states that VSS service will release memory after some time in future. But this never happens with this code sample above. This code executes 2-3 times per day and results in large memory consumptions by the service in a few days.
Could someone please clarify if I'm using code right? I'm not seeing any memory leak in it.
Thanks.