Dispose Code changed

Use this forum to read, ask or inform about memory issues in the .NET Framework and third party components.

Moderator: SciTech Software

Dispose Code changed

Postby pavanpuligandla » Tue Sep 13, 2011 11:06 am

Hi All,
I ran .NET memory profiler 4.0 for my ASP.NET Application and identified issues like 'Object disposed but not GCed', Now i've changed my code and tested it with profiler again, Now i dont see any errors, But my question here is related to .NET Coding.

Scenario:
I need to get data from database and bind it to a gridview, For that i'm using Generic List<T> type across all the layers of my application to bind it to grid, Which is shown as below:
Code: Select all
private void GetOnlineJobRequisitions()
        {
            using (JobActivities jobActivity = new JobActivities())
            {
                List<JobRequisitionDTO> jobrequsitionrequstsList = jobActivity.GetApprovedcandidates();

                try
                {
                    if (jobrequsitionrequstsList.Count > 0)
                    {
                        gvOnlineJobRequisitions.DataSource = jobrequsitionrequstsList;
                        gvOnlineJobRequisitions.DataBind();
                    }
                    else
                    {
                        UpdatePanel9.Update();
                    }
                }

                catch (Exception ex)
                {
                    ExceptionHandler.Equals(ex.Message, ex);
                }
            }
        }

Here is there any necessity to NULL the List object?? Prior to this change i used finally block and in finally block i used to write jobrequsitionrequstsList =null;
Is there any problem if i dont null the LIST? Can anyone please help me out with good suggestions and improvements.

Awaiting your response,
Thanks.
pavanpuligandla
 
Posts: 2
Joined: Tue Sep 13, 2011 11:00 am

Re: Dispose Code changed

Postby Andreas Suurkuusk » Tue Sep 13, 2011 9:15 pm

No, there's no problem that you don't null the jobrequsitionrequstsList. In fact, nulling the jobrequsitionrequstsList can potentially delay the garbage collection of the list instance. As soon as the variable goes out of scope it will no longer prevent the instance from being GCed. If you explicitly null the variable, the variable scope is widened.
Best regards,

Andreas Suurkuusk
SciTech Software AB
Andreas Suurkuusk
 
Posts: 964
Joined: Wed Mar 02, 2005 7:53 pm
Location: Sweden

Re: Dispose Code changed

Postby pavanpuligandla » Wed Sep 14, 2011 2:38 am

Andreas Suurkuusk wrote:No, there's no problem that you don't null the jobrequsitionrequstsList. In fact, nulling the jobrequsitionrequstsList can potentially delay the garbage collection of the list instance. As soon as the variable goes out of scope it will no longer prevent the instance from being GCed. If you explicitly null the variable, the variable scope is widened.


Hi Andreas,
Thanks for the comeback..

Are there any code issues or can you please suggest any improvements in the above code? So no need to null the Generic List?
Code: Select all
List<JobRequisitionDTO> jobrequsitionrequstsList = jobActivity.GetApprovedcandidates();

Can i rewrite the above line with using directive? Any suggestions please..

When i ran memory profiler, I didnt get any serious memroy leaks, But it is telling that Instances were disposed but not properly GCed. what does this error mean? and how can i rectify this throughout my code?

Thanks.
pavanpuligandla
 
Posts: 2
Joined: Tue Sep 13, 2011 11:00 am

Re: Dispose Code changed

Postby Andreas Suurkuusk » Wed Sep 14, 2011 12:59 pm

The warning about disposed instances tells you that you have called the Dispose method on an instance, but the instance is still alive at the time of the snapshot. Since disposing an instance normally indicates that you don't need the instance anymore, it should become also be collected. There are exceptions to this, for example if the instance is returned to a pool when it is disposed and resused a later point. In this case you can just Ignore the issue.

The root paths of the disposed instance will provide you with information about why it has not been GCed. In your example, is seems plausible that the JobRequisitionDTO instances have references back to the "parent" jobActivity. If this is the case, the jobActivity instance will be kept alive by the JobRequisitionDTO instance, even after the jobActivity instance has been disposed and you will get the warning issue. To avoid the issue, you can try to avoid disposing the jobActivity until the jobrequsitionrequstsList databinding source has been cleared, or you can try remove the reference to the parent instance.
Best regards,

Andreas Suurkuusk
SciTech Software AB
Andreas Suurkuusk
 
Posts: 964
Joined: Wed Mar 02, 2005 7:53 pm
Location: Sweden


Return to Known Memory Issues

Who is online

Users browsing this forum: No registered users and 1 guest

SciTech Software logo

© Copyright 2001-2016. SciTech Software AB
All rights reserved.


SciTech Software AB
Kartvägen 21
SE-175 46 Järfälla
Sweden


E-mail: mail@scitech.se

Telephone: +46-706868081

cron