Some test text!

menu
Memory managementkeyboard_arrow_down

Memory Management in Node.js

The PDFTron SDK in Node.js automatically cleans up all objects in memory when a process is initialized using PDFNet.runWithCleanup() once the process has finished running. In almost all cases, it is recommended to use the PDFNet.runWithCleanup() function to avoid memory leaks and complicated memory management.

For most situations, using PDFNet.runWithCleanup() and [Obj].takeOwnership() is sufficient for managing memory. However, there are additional ways to manage your memory usage:

linkRetain objects

In some situations you may wish to retain an object after the process has finished. A common example of this would be to create a document and retain it after processing. The ElementEdit sample on the samples page has an example of how this can be done.

  • [Obj].takeOwnership()

linkDeallocate objects

Deallocates individual objects. Only objects that derive from PDFNet.Destroyable have this method.

  • [Obj].destroy()

linkDeallocate stack

Stack-based deallocation. Calling endDeallocateStack() will deallocate all objects that were created since the last call to PDFNet.startDeallocateStack(). In general, stack-based deallocation is recommended because it is easier to manage for larger sections of code.

  • PDFNet.startDeallocateStack()
  • PDFNet.endDeallocateStack()

linkSome examples

Here are some examples for the various deallocation options

linkAutomatic (default) deallocation

async function main() {
  try {
    // documents require deallocation
    const doc = await PDFNet.PDFDoc.create();
    // object that requires deallocation
    const page_iter = await doc.getPageIterator();
    // object that requires deallocation
    const writer = await PDFNet.ElementWriter.create();
  } catch (err){
    console.log(err);
  }
}

// deallocates all objects once main finishes running
PDFNet.runWithCleanup(main);

linkIndividual deallocation

async function main() {
  try {
    const doc = await PDFNet.PDFDoc.create();
    const page_iter = await doc.getPageIterator();
    const writer = await PDFNet.ElementWriter.create();
  } catch (err){
    console.log(err);
  } finally {
    await doc.destroy();
    await page_iter.destroy();
    await writer.destroy();
  }
}
PDFNet.runWithoutCleanup(main);

linkStack-based deallocation

async function main() {
  try {
    await PDFNet.startDeallocateStack();
    const doc = await PDFNet.PDFDoc.create();
    const page_iter = await doc.getPageIterator();
    const writer = await PDFNet.ElementWriter.create();
    await PDFNet.endDeallocateStack();
  } catch (err){
    console.log(err);
  }
}
PDFNet.runWithoutCleanup(main);

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

Join our upcoming webinar to learn about how to collaborate on videos frame by frame directly in your browser

Save your seat
close