In Java it is recommended to use char[]
for storing passwords or other sensitive information to be able to clear it manually once the data is no longer needed.
How can such an array be cleared across all threads? If I understand it correctly threads might only perform changes in their cache, but not in the shared memory, so the following would not work reliably:
char[] password = ...
...
Arrays.fill(password, '\0');
- Is this assumption correct or do the threads always write to the shared memory?
- Is it necessary to use
volatile
(or other synchronization) to make sure the shared memory is updated?- Is a happens-before relationship required for this because the compiler / JVM would otherwise omit memory synchronization due to optimization?
- Do other threads have to establish a happens-before relationship to clear the array content from their cache, or is this negligible? Possibly because the cache will be used for other more frequently accessed data and the array will be discarded, given that it is not actively used anymore.
Edit: The statement that char[]
should be used for passwords was based on Why is char[] preferred over String for passwords?, however after looking at it again, this is also a little bit controversial.