Git Product home page Git Product logo

Comments (5)

ThomasHoevel avatar ThomasHoevel commented on May 27, 2024

How do you know it is never cleared from memory? The garbage collection should remove it.

from pdfsharp.

sneakyfluffy avatar sneakyfluffy commented on May 27, 2024
            Thread.Sleep(5000);
            
            using (PdfDocument pdfDocument = PdfReader.Open("a.pdf", PdfDocumentOpenMode.Import))
            {
                pdfDocument.Dispose();
            }
            Console.WriteLine("pdfDocument should be closed and removed from memory");
            Console.ReadKey();

report1
snap1

In my understanding, when the program reaches Console.Writeline, GC should remove pdfDocument from memory.

from pdfsharp.

ThomasHoevel avatar ThomasHoevel commented on May 27, 2024

GC runs when it runs - unless you invoke it explicitly.
At WriteLine the object should be disposed, but GC will not run automatically at every Dispose.
Application memory will not shrink even if heap has large unused areas.
What do you get when you do Import/Dispose 10 times in a loop?

Code to invoke GC:

GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
GC.Collect();

from pdfsharp.

sneakyfluffy avatar sneakyfluffy commented on May 27, 2024
            Thread.Sleep(5000);
            
            for(int i = 0; i < 10; i++)
            {
                using (PdfDocument pdfDocument = PdfReader.Open("file.pdf", PdfDocumentOpenMode.Import))
                {
                    pdfDocument.Dispose();
                }

            }
            GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
            GC.Collect();
            Console.WriteLine("pdfDocument should be closed and removed from memory");

            Console.ReadKey();

report2

snap2

Running GC manually does not seems to make any diffrence.
file.pdf is 489MB

from pdfsharp.

ThomasHoevel avatar ThomasHoevel commented on May 27, 2024

So it is about 500 MB after one cycle and about 500 MB after 10 cycles.
Looks as if garbage collection works as it should, even though it does not occur when you expect it.

from pdfsharp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.