When exposed to temperature variations, the rail tends to vary its length. If this tendency would be freely allowed, for a temperature variation Δt° the rail length L will vary by ΔL.
This length variation is ΔL = αLΔt°.
If, on the other hand, the expansion is not permitted in any way, and both ends of the rail are fixed, the thermal variation Δt° will generate in the rail a compression axial (normal) internal force, N.
This axial internal force is N = αEA Δt°.
One way to reduce the influence of this temperature variation is to reduce Δt° itself, consequently reducing the caloric energy absorbed by the rail. A simple and effective way to do this is to paint the rails white. In this way a significant percent of the infrared light is reflected and the temperature of the painted rail is lower of the not-painted one.
An AREMA research on this subject showed that, depending on the coating used, the rail temperature reduction can be up to 10°C:
The method is efficiently used in UK and around the world. Network Rail is mentioning it in its video about buckled rail.
Here are a few pictures I took on the Italian lines where, at least in the Milan area, all the main lines are continuously painted white: