> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/core/learn-more/platform-specifics/memory-management.md).

# Memory Management in Node.js

Learn how to efficiently manage memory in Node.js using the Apryse SDK with PDFNet.runWithCleanup(). Avoid memory leaks and simplify memory management for your applications. The Apryse Server SDK stre

The Apryse 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:

## Retain 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](/core/get-started/samples/elementedittest.md) on the [samples page](/core/get-started/samples.md) has an example of how this can be done.

* `[Obj].takeOwnership()`

## Deallocate objects

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

* `[Obj].destroy()`

## Deallocate 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()`

## Some examples

Here are some examples for the various deallocation options

### Automatic (default) deallocation

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
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);
```

{% endcode %}
{% endtab %}
{% endtabs %}

[PDFDoc.create](https://sdk.apryse.com/api/pdfnet-node/PDFNet.PDFDoc.html#.create__anchor) [PDFDoc.getPageIterator](https://sdk.apryse.com/api/pdfnet-node/PDFNet.PDFDoc.html#getPageIterator__anchor) [ElementWriter.create](https://sdk.apryse.com/api/pdfnet-node/PDFNet.ElementWriter.html#.create__anchor)

### Individual deallocation

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
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);
```

{% endcode %}
{% endtab %}
{% endtabs %}

[PDFDoc.create](https://sdk.apryse.com/api/pdfnet-node/PDFNet.PDFDoc.html#.create__anchor) [PDFDoc.getPageIterator](https://sdk.apryse.com/api/pdfnet-node/PDFNet.PDFDoc.html#getPageIterator__anchor) [PDFDoc.destroy](https://sdk.apryse.com/api/pdfnet-node/PDFNet.PDFDoc.html#destroy__anchor) [ElementWriter.create](https://sdk.apryse.com/api/pdfnet-node/PDFNet.ElementWriter.html#.create__anchor) [ElementWriter.destroy](https://sdk.apryse.com/api/pdfnet-node/PDFNet.ElementWriter.html#destroy__anchor) [Iterator.destroy](https://sdk.apryse.com/api/pdfnet-node/PDFNet.Iterator.html#destroy__anchor) [PDFNet.runWithoutCleanup](https://sdk.apryse.com/api/pdfnet-node/PDFNet.html#.runWithoutCleanup__anchor)

### Stack-based deallocation

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
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);
```

{% endcode %}
{% endtab %}
{% endtabs %}

[PDFNet.startDeallocateStack](https://sdk.apryse.com/api/pdfnet-node/PDFNet.html#.startDeallocateStack__anchor) [PDFNet.endDeallocateStack](https://sdk.apryse.com/api/pdfnet-node/PDFNet.html#.endDeallocateStack__anchor) [PDFDoc.create](https://sdk.apryse.com/api/pdfnet-node/PDFNet.PDFDoc.html#.create__anchor) [PDFDoc.getPageIterator](https://sdk.apryse.com/api/pdfnet-node/PDFNet.PDFDoc.html#getPageIterator__anchor) [ElementWriter.create](https://sdk.apryse.com/api/pdfnet-node/PDFNet.ElementWriter.html#.create__anchor) [PDFNet.runWithoutCleanup](https://sdk.apryse.com/api/pdfnet-node/PDFNet.html#.runWithoutCleanup__anchor)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.apryse.com/core/learn-more/platform-specifics/memory-management.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
