Can macOS's ramdisk be swapped to disk by the operating system?
Since macOS High Sierra 10.13.x the virtual memory &quot;swapfile(s)&quot; have been encrypted even if FileVault2 disk encryption is off.  Obviously, I would test that theory and review any CVE reports and validate it.
https://support.apple.com/guide/mac-help/what-is-secure-virtual-memory-on-mac-mh11852/mac
You might be scratching your left ear with your right hand roundabout the hard way.  Consider creating an additional keychain and using that instead. It's far more secure and this is precisely what keychains are designed to accomplish.  They secure secrets.
Developer API Docs:
https://developer.apple.com/documentation/security
Command line (scripting):
man security