The crushed rock around railroad tracks is called ballast. It is part of the track structure, not leftover construction material.
Ballast helps support the rails and ties, distribute enormous train loads, drain water, resist movement, and make it possible to maintain the track’s alignment.
For a pile of rocks, it has a surprisingly long job description.
Trains put huge forces into the track
A train concentrates a great deal of weight onto relatively small areas where steel wheels meet steel rails.
The rails transfer that load into the cross ties. The ties then transfer it into the ballast below.
The ballast spreads the pressure over a wider area before it reaches the soil underneath.
Without that load distribution, the ground could deform more easily and the track could settle unevenly.
Uneven track is not something you want to discover at train speed.
Ballast helps hold the track in place
Rails expand, contract, flex, and experience sideways forces as trains move across them.
The angular crushed stones interlock with one another and grip the ties. That helps resist unwanted movement.
Rounded river stones would slide more easily. Railroad ballast is generally crushed to create rough, angular pieces that lock together while still leaving spaces for drainage.
Drainage is a major reason for the gaps
Water is one of the great enemies of roads, foundations, and railways.
The open spaces between ballast stones allow rainwater to drain away rather than collecting around the ties and softening the ground underneath.
Good drainage helps preserve the strength of the track bed and reduces problems caused by mud and frost.
Why not pour concrete under every railroad?
Some rail systems do use concrete slab track, especially in certain tunnels, high-speed corridors, and transit systems.
But ballasted track has major advantages. It can be comparatively economical, it can absorb and distribute loads, and crews can adjust the ballast to correct track position.
Special machines can lift the track and pack or “tamp” ballast underneath the ties to restore alignment.
A loose-looking rock bed is therefore surprisingly adjustable engineering.
Does ballast stop weeds?
It can help inhibit vegetation, although that is not its main job. A thick layer of coarse rock is not an easy place for plants to establish deep roots.
Railroads may still need vegetation management because nature has never shown much respect for infrastructure plans.
Why does the rock extend past the ends of the ties?
Ballast shoulders along the sides add lateral stability and help hold the track structure in place.
The shape and depth of the ballast bed are engineered, not random.
So when you see a railroad disappearing into the distance on a bed of gray stone, you are looking at part of the load-bearing system.
The train runs on steel rails.
But a lot of what keeps those rails where they belong is sitting underneath them looking like a gravel pile.
A related explanation is Why does metal feel colder than wood?, which looks at another familiar effect from the same general system.
And that’s the short explanation.
Related explanations: Why does metal feel colder than wood? and Why do you get a static shock from a doorknob?