In the CodeSys manual we can read that:
"If you declare a local variable in a function block as RETAIN, CODESYS stores the complete instance of this function block in the Retain range (all data of the function block); however, only the declared RETAIN variable is treated as such."
But does someone actually tested? I created a function block with only the following variables:
VAR
Test1: ARRAY[1..50] OF UINT; //100 bytes
END_VAR
VAR RETAIN
Test2: ARRAY[1..50] OF DINT; //200 bytes
END_VAR
My program only implements one instance of this function block. Using SIZEOF
at runtime shows a function block size of 312 bytes
Now, if I right-click on the device, and go to "Device Memory Info", the size of my Retain Data is only 203 bytes.
If the complete instance of the function block is stored in the retain range, I would expect the retain data size to be the same as the function block size (312 bytes), but it isn't, it's only 203 bytes (size of the retain data). Is the manual incorrect?
I can say it is true. The first project where I needed retains inside a FB, I ran out of memory after having needed many, many instances (the FB were not “simple”). Once I removed the retain from the FB and linked to an external Retained variable, my problem went away. That is the day I learned the documentation warned me.
But I can say in that case I was making a solution that had around 100 unique and complicated FBs where each was storing a user entry. I have made several other projects that were much simpler and I just let the compiler put the whole FB is retain.