This is a migrated thread and some comments may be shown as answers.

Removing old pdfs from memory

5 Answers 245 Views
PDFViewer
This is a migrated thread and some comments may be shown as answers.
Ars
Top achievements
Rank 1
Ars asked on 13 Dec 2012, 08:59 AM
Hi all,

I`m using RadPdfViewer to show user a pdf files from my database (files stores at database by Filestream).
 
<telerik:RadPdfViewer x:Name="PdfViewer" Grid.Column="1" ></telerik:RadPdfViewer>

I`m loading files by Uri, here is my code:

private void BrowseDocumentOnClick(object sender, RadRoutedEventArgs e)
{
    //check if user have select document from tableview
    if (_viewModel.SelectedEntity == null) return;
 
    //form uri query to server. Inserting document identifier
    var uri = new Uri(String.Format("/Files/FileView.aspx?docId={0}", _viewModel.SelectedEntity.pk_documentid),
                      UriKind.Relative);
     
    //start loading file by chunks from server
    this.PdfViewer.DocumentSource = new PdfDocumentSource(uri);
}


The problem is that when I`m trying to sequentially see several different documents by one instance of RadPdfViewer  - the old documents are not removed from memory. And I`dont find any method at "this.pdfViewer" which could clean the memory from old documents.

5 Answers, 1 is accepted

Sort by
0
Kammen
Telerik team
answered on 13 Dec 2012, 04:44 PM
Hello Ars,

Can you clarify in what way you understand that documents are not removed from the memory? Maybe send us a demo project that will reproduce this issue in a support ticket?
 
We have tested RadPdfViewer and in our test project, the documents were released correctly.

Looking forward to your answer.

Regards,
Kammen
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Ars
Top achievements
Rank 1
answered on 17 Dec 2012, 10:15 AM
| Can you clarify in what way you understand that documents are not removed from the memory?

1. After solution run (two projects:asp.net, silverlight app) Instance of IE 9 at task manager shows 60-62 mb of memory usage.
2. When I download a first pdf from a server side to telerik pdf control, the memory usage increase to nearly 70-75 mb ( exact size of upload file - 1 mb).
3. When I download another pdf file from server, the memory usage increases up to 200 mb. (though pdf file size is about 2 mb). (It`s consist of images inserted inside a pdf file)

I`ve attached 3 pics which show memory usage.
1. 0 pdf files download from server
2. 1 pdf files download from server
3. 2 pdf files download from server


The code of server side which sends file in chunks by bytes. (Code of client side I published in my first post. )
using System;
using System.IO;
using System.Linq;
  
namespace ATZO.Web.Files
{
    public partial class FileView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var doc = Page.Request.QueryString["docId"];
  
            // some check logic
            if (doc == null)
            {
  
            }
  
            var DocumentId= long.Parse(doc);
  
            Response.ContentType = "application/pdf";
            Response.AddHeader("Pragma", "no-cache");
  
            var doc = new document();
  
            //get document from database
            using (var dbConnect= new DBConnect())
            {
                doc= dbConnect.reg_document.SingleOrDefault(_=> _.DocId  == DocumentId);
            }
  
            int BufferSize = 8000;
  
            var fileBytes = doc.fileDATA;
              
            try
            {
  
                long fileSize = fileBytes.Length;
                int chunkLength = 0;
                int position = 0;
                var chunk = new Byte[BufferSize];
                //count of bytes left to transmit            
                var bytesLeftToTransmit = fileSize;
  
                while (bytesLeftToTransmit > 0)
                {
                    chunkLength = fileBytes.Skip(position).Take(BufferSize).Count();
  
                    chunk = fileBytes.Skip(position).Take(chunkLength).ToArray();
  
                    Response.OutputStream.Write(chunk, 0, chunkLength);
  
                    Response.Flush();
                    position += chunkLength;
                    bytesLeftToTransmit -= chunkLength;
                }
            }
            finally
            {
                Response.End();
            }
            Response.End();
       }
    }
}
0
Kammen
Telerik team
answered on 20 Dec 2012, 10:05 AM
Hello Ars,

 As we mentioned before, we have tested this scenario with a profiler and the results are that all documents are removed as soon as the garbage collector collects them. The memory usage is increased because of the UI virtualization that RadPdfViewer implements. We do not release the created UI elements, but we store them in a factory so they can be used later. This behavior is due to performance reasons and we do not plan to change it for the moment.

Also, it is normal that  RadPdfViewer uses more memory than the PDF document's size, because additional preprocessing is done, document elements are created and so on.

Hope this answers your question.

Regards,
Kammen
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Ars
Top achievements
Rank 1
answered on 20 Dec 2012, 03:55 PM
 Hello Kammen,
thank you for your reply.

My anxiety was associated with a dramatic increase in memory usage.
From 60 mb to 200-250 mb by showing a client a few pdf files (2-4).

Regards,
Ars Stepanov
0
Jared
Top achievements
Rank 2
answered on 25 Nov 2015, 10:06 PM

Hi Ars,

I'm with you- i have an app that shows users PDF files, and a 6MB pdf turns into 400MB+ in memory. 
i find that if you dispose the pdf viewer after it closes it still remains in memory- until you create a new pdfViewer object then the memory usage drops back to normal.

So what i do now, is when i want to view a PDF, i create a new tab (with option: CloseAction = DockWindowCloseAction.CloseAndDispose) and with that tab, a new RadPdfViewer & RadPdfViewerNavigator. it seems when you create another pdfviewer object the previous one gets removed from memory- does not matter if the tab is disposed, or you can force a GC(not recommended) but the code is below.

 Note: if you are going to run your app in a shared environment like a Terminal Server and call the GC collects, the runtime options in your App.Config should reference the <gcServer> and <gcConcurrent> options and be set to TRUE. It goes without saying: Use at your own risk and always test prior to deployment...

Dim CurProc As Process = Process.GetCurrentProcess()
'If the Process has gone over 500MB
 If FormatNumber(CDbl(((c.WorkingSet64 / 1024) / 1024)), 2) > 500.0 Then
   Try
      GC.Collect()
      GC.WaitForPendingFinalizers()
   Catch ex As Exception
   'Do something with our error here.
   End Try
 Else
 'Still within our operating limitations.
 End If
 'Cleanup
 CurProc = Nothing

Tags
PDFViewer
Asked by
Ars
Top achievements
Rank 1
Answers by
Kammen
Telerik team
Ars
Top achievements
Rank 1
Jared
Top achievements
Rank 2
Share this question
or