Use a language that provides appropriate memory abstractions.
Use OS-level preventative functionality integrated after the fact. Not a complete solution.