1

We have implemented a winforms application using vb.net. We are facing and verified some memory leaks using ANTS memory profiler.

The problem occurs when we have a LINQ with an if statement.

For example the below LINQ:

 `Dim vLst = (From v In myPageDbContext.customer
                Group Join p In myPageDbContext.discounts
                On p.discountid Equals v.discountid Into Group From p In Group.DefaultIfEmpty
                Where v.customerid = dCustId
                Select firstname = If(v.firstname IsNot Nothing AndAlso v.firstname.Trim.Length > 0,     v.firstname, ""),
                       surname = If(v.surname IsNot Nothing AndAlso v.surname.Trim.Length > 0, v.surname, v.dtitle),
                       v.phone1,
                       p.discpercent,
                       v.comments,
                       mobilephone = If(If(v.mobilephone Is Nothing, "", Trim(v.mobilephone)).Length = 0,    If(v.phone2 Is Nothing, "", Trim(v.phone2)), If(v.mobilephone Is Nothing, "",   Trim(v.mobilephone)))).FirstOrDefault()`

If we remove the if statement the problem is solved.

We have tried everything we no success and have found a solution by converting the LINQ with if statement to SQL commands that do not create a memory leak in our case.

Before doing the above we are just wondering if we are doing something wrong by using the if statement in LINQ and if this is normal?

  • Can you tell which if operator is leading to the leak? There are 6 in the code shown. Or is it *every* time there is an if operator used in LINQ? – Andrew Morton May 08 '23 at 18:19
  • It is every time there is an if operator used! – user8429599 May 08 '23 at 18:40
  • Are you disposing of the context as soon as possible, as written in [Memory leak when using Entity Framework](https://stackoverflow.com/a/30209700/1115360)? – Andrew Morton May 08 '23 at 18:47
  • Yes we have tried everything. If we remove the if statements of the LINQ it works correct without a memory leak – user8429599 May 08 '23 at 18:58
  • Only if you remove all the `If` functions? The first two don't seem to be doing anything that should create a "memory leak". – NetMage May 08 '23 at 18:59
  • The above is just an example because we have LINQ with if statements everywhere in our application. And yes we found out if we remove the if it stops creating a memory leak – user8429599 May 08 '23 at 19:12
  • Would using [Generated Values](https://learn.microsoft.com/en-us/ef/core/modeling/generated-properties?tabs=data-annotations) help? – Andrew Morton May 08 '23 at 19:18
  • N.B. The code shown is using the [If operator](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/if-operator), not if statements. – Andrew Morton May 08 '23 at 19:22
  • I don't think it would help. It is just so weird and we couldn't find anything on the internet – user8429599 May 08 '23 at 19:23
  • Yes you are correct – user8429599 May 08 '23 at 19:24
  • If all the instances are of the form of COALESCE in SQL, you could use something like `{z.A, z.B, "pqr"}.First(Function(s) Not String.IsNullOrWhiteSpace(s))` instead of the If operator, where "pqr" is the fallback value. – Andrew Morton May 08 '23 at 19:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/253545/discussion-between-user8429599-and-andrew-morton). – user8429599 May 08 '23 at 19:43
  • @NetMage The first two `If` operators appear to be creating new strings via the `Trim` calls which might be allocating new memory. I'm not familiar with the tool, so I don't know how reliably it accounts for garbage collection and the indeterminacy of when no-longer-used memory is returned to .NET. – Craig May 08 '23 at 21:16

0 Answers0