Click here to Skip to main content
15,901,982 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Hello ,

I have given a project to identify the root cause for the memory leaks in .net based web application.

This is the first time i have given such task. I not sure how to look for the memory leaks any suggestions please.

I updated my question with code block will that code block result in any memory leak.
Can anybody review the code block and provide some information about code line which may cause memory leaks if any.

What I have tried:

C#

checking for static classes and using diagnostic tools provided by visual studio 2019

C#
<pre>[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SaveFileData(FileEdit model)
        {
            var file = new File();
            EntityMapper<FileEdit, File>.Map(model, file);
            string fileName = string.Empty;
            string relativePath = string.Empty;
            string extension = string.Empty;

            file.SchoolId = SessionHelper.CurrentSession.SchoolId;
            file.IsActive = true;
            bool result = false;
            if (model.PostedFile != null)
            {
                fileName = model.PostedFile.FileName;
                extension = Path.GetExtension(model.PostedFile.FileName).Replace(".", "");


                //string resourceDir = Server.MapPath(Constants.ResourcesDir);
                //string fileContent = Server.MapPath(Constants.FileDir);
                //string fileModule = Server.MapPath(Constants.BlogDir + "/Module_" + model.ModuleId);
                //string filePath = Server.MapPath(Constants.BlogDir + "/Module_" + model.ModuleId + "/User_" + SessionHelper.CurrentSession.Id);


                string resourceDir = MyConfiguration.Instance.WriteFilePath + Constants.ResourcesDir;
                string fileContent = MyConfiguration.Instance.WriteFilePath + Constants.FileDir;
                string fileModule = MyConfiguration.Instance.WriteFilePath + Constants.FileDir + "/Module_" + model.ModuleId;
                string filePath = MyConfiguration.Instance.WriteFilePath + Constants.FileDir + "/Module_" + model.ModuleId + "/User_" + SessionHelper.CurrentSession.Id;

                Common.Helpers.CommonHelper.CreateDestinationFolder(resourceDir);
                Common.Helpers.CommonHelper.CreateDestinationFolder(fileContent);
                Common.Helpers.CommonHelper.CreateDestinationFolder(fileModule);
                Common.Helpers.CommonHelper.CreateDestinationFolder(filePath);
                fileName = Guid.NewGuid() + "_" + fileName;
                relativePath = Constants.FileDir + "/Module_" + model.ModuleId + "/User_" + SessionHelper.CurrentSession.Id + "/" + fileName;

                var extId = _fileTypeList.FirstOrDefault(r => r.Extension.Contains(extension.ToLower())).TypeId;

                fileName = Path.Combine(filePath, fileName);

                model.PostedFile.SaveAs(fileName);
                file.FileName = model.PostedFile.FileName;
                file.FilePath = relativePath;
                file.FileTypeId = extId;
                file.FolderId = !string.IsNullOrWhiteSpace(model.ParentFolderId) ? Convert.ToInt32(EncryptDecryptHelper.DecryptUrl(model.ParentFolderId)) : 0;
                file.SectionId = !string.IsNullOrWhiteSpace(model.SectId) ? Convert.ToInt32(EncryptDecryptHelper.DecryptUrl(model.SectId)) : 0;

                if (model.FolderId > 0)
                {
                    file.UpdatedBy = SessionHelper.CurrentSession.Id;
                    result = _fileService.Update(file);
                }
                else
                {
                    file.CreatedBy = SessionHelper.CurrentSession.Id;
                    result = _fileService.Insert(file);
                }
                return Json(new OperationDetails(result), JsonRequestBehavior.AllowGet);
            }
            return Json(new OperationDetails(result), JsonRequestBehavior.AllowGet);
        }
Posted
Updated 22-Oct-19 20:11pm
v3

1 solution

Here is an overview of free .NET profilers: best-free-net-profilers[^]

See CodeProject articles: Easy Detection of Memory Leaks[^]
And: Best Practices No. 5: Detecting .NET application memory leaks[^]

And this article: Debugging Hard to Reproduce Issues[^]

If you are willing to spend some money, the SciTech debugger is a good choice: Improve Your Visual Studio Debugging Experience with .NET Memory Profiler[^]

It's recommended to use the using statement: using statement - C# Reference | Microsoft Docs[^]
Some objects might also need to be disposed, see: Dispose And Finalize in C# with examples and use cases[^]
 
Share this answer
 
v4
Comments
khaleelsyed 22-Oct-19 3:28am    
Thank you for your reply and suggested links
khaleelsyed 23-Oct-19 2:13am    
i have updated my question and added some code block which contains logic for uploading files can u please review the code lines and update if any issues with the code which may cause the memory leaks.
RickZeeland 23-Oct-19 2:46am    
I don't see any using {} constructions in your code, maybe that would help. What kind of mapping framework are you using ?
khaleelsyed 23-Oct-19 3:02am    
means using using {} can resolve the memory leak issue no other change required?
RickZeeland 23-Oct-19 3:05am    
Maybe, when using using (haha) you are sure the used object will be disposed. I will update the solution with some links.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900