Working with beans: Should I unset them after using them?

When googling, I found (more related to SugarCRM) that unset() on beans could/should be done. Is there any merit in doing so in case of SuiteCRM except for helping garbage collection a bit ?

Yes - there is merit in doing this for the the following reason:

Whenever an action is carried out, it is possible that additional modules may be run. This functionality is controlled by adding logic hooks:

If you were 100% positive that no other hooks would ever be executed after your code, and you were extremely lazy, and you weren’t really worried about what people thought of your code, then there is no reason to unset the beans.

However, if you’ve altered something and another module/function runs from the same logic hook, then chaos can ensue! Worst case I’ve seen is of a hooked module changing the date time format, and another module also wanted to use the date time, but required it in a particular expected format. End result - Over 36 million errors in the log file, and a full drive resulting in a server crash as the system repeatedly tried to carry out the tasks that it was instructed.

So - morale of the story is - Clean up after your code.

But there’s a previous question… Which unsets on which beans, when?

If you have a bean variable you create yourself, you can do what you want with it, and needn’t bother about what happens next. You could tidy up and unset, but given the ephemerous nature of a PHP request, I doubt that is worth the effort, and I wonder if it could even be counter-productive, by triggering garbage-collection on a memory heap that is just about to be wiped clean anyway.

I think it’s worth unsetting when you are in long-lived operations, or in loops where memory consumption is an issue, or when you expect one of those situations to arise after what you’re doing, but still inside the same web-server request.

1 Like