Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory Leak during processing #103

Open
sej69 opened this issue Nov 2, 2020 · 3 comments
Open

Memory Leak during processing #103

sej69 opened this issue Nov 2, 2020 · 3 comments
Labels

Comments

@sej69
Copy link

sej69 commented Nov 2, 2020

When I created my first AD service about 15 years ago, I found a bug in the C# AD library that would occasionally hang onto a connection and forget that it allocated RAM. Over time this would eat up a lot of memory. Worst part about it, it took days for this to actually start happening. It took me months to figure that out. I fixed it by opening the connection a single time then reusing the connection vs opening and closing each processing attempt.

So while I'm investigating your library for re-writing my service I'm running a lot of benchmarks to test various scenarios I ran into with MS's AD library.

While not as bad as MS's implementation, I do still see a memory leak of some sort going on.

This is the code I am testing:

    private async void btnButton4_Click(object sender, EventArgs e)
    {
        Process currentProcess = Process.GetCurrentProcess();

        lblStartMemory.Text = currentProcess.PrivateMemorySize64.ToString();

        for (int nLoop = 0; nLoop < 10000; nLoop++)
        {
            await DoNext("CN=Hand Created,CN=Users,DC=Testdomain1,DC=Local", nLoop);
            lblCount.Text = "Count: " + nLoop;
            currentProcess.Refresh();
            if (lastMemory != currentProcess.PrivateMemorySize64)
            {
                tbxProcess.AppendText( "Count " + nLoop + " was " + lastMemory.ToString() + " updated to " + currentProcess.PrivateMemorySize64.ToString() + " DIF " + (currentProcess.PrivateMemorySize64 - lastMemory)  + "\r\n");
                lastMemory = currentProcess.PrivateMemorySize64;
            }
            lblMemoryUsage.Text = currentProcess.PrivateMemorySize64.ToString();
        }

        System.GC.Collect();
        lblEndMemory.Text = currentProcess.PrivateMemorySize64.ToString();

    }

    private async Task DoNext(string UserToProcess, int nCount)
    {

        using (var cn = new LdapConnection())
        {
            cn.Timeout = new TimeSpan(0, 0, 2); // 1 minute
                                                        // connect
            cn.Connect("10.1.1.11", 389);
            await cn.BindAsync("GSSAPI", "testadmin@testdomain1.local", "mypass");

            await cn.ModifyAsync(new LdapModifyEntry
            {
                Dn = "CN=Hand Created,CN=Users,DC=TestDomain1,DC=local",
                Attributes = new List<LdapModifyAttribute>
            {
                new LdapModifyAttribute
                {
                LdapModOperation = LdapModOperation.LDAP_MOD_REPLACE,
                Type = "department",
                Values = new List<string> {"testDept " + nCount}
                }

            }
            });
        }
    }

I see a bunch of allocation starting up (which I expect) then random memory jumps. I can click on the button multiple times to continue to see if this is an allocation issue or a lost memory issue. If I had to guess, it's a similar problem I ran into with the connection not closing properly with the MS ad library.

This is the process run once:

