From the magneto optics images (MOI), the dendritic magnetic avalanche is known to appear dominantly for thin films of the newly discovered MgB$_2$. To clarify the origin of this phenomenon, we studied in detail the MOI of carbon-free MgB$_2$ thin films with and without a deposited gold layer. The MOI indicated carbon contamination was not the main source of the avalanche. The MOI clearly showed that the deposition of metallic gold deposition on top of a MgB$_2$ thin film improved its thermal stability and suppressed the sudden appearance of the dendritic flux avalanche. This is consistent with the previous observation of flux noise in the magnetization.