I encountered an interesting issue. I have a function block with 2 UDINT VAR set as retain.
VAR RETAIN
Times : UDINT; (*Accumulated times of Target fulfills Condition*)
TimeElapsed : UDINT; (*Accumulated TimeInterval*)
END_VAR
Theoretically, when I have one instance of this function block in my build, I should be using 8 bytes of remanent memory.
However, Automation Studio says that the task using one instance of this function block uses 96 bytes remanent memory which is way more than expected.
I’m suspecting this issue is due to the compiler making all VAR using remanent memory if one of the VARs uses remanent memory.
To verify my theory, I changed all VARs to retain and surprise surprise, 96 bytes used.
I wonder if there is a way to change this interesting memory allocation because in my test, I’m going to create a lot of instances of this function block and my test PLC is nowhere near enough.
I don’t want to make these tags standalone from the function block and muddy the program structure.
If at least one variable of a function block is marked with RETAIN, the instances of this function block are stored completely in the memory area for remanent variables. This means that the variables of the function block not marked with RETAIN are also contained in the memory area for remanent variables. These variables not marked with RETAIN are not retained, however, and therefore behave as specified in the declaration of the function block.
No it is not possible to adjust the allocation. However you could change your code to use external variables (outside of the FB) to keep the RETAIN data and feed them to the FB as needed. One option would be to use these as References on the FB side.
I though about this option. I think in test phase I will do this to allow myself run enough instances at the same time. The production code probably will have to waste some remanent memory to better manage the tags.
The answer is pretty clear and this case can be closed.