StrataFrame Forum

Releasing memory properly when using a BBS

http://forum.strataframe.net/Topic20807.aspx

By Andria Jensen - 11/19/2008

I have numerous DevExpress grids and tree list controls in my application which I am binding to BOs using BusinessBindingSource controls.  I have recently discovered that DevExpress does a very poor job of releasing memory and disposing of their own object properly.  I have a lot of memory leaks coming up in my app because of this.  Now, to the part where SF is involved...I have been running memory profilers which are showing that the grids, trees, etc are still linked to the BBS which is preventing them from being disposed of properly at garbage collection. 

My theory is that the BBS is somehow dynamically creating columns in the grid or tree at runtime, but not manually disposing of them when it's unloaded.  DevExpress basically expects you to dispose of everything, so I'm wondering if somehow the BBS is still linked to the DevExpress controls and preventing them from being released from memory correctly.  Is this a possibility?

I can open and close a test form I have created without filling the BO that is linked to the BBS, and it releases all memory correctly when the form is closed.  If I fill the BO when opening the form, it will not release all memory once closed.  Any ideas here?

By Trent L. Taylor - 11/19/2008

My theory is that the BBS is somehow dynamically creating columns in the grid or tree at runtime, but not manually disposing of them when it's unloaded.  DevExpress basically expects you to dispose of everything, so I'm wondering if somehow the BBS is still linked to the DevExpress controls and preventing them from being released from memory correctly.  Is this a possibility?

No, sorry.  You have been around for a while and probably remember that we (MicroFour) were initially using DevExpress controls in our medical application and is why we initially created the DevExpress wrapper.  We wrote a single piece of our medical application and experienced massive memory leaks and very poor performance.  It became so bad that we could not release our product using DevExpress and that is when we wrote more SF controls and Themes got introduced.

The BBS follows .NET binding standards and does not create anything within the grid.  If the DevExpress grid is not releasing the binding, this is on the DevExpress side...period.  The BBS does nothing more than implement the IBindingList interface and then expose a unique BO with the strong-typed properties from the wrapped BO.  But if the BBS is properly managed, the Dispose should be picked up and cleaned up through the garbage collector.

As a side note here, we have hundreds of Custom BBS classes in our medical application and we have no memory leaks and we deal with massive amounts of data throughout the life of a session.  So I would never say that there is no possible way that the BBS may not be getting cleaned up due to an SF bug, but I will say that it is very unlikley.

I can open and close a test form I have created without filling the BO that is linked to the BBS, and it releases all memory correctly when the form is closed.  If I fill the BO when opening the form, it will not release all memory once closed.  Any ideas here?

Possibly.  Now this is generally more attributed to rendering issues, but there is a lot of logic that happens in the instantiation of objects as well as registering itself with the parent container or control as well as the native .NET collections )i.e. garbage collector).  You might wait until a handle is created by testing on the IsHandleCreated or override the OnLoad of the form and fill there.  If you are doing the Fill in a constructor the I could see this being more of an issue.  Finally, Fill from different locations to see where the GC finally recognizes the object.  For example, the Shown event, etc.  This is all going to be related to .NET and the GC versus the management of the BBS and BOs.

By Andria Jensen - 11/20/2008

I think I understand what you are saying...basically, depending on the area where you fill a BO it may or may not get registered with the collection that the GC uses to dispose of objects in memory during collection cycles.  Is that right? 

But, if I am manually disposing of the BO and the BBS in the FormClosed event, shouldn't this be enough no matter where it's being filled or instantiated? 

Also, what is the proper and recommended way to manage memory as far as StrataFrame objects go?  Should we be manually disposing of things when they are no longer in use or is it ok to leave that to the GC?

By Trent L. Taylor - 11/20/2008

I think I understand what you are saying...basically, depending on the area where you fill a BO it may or may not get registered with the collection that the GC uses to dispose of objects in memory during collection cycles.  Is that right? 

Yes.

But, if I am manually disposing of the BO and the BBS in the FormClosed event, shouldn't this be enough no matter where it's being filled or instantiated? 

Not if something holds on to an event or an instance binding and will not release it.

Also, what is the proper and recommended way to manage memory as far as StrataFrame objects go?  Should we be manually disposing of things when they are no longer in use or is it ok to leave that to the GC?

If you drop an instance on the form, the Dispose should be intrinsically called as all BOs inplement the IDidposable interface.  If done in code, then manually Dispose of the object.

By Andria Jensen - 11/21/2008

I'm still tracking this down, but have come across a lot of places where I was not manually disposing of a BO when I instantiated in code.  I was not aware that the BO would not be collected automatically by the GC.  So, I am now going through my code and cleaning up everything I can find.  Which leads me to my question now, where is the appropriate place to dispose of the class level BOs I have created inside of another BO?  The dispose method is already overriden in the designer code, but I would think that would be the place to do it.  What is the best practice here?
By Trent L. Taylor - 11/21/2008

You can place your code in the Dispose of that class.  Just place your code at the top of the Dispose :

If disposing Then
   myDefinedBO.Dispose()
End If
By Andria Jensen - 11/21/2008

But if it is the dispose of a BO, it is in the designer code which is regenerated with each partial.  Is this where you are referring to?

<System.Diagnostics.DebuggerNonUserCodeAttribute()> _
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  If (disposing AndAlso (Not (Me.components) Is Nothing)) Then
    Me.components.Dispose()
 
End If

  MyBase.Dispose(disposing)

End Sub

 

By Trent L. Taylor - 11/21/2008

From your post, I thought you were referring to a BBS instead of the BO.  Technically, in this example, they objects should dispose of themselves since the BOs implement IDisposable.  However, .NET isn't always good about managing this. 

In this particular example, I rarely manually dispose of the object.  And we have this type of code all over the place.  We have hundreds of instances where we have this type of code and it gets cleaned up.  When I was referring to in code, I was talking about as a local like this:

Dim lc As New MyBo()

'-- Do your stuff here

lc.Dispose()

However, it never hurts to manually Dispose() of objects if they are getting "hung up" due to the environment in which they are being used.  So in your case, I would manually Dispose() to see if memory gets cleaned up.  In this case, I would palce all of my logic in the BaseBO in which all of my BOs inherit.  Then this code would be implemented in a single location versus having to be re-written for each BO.  Also, using this approach will not be an issue with the partial class.

By Andria Jensen - 11/21/2008

I actually was referring to the instance you were talking about.  Basically something like this:

Public Class MyBO

  Private MyOtherBO as New BOType

  -----  subs, functions, whatever ----

End Class

Where does MyOtherBO get disposed of?  It's a BO instantiated as a class level variable, inside of another BO.  It should go in the dispose for MyBO, but that is being generated inside the Designer code and will get overwritten if I regenerate the partial.

By Trent L. Taylor - 11/21/2008

Where does MyOtherBO get disposed of?  It's a BO instantiated as a class level variable, inside of another BO.  It should go in the dispose for MyBO, but that is being generated inside the Designer code and will get overwritten if I regenerate the partial.

That is what I was referring to when talking about the BaseBO.  All of your BOs should inherit from a BaseBO that inherits BusinessLayer.  This allows you to provide base level logic for this very type of thing without having to code it for each individual BO and at the risk of the Dispose being overwritten.

By Andria Jensen - 11/21/2008

I am inheriting from my own BaseBO.  I guess I'm just confused as to what would be in the Dispose at the base level.  Would the declared BO be part of a collection of some sort that i would iterate through and dispose?
By Trent L. Taylor - 11/21/2008

Create a method on the BaseBO that adds the BO to a collection, then enumerate through that collection in the Dispose of the BaseBO to dispose of them.  So you would go into the constructor of the inherited BO and add those custom BOs to a collection like this:

Me.AddCustomBoDeclaration(Me.MyChildBO)
Me.AddCustomBoDeclaration(Me.MyChildBO2)

Then in the Dispose of your BaseBO, you know what you need to do.  The other option would be to use reflection which gets more complicated, but you wouldn't ahve to do anything within the inherited BOs.  The downside of reflections, though, could be that it could slow things down if not done 100% right.

By Andria Jensen - 11/21/2008

Duh!  That makes perfect sense.  I've been looking at this stuff too long and couldnt see that solution for some reason.  Okay, I will try that out and see what kind of results I get.  And if I get brave I will try the reflection approach next.  Thanks so much Trent.
By Trent L. Taylor - 11/21/2008

No problem, glad to help Smile
By Andria Jensen - 11/24/2008

I have added the below code to my base BO....

Protected DeclaredBOs As New Collection(Of IDisposable)

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
 
If disposing Then
   
If components IsNot Nothing Then components.Dispose()
   
For Each bo As IDisposable In DeclaredBOs
      bo.Dispose()
   
Next

    DeclaredBOs.Clear()
 
End If
 
 
MyBase.Dispose(disposing)

End Sub

...however, i'm still not seeing the BO getting disposed of properly.  I feel like I have to be missing something.  I have a form with a grid and a textbox on it, both of which are bound to a BO.  The grid is bound through a BBS.  When I close the form, it appears to be hitting all of the appropriate logic to dispose of the BO, but when I look at it in a memory profiler, it shows 5 instances of the BO in memory.  What it seems like it 1 instance for the textbox, and 4 instances for the grid (one for each row of data).  If I only have one row of data in the grid, there is only one instance of the BO. 

SO a couple of questions... why are there so many instances of the BO in memory?  It seems as if it is creating an instance for each control it's binding to, and I would expect one instance only.  Also, is there anything else I could possibly be overlooking in this scenario that would be causing the BO to stay in memory.  I have been looking at this for over a week now and am still not able to pinpoint the cause of why my BOs are staying in memory and not disposing correctly.

 

By Andria Jensen - 11/24/2008

I have created a sample projects which demonstrates the design I was talking about earlier.  It is using a windows grid, so DevExpress is taken out of the equation.  I have used a BBS as the data source, pointing to the Customers table.  I have two textboxes also bound to the BO.  When the form is closed, it does not come out of memory and there are 11 instances of the BO in memory.  I have also attached a screenshot of the memory profile after closing the form and doing a GC.Collect.  Can you tell me what's going on here?
By Trent L. Taylor - 12/1/2008

Taking a look at this.  Thanks for the sample, it always helps speed up the process.
By Danny Doobay - 11/20/2013

TaylorAndria Jensen

Did you get this resolved?
By Purnell W Glenn - 3/4/2014

Trent,

Did you have an attachment sample for your solution?
By Trent L. Taylor - 3/5/2014

Purnell:

This particular issue was actually not specifically an SF issue, however, there have been some pretty significant changes in SF since this post regarding how we clean up handlers.  I wasn't sure exactly what you were asking though.  Sorry for any confusion.