Global variables, wich problems?

I know the difference between global, flow and context variable and I don't use the global variable because some time ago I had read that to reduce memory usage (Pi4), among other things, it was advisable not use global variables.
I don't find anymore the post on web and so I'd like to know if there are some problem to use global variables (I should use at least 20).

(yes, my English is terrible but I do my best)

EDIT:
Found!

https://www.ness.com/understand-how-to-reduce-memory-usage-of-promises-in-node-js/

Hi @Giamma - Your english is better than mine, and I'm British!

global, flow and context variables are nothing more than the scope that they apply.

They will occupy the same amount of resource, and unless your storing file blobs in variables, I really wouldn't worry.

if storing variables at the global scope works for you, then carry on.

I am not a pro at the Node JS runtime, but if there is a difference, I'm sure it's negligible, and in most cases, won't get noticed.

As I said, I am not a pro - so others may correct me, if there is a measurable difference.

1 Like

Found!

https://www.ness.com/understand-how-to-reduce-memory-usage-of-promises-in-node-js/

Avoid global variables: Do not use global variables. 
They don’t get collected and consume a lot of memory. 
Instead, use local variables or pass parameters

I think you may be assuming that flow, & context variables act differently to global variable in Node RED - at the process level they do not, it only denotes what can access them.

flow, context & global variables will all live in memory in the same way.

Why do you assume a global is not collected?
(remember a global variable is a Node RED concept - not a process concept)

if that is the thought pattern, then a flow & context variable also does not get collected.

any variable can be removed (regardless of scope) by setting it to undefined

global.set("someVar",undefined)

All variables will remain, until Node RED is terminated or you set the var to undefined

EDIT
Once again - I am not a pro here, but if global storage is different at the process level to a flow or context storage, then I stand to be corrected.

Certainly a global context variable, once set, will be retained indefinitely.

A flow context variable seems to behave similarly, though I'm not sure how to demonstrate that.

Edit: nonsense!
A context variable local to a single function node can presumably be garbage collected once the function is finished and created anew when the function next starts.
I don't know if the memory actually does get freed.

It's generally good coding practise to limit the scope of variables, both for clarity and memory efficiency.
Similarly in a function it is better to declare a "variable" as const where possible and to use let in preference to var.

I had to test this...
The variable remains, as long as the function node is still deployed, so will remain alive during each execution of the function node

Yes, of course you are right, it is retained!

context.set/get is there specifically to retain values from message to message.

Ah!

context vs THE context :sweat_smile:

Or until the value is changed, in which case the memory containing the original value will be freed.

@Giamma, what are you thinking of storing in context that will consume a lot of memory?

@Colin
No, nothing that will consume a lot of memory,
but I have a lot of variables and I pass the values between many flow through node link-out - link-in and I wanted to use global var to avoid many node link-out - link-in but that article don't recommends it.
Do you know another way to pass values between flow?

Personally, I think you are reading into that article way too much - it's talking about javascript in general, not how Node RED offers variables across context.

You will have to store many many MB's of vars before having to worry about any performance impact (if at all)

  • if you need to access the same values across flows

    • use global vars
  • if you need to access the same values within the same flow only

    • use flow vars
  • If the vars never change

    • Use environment vars

OR if you are for some reason worried,
use a single context var inside a function, and access it via a link-call node

:man_shrugging:

1 Like

Ok,
I'll add some global variables and I will check the memory usage ...
Thank you .....

Link nodes. It is best to pass data via messages when you can and only use context where there is a real advantage. It is very easy to get race conditions using global and flow variables.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.