This paper discusses the free energy of complex dislocation microstructures, which is a fundamental property of continuum plasticity. In the past, multiple models of the self energy of dislocations have been proposed in the literature that partially contradict each other. In order to gain insight into the relationship between dislocation microstructures and the free energy associated with them, instead of deriving a model based on theoretical or phenomenological arguments, here, these quantities are directly measured using large scale molecular dynamics simulations. Plasticity is induced using nanoindentation that creates an inhomogeneous distribution of dislocations as the result of dislocation nucleation and multiplication caused by the local deformation. Using this approach, the measurements of dislocation densities and free energies are ab-initio, because only the interatomic potential is defining the reaction of the system to the applied deformation. The simulation results support strongly a linear relation between the scalar dislocation density and the free energy, which can be related very well to the classical model of mechanical energy of straight dislocations, even for the complex dislocation networks considered here.