Assuming there is malware on your device which tries to scan main memory for your sensitive data, the answer unfortunately is no.
Keepass provides some protection against this threat. It admittedly tries its best to protect your passwords (but not your user names etc.) in memory:
While KeePass is running, sensitive data is stored encryptedly in the process memory. This means that even if you would dump the KeePass process memory to disk, you could not find any sensitive data. For performance reasons, the process memory protection only applies to sensitive data; sensitive data here includes for instance the master key and entry passwords, but not user names, notes and file attachments. Note that this has nothing to do with the encryption of database files; in database files, all data (including user names, etc.) is encrypted.
Furthermore, KeePass erases all security-critical memory (if possible) when it is not needed anymore, i.e. it overwrites these memory areas before releasing them.
For some operations, KeePass must make sensitive data available unencryptedly in the process memory. For example, in order to show a password in the standard list view control provided by Windows, KeePass must supply the cell content (the password) as unencrypted string (unless hiding using asterisks is enabled). Operations that result in unencrypted data in the process memory include, but are not limited to: displaying data (not asterisks) in standard controls, searching data, replacing placeholders (during auto-type, drag&drop, copying to clipboard, ...), importing/exporting files (except KDBX) and loading/saving unencrypted files. Windows and .NET may make copies of the data (in the process memory) that cannot be erased by KeePass.
A 2019 study linked on Reddit (using KeePass 2.40) has demonstrated how passwords can be left in memory unencrypted. This applies only to passwords you have interacted with in the open session, and does not apply to the master password. Apparently, there is no difference in security regarding this issue with locked or unlocked state. (Obviously, locked state still prevents people from using your Keepass file if you leave your device unlocked and unattended.) They conclude:
End users should, as always, employ security best practices to limit exposure to adversarial activity, such as:
(...)
- Shutting a password manager down completely when not in use even in a locked state (If using one that doesn’t properly sanitize secrets upon being placed into a locked running state)
However, there is only a threat if there is malware on your device. So, it could also be that the malware
- steals your sensitive data in the few seconds you have Keepass opened.
- is a keylogger. Though Keepass offers some protection against keyloggers, I am not sure whether it applies to the master password. If not, you could still use an additional master key file to mitigate this issue.
So the question remains open how much additional security the habit of always closing (not only locking!) Keepass as soon as possible really offers in practice.
As an idea for a tradeoff between security and convenience, you could use two Keepass files: One you close as soon as possible that you use for super-sensitive data like email logins, banking logins, crypto asset keys etc.; and another one you leave open for a long period of time that you use for less sensitive data like message board logins etc.
Disclaimer:
I am not a security expert, only a professional software developer.