Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Here I have a List<DocuemntModel>, where DocumentModel holds property byte[] XpsData.
After converting byte[] XpsData into XPSDocument to bind in <DocumentViewer>
    public static XpsDocument ByteToXpsDocument(byte[] sourceXPS)
{
       temp++;
       MemoryStream ms = new MemoryStream(sourceXPS);
       string memoryName = "memorystream://ms" + temp + ".xps";
       Uri memoryUri = new Uri(memoryName);
       try
       {
       PackageStore.RemovePackage(memoryUri);
       }
       catch (Exception) { }
       Package package = Package.Open(ms);
       PackageStore.AddPackage(memoryUri, package);
       XpsDocument xps = new XpsDocument(package, 
       CompressionOption.SuperFast, memoryName);
       return xps;
    }
List<DocumentModel> will loaded 300+ objects to bind in UI. For in each object it takes 400+ kb size of XPSDocument .

I have sucessfully done Bindings. But when page has been loaded the App size increases 300MB . Because of loading all 300+ XPSDocument in page UI.

After i closes the current page, App memory size remains stable. (What im expecting is after clsosing a page it will release all its memory and App size will getback to inital size.) and it is not happening.

When i go back and come again to this same page with another 300+ data, the app size increaces 500+. App getting slow by slow. Memory is also holding previous data(not requrired any more) of this current page.

What I have tried:

<pre lang="C#">
GC.Collect();
GC.WaitForFullGCComplete();
GC.Collect();
Posted
Updated 25-Mar-22 6:01am

1 solution

Quote:
The GC does not dispose your objects, as it has no knowledge of IDisposable.Dispose() or IAsyncDisposable.DisposeAsync().


You're not "disposing" the objects that need it (like MemoryStream).

https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/using-objects
 
Share this answer
 
Comments
Member 14858655 29-Mar-22 6:02am    
Can you please help with the sample code?

How to implement this when by bindings.

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