When this happens gasses dissolved in the magma are able to come out of solution, forming gas bubbles (the cavities) inside it. When the magma finally reaches the surface as lava and cools, the rock solidifies around the gas bubbles and traps them inside, preserving them as holes filled with gas called vesicles.