locked
Memory Not Release In Windows Store App RRS feed

  • Question

  • I have created windows store app using c#. In which i am creating image form pdf using pdfdocument class and display in the image. on button click i am removing the objects and call garbage collector after disposing all object why memory is not released?
    doc = await PdfDocument.LoadFromFileAsync(file);
            if (doc != null)
            {
                for (int i = 0; i < doc.PageCount; i++)
                {
                    PdfPage pdf_page = doc.GetPage((uint)i);
    
                    IRandomAccessStream randomStream = new InMemoryRandomAccessStream();
    
                    await pdf_page.RenderToStreamAsync(randomStream);
                    MyImage = new WriteableBitmap((int)pdf_page.Size.Width,(int)pdf_page.Size.Height);
                    await MyImage.SetSourceAsync(randomStream);
                    await randomStream.FlushAsync();
                    randomStream.Dispose();
                    pdf_page.Dispose();
    
                 Image img1=new Image();
                 img1.Source=MyImage;
               imggrid.Items.Add(img1);
                }
    
    
            }
         private void btnunload_click(object sender, RoutedEventArgs e)
        {
          imggrid.Items.Clear();
           
            GC.Collect();
           GC.WaitForPendingFinalizers();
            GC.Collect();
        }

    Wednesday, December 4, 2013 7:25 AM

Answers

  • I think you're overthinking this.  As memory utilization increases in a .NET app, the memory threshold which is used goes up and stays up.  It initially requests some amount of memory - I can't recall what - and as the memory usage increases past that point, it GCs in order to reclaim that memory.  However, if it still doesn't fit in that space, the process claims additional memory, which does not get released, even after GC.  This space is used repeatedly to hold information.  You should not expect that GCing a process results in the total reclamation of all process space.  In fact, it's recommended that you don't mess with GC at all unless you have a specific reason, and it doesn't appear that you have such a reason.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Tanvi03 Thursday, December 5, 2013 5:52 AM
    Wednesday, December 4, 2013 6:20 PM
    Moderator

All replies

  • Why are you managing memory like this?  Is your app crashing due to OOM? 

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, December 4, 2013 3:38 PM
    Moderator
  • Hi,

    My app is not  crashing but when  i load different  pdf ,memory is increase and not released . also i observed that if i navigate from one page  to another back and forth it will not release  memory . is there  any way to get back app memory at initial state?

    Thanks,

    Tanvi.

    Wednesday, December 4, 2013 6:06 PM
  • I think you're overthinking this.  As memory utilization increases in a .NET app, the memory threshold which is used goes up and stays up.  It initially requests some amount of memory - I can't recall what - and as the memory usage increases past that point, it GCs in order to reclaim that memory.  However, if it still doesn't fit in that space, the process claims additional memory, which does not get released, even after GC.  This space is used repeatedly to hold information.  You should not expect that GCing a process results in the total reclamation of all process space.  In fact, it's recommended that you don't mess with GC at all unless you have a specific reason, and it doesn't appear that you have such a reason.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Tanvi03 Thursday, December 5, 2013 5:52 AM
    Wednesday, December 4, 2013 6:20 PM
    Moderator