How can I guarantee that a Java object (containing cryptographic material) is nullified?

My concern is that the cryptographic keys and secrets managed by the garbage collector can be copied and moved to memory without resetting.

As a possible solution, is this enough?

public class Key {
  private char[] key;
  // ...
  protected void finalize() throws Throwable { 
    try {
      for(int k = 0; k < key.length; k++) {
        key[k] = '\0';
      }
    } catch (Exception e) {
      //...
    } finally {
      super.finalize();
    }
  }
  // ...
}

EDIT: Please note that my problem is not only resetting the official (reference) copy of the object, but also any outdated copies that the garbage collector could make when it moves memory around space and speed efficiency.

GC , "", . - , . , , ( "" ).

, , , JVM, .

+5
4

, , @jambjo @james, , .

, , C ( ) .

- , .

0

Java , . .

+4

allocateDirect NIO. , . , , ( , ) .

+1

, ? , , , , .

, , - JVM, ? JVM-, , .

, , , JVM: AFAIK .

+1

All Articles