Q: I have a .NET library that exposes functionality built using the
PDFNet library. Can you please tell me how should I handle the
Initialize() and Terminate() calls of the PDFNet library? I saw in the
documentation that Initialize() is called once, during process
initialization, but I may have many clients using this library in the
same time. Given the fact that Initialize() is called inside my
library, how can this affect me? Or how should I handle this?
The same for the Terminate() call. Imagine that again I have many
clients that use my library. One of them has just terminated work and
will call Terminate(). I see that it is unsafe to call any other
PDFNet API after the call of Terminate().
Also how should I handle the memory management in .NET? I am talking
of PDF documents with ~2 thousands pages. I tried to force GC.Collect
() but this is a performance penalty for me.
A: Terminate() method is currently a no-op in DotNET version and you
can safely omit it. Even in C++ or Java version terminate is not
required to be called. The main reason for Termonate is to clean up
some resources which would be mistakengly reported as memory ‘leaks’
in memory tracking applications (such as Purify or BoundsChecker etc).
Regarding initialize you should probably call it in your application/
library OnInitialize/Main method. In case you don’t have such method,
you could call Initialize() multiple times (PDFNet will make sure that
it is inialized only once), you only need to make sure that it is
called before any other PDFNet method.
How should I handle the memory management here? I am talking of PDF
documents with ~2 thousands pages. I tried to force GC.Collect() but
this is a performance penalty for me.
GC.Collect() is not necessary. You could simply call Dispose() (or
Close) on PDFDoc, PDFDraw, ElementBuilder, ElementWriter,
TextExtractor, and other larger objects. All memory will be instantly
released. You can also use IDispose pattern (on ‘using’ C# keyword) to
immediately release these resources.