Thermodynamic Principle and Engineering of Resistive Memory