Is the potential energy stored in a spring proportional to the displacement or the square of it? Suppose a mass of $M$ kg is hanging from a spring in earth. The mass will stretch the spring about $x$ m. So the change in the gravitational potential energy is $mgx$ J (supposing $x$ to be very small compared to the radius of earth).
And this amount of energy will be stored in the spring as potential energy. So,
Change of gavitational energy = $mgx$ = potential energy stored in the spring
And it seems that the potential energy stored in a spring is proportional to displacement $x$. But the potential energy in a spring is $U=\frac{1}{2}kx^{2}$ and so it's proportional to $x^2$, the square of displacement. So surely I am wrong somewhere. But where am I wrong?
 A: Imagine that when you   attach the mass to the unstretched spring you are holding the mass  in you hand. You then gently lower the mass until the uplift $kx_{\rm max}$ of stretched spring just balances the dowward  weight $mg$ of the mass.  Gravity has done work to stretch the spring, but the net downward force at stretch $x$
$$
F(x)=mg - kx
$$ 
has also done work 
$$
\int_0^{x_{\rm max}} (mg-kx)dx = mgx_{\rm max}-\frac 12 kx_{\max}^2
$$
on your hand. Thus the difference in your two formulae
$$ 
mgx_{\rm max} - \frac 12 kx^2_{\rm max}
$$ is accounted for by the work done on you.  
If you just attatch and let go, the mass would bounce up and down and ther would also be kinetic energy to keep track of.
A: If there is just the (ideal) spring and a mass, i.e., if there is no dissipation, the total energy $E$ of the system is constant and is the sum of three terms:
$$E = mgh + \frac{1}{2}kz^2 + \frac{1}{2}m\dot{z}^2 = U_g + U_s + T$$
where $h$ is the height of the mass from the ground (zero reference of gravitational potential energy), and $z$ is the displacement of the mass from the zero spring potential energy position.
If the initial position $z_0$ is the zero of the spring potential energy $U_s(z_0) = 0$, and if the initial kinetic energy $T$ is zero, then as the mass falls downward under the influence of gravity, the gravitational potential energy $U_g$ decreases while $U_s$ and $T$ increase just so that $E$ remains constant.
If, on the other hand, the system is damped (add a dashpot), then eventually the mass will stop at the equilibrium position, and the magnitude of the difference in the change of the potential energies will equal the energy dissipated by the damping mechanism.
A: Note: The potential energy stored in a spring is proportional to the square of the displacement from equilibrium.
When you attach a mass to an unstretched spring, there will be a new equilibrium position for that mass on that now-stretched spring. About this new equilibrium position, you will have simple harmonic motion.
A: You are correct that
 $$
\text{change of gravitational energy} = mgx \ \ \ \ (= \text{potential energy stored in the spring}).
$$
No mistake there. That only gets you halfway there though. It is ALSO true that
$$
\text{potential energy stored in the spring} = \frac{1}{2}kx^2 \ \ \ \ (= \text{change of potential energy}).
$$
For both to be true, we must have
$$
mgx = \frac{1}{2}kx^2.
$$
Solving for $x$,
$$
x = 2\frac{mg}{k}
$$
Voilá.
I put the text on the right of those first two equations in parentheses, to emphasize that, while they are technically true, just to stop there is to argue in circles. You need both constraints to arrive at a unique solution.
Also, to be strict, the change in the gravitational energy (of the mass) is actually $-mgx$, not $+mgx$, and this is countered by the change in the potential energy of the spring. So really,
$$
\text{change of gravitational energy} = - (\text{potential energy stored in spring})
$$
but the end result remains the same (the two minus signs cancel).
A: The elementary work $dW$ done on the spring in a elementary displacement $dx$ is:
$dW = Fdx$. 
At this point the spring has already been stretched by $x$, so $F = kx$.
$dW = kxdx$. 
Integrating from $x=0$ (the unstretched position) until the final $x$:
$W = \frac{kx^2}{2}$
$\frac{kx^2}{2} = mgx$, the loss of potential gravitational energy equals the stored elastic energy.
