The spring is first relaxed at its lower end h_1, that's when we attached mass m to itand let it fall freely and come to rest. Now by Force analysis we can show that in final position of mass the forces of gravity and restoring force from spring are balanced. i.e. F_g=F_s which yields
x=-mg/k...