# Reclaiming memory from Nodes

**URL:** <https://discourse.nodered.org/t/reclaiming-memory-from-nodes/49554>\
**Category:** Developing Nodes\
**Created:** [10 August 2021 04:29 UTC](https://discourse.nodered.org/t/reclaiming-memory-from-nodes/49554 "2021-08-10T04:29:25Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [10 August 2021 21:07 UTC](https://discourse.nodered.org/t/reclaiming-memory-from-nodes/49554/9 "2021-08-10T21:07:37Z")

</div>

> [@BShashwat](#):
>
> Also I don't think Javascript passes by **reference**. It passes by value, just that if any objects are getting passed as parameters they retain reference to values i.e. new copy will refer to old values

An interesting view. from that Q:

> It's always pass by value, but for objects the value of the variable is a reference

Most things in JS are objects and this explains it much better than I did but is saying much the same.

> The author means that by passing a reference, you're passing a reference value (another way to think of it is passing the value of the memory address

OK, so some semantics there and I kind of get it but I'm not convinced that it matters at least most of the time, the effect is pretty much the same.

This is the reason Node-RED works at all of course - the so-called msg is just an object variable and so is (mostly) passed by reference (reference value) from node-to-node. Only where you have 2 or more wires off an output do you get a copy. If this weren't the case, node-red would be a nightmare and would rapidly bloat itself into oblivion.

> [@BShashwat](#):
>
> Assigning **node = this** in every function might b a cause of memory leak as this can cause circular reference and not allow GC to collect and this was in almost all functions (copied it from Node-Red docs). So as an experiment I commented it and instead passed node as a parameter (this will get collected as soon as function goes out of scope) or we can manually make it null as it does not affect the original this object.

OK, now I'm a little confused. Perhaps you could share a bit more of your code where you doing this so that we can see the diff in the code?

In my nodes, I implement a function called `nodeInstance`

```js
// ... module-level setup ...
module.exports = function( RED ) {
    // ... module-level code ...
    function nodeInstance(config) {
        // ... instance-level code ...
        // - fn defined here so that it inherits `RED`. I don't know of another way to do this.

        // Create the node
        RED.nodes.createNode(this, config)
        ...
        // Take a reference to `this` in case the context changes
        const node = this
        ...
        // apply the properties from the config to the node (this)
        node.name = config.name || ''
        node.topic = config.topic || ''
        node.url = config.url || 'uibuilder'
        ...
        // define the handler function for incoming messages
        // - defined here so it inherits both `RED` and `node`, I don't know a better way to do that
        function nodeInputHandler(msg, send, done) { ... }
        // Register a handler for incoming messages
        node.on('input', nodeInputHandler)
        // Register the handler for node-red closing or the instance being (re)deployed
        node.on('close', function(removed, done) { ... }
    }

    // register the node
    RED.nodes.registerType(moduleName, nodeInstance, { ... })
    ...
    // ... Express API code ...
}
//EOF

```

You seem to be suggesting that `const node = this` has a real memory impact? I can't say I've seen this myself and I use the same approach in other places as well such as in the `oneditprepare` function in the node's html file.

I'm certainly not an expert JavaScript programmer by any stretch though so I may have this all wrong. I would certainly be interested if there is a better/cleaner way to do things.

---

_[View the full topic](https://discourse.nodered.org/t/reclaiming-memory-from-nodes/49554)._