Count 0 was 0 updated to 19034112 DIF 19034112
Count 1 was 19034112 updated to 19173376 DIF 139264
Count 2 was 19173376 updated to 19238912 DIF 65536
Count 3 was 19238912 updated to 19304448 DIF 65536
Count 4 was 19304448 updated to 19369984 DIF 65536
Count 5 was 19369984 updated to 19435520 DIF 65536
Count 6 was 19435520 updated to 19501056 DIF 65536
Count 7 was 19501056 updated to 19566592 DIF 65536
Count 8 was 19566592 updated to 19697664 DIF 131072
Count 9 was 19697664 updated to 19836928 DIF 139264
Count 10 was 19836928 updated to 19902464 DIF 65536
Count 11 was 19902464 updated to 19968000 DIF 65536
Count 12 was 19968000 updated to 20033536 DIF 65536
Count 13 was 20033536 updated to 20099072 DIF 65536
Count 14 was 20099072 updated to 20164608 DIF 65536
Count 15 was 20164608 updated to 20230144 DIF 65536
Count 16 was 20230144 updated to 20365312 DIF 135168
Count 17 was 20365312 updated to 20430848 DIF 65536
Count 18 was 20430848 updated to 20496384 DIF 65536
Count 19 was 20496384 updated to 20561920 DIF 65536
Count 20 was 20561920 updated to 20627456 DIF 65536
Count 21 was 20627456 updated to 20692992 DIF 65536
Count 22 was 20692992 updated to 20824064 DIF 131072
Count 23 was 20824064 updated to 20889600 DIF 65536
Count 24 was 20889600 updated to 20955136 DIF 65536
Count 25 was 20955136 updated to 21020672 DIF 65536
Count 26 was 21020672 updated to 21086208 DIF 65536
Count 27 was 21086208 updated to 21151744 DIF 65536
Count 28 was 21151744 updated to 21217280 DIF 65536
Count 29 was 21217280 updated to 21352448 DIF 135168
Count 30 was 21352448 updated to 21417984 DIF 65536
Count 31 was 21417984 updated to 21483520 DIF 65536
Count 32 was 21483520 updated to 21549056 DIF 65536
Count 33 was 21549056 updated to 21614592 DIF 65536
Count 34 was 21614592 updated to 21680128 DIF 65536
Count 35 was 21680128 updated to 21745664 DIF 65536
Count 36 was 21745664 updated to 21811200 DIF 65536
Count 37 was 21811200 updated to 21942272 DIF 131072
Count 38 was 21942272 updated to 22007808 DIF 65536
Count 39 was 22007808 updated to 22073344 DIF 65536
Count 40 was 22073344 updated to 22138880 DIF 65536
Count 41 was 22138880 updated to 22204416 DIF 65536
Count 42 was 22204416 updated to 22405120 DIF 200704
Count 43 was 22405120 updated to 22470656 DIF 65536
Count 44 was 22470656 updated to 22536192 DIF 65536
Count 45 was 22536192 updated to 22667264 DIF 131072
Count 46 was 22667264 updated to 22732800 DIF 65536
Count 47 was 22732800 updated to 22798336 DIF 65536
Count 48 was 22798336 updated to 22863872 DIF 65536
Count 49 was 22863872 updated to 22929408 DIF 65536
Count 50 was 22929408 updated to 22994944 DIF 65536
Count 51 was 22994944 updated to 23126016 DIF 131072
Count 52 was 23126016 updated to 23191552 DIF 65536
Count 53 was 23191552 updated to 23257088 DIF 65536
Count 54 was 23257088 updated to 23322624 DIF 65536
Count 55 was 23322624 updated to 23388160 DIF 65536
Count 56 was 23388160 updated to 23453696 DIF 65536
Count 57 was 23453696 updated to 23523328 DIF 69632
Count 58 was 23523328 updated to 23588864 DIF 65536
Count 59 was 23588864 updated to 23678976 DIF 90112
Count 87 was 23678976 updated to 23683072 DIF 4096
Count 127 was 23683072 updated to 23748608 DIF 65536
Count 128 was 23748608 updated to 23879680 DIF 131072
Count 129 was 23879680 updated to 23945216 DIF 65536
Count 130 was 23945216 updated to 24010752 DIF 65536
Count 131 was 24010752 updated to 24076288 DIF 65536
Count 132 was 24076288 updated to 24141824 DIF 65536
Count 162 was 24141824 updated to 24145920 DIF 4096
Count 163 was 24145920 updated to 24141824 DIF -4096
Count 250 was 24141824 updated to 24145920 DIF 4096
Count 251 was 24145920 updated to 24141824 DIF -4096
Count 277 was 24141824 updated to 24207360 DIF 65536
Count 278 was 24207360 updated to 24272896 DIF 65536
Count 350 was 24272896 updated to 24403968 DIF 131072
Count 351 was 24403968 updated to 24469504 DIF 65536
Count 1201 was 24469504 updated to 24543232 DIF 73728
Count 1946 was 24543232 updated to 24547328 DIF 4096
Count 1947 was 24547328 updated to 24559616 DIF 12288
Count 2040 was 24559616 updated to 24784896 DIF 225280
Count 3150 was 24784896 updated to 24862720 DIF 77824
Count 3960 was 24862720 updated to 24543232 DIF -319488
Count 3961 was 24543232 updated to 24735744 DIF 192512
Count 3962 was 24735744 updated to 24862720 DIF 126976
Count 3969 was 24862720 updated to 24936448 DIF 73728
Count 5178 was 24936448 updated to 25010176 DIF 73728
Count 5892 was 25010176 updated to 25083904 DIF 73728
Count 7192 was 25083904 updated to 25157632 DIF 73728
Count 7457 was 25157632 updated to 25153536 DIF -4096
Count 7512 was 25153536 updated to 25157632 DIF 4096
Count 8395 was 25157632 updated to 25083904 DIF -73728
Count 8549 was 25083904 updated to 25157632 DIF 73728
Count 8772 was 25157632 updated to 25231360 DIF 73728
Count 9960 was 25231360 updated to 25268224 DIF 36864
Count 9988 was 25268224 updated to 25305088 DIF 36864

@flamencist
Copy link
Owner

Hello! What OS used? Windows? I guess that the bug of wldap.dll. Need to check

@sej69
Copy link
Author

sej69 commented Nov 2, 2020

Yes, Windows C# against Windows AD (2016)

@vossjannik
Copy link
Contributor

Hi @sej69,
did you find a workaround for this?
Did you, by any chance, check whether #178 solves these issues for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants