We examine the role of the gravitational instability in an isothermal , self-gravitating layer threaded by magnetic fields on the formation of filaments and dense cores . Using numerical simulation we follow the non-linear evolution of a perturbed equilibrium layer . The linear evolution of such a layer is described in the analytic work of Nagai et al . ( 50 ) . We find that filaments and dense cores form simultaneously . Depending on the initial magnetic field , the resulting filaments form either a spiderweb-like network ( for weak magnetic fields ) or a network of parallel filaments aligned perpendicular to the magnetic field lines ( for strong magnetic fields ) . Although the filaments are radially collapsing , the density profile of their central region ( up to the thermal scale height ) can be approximated by a hydrodynamical equilibrium density structure . Thus , the magnetic field does not play a significant role in setting the density distribution of the filaments . The density distribution outside of the central region deviates from the equilibrium . The radial column density distribution is then flatter than the expected power law of r ^ { -4 } and similar to filament profiles observed with Herschel . Our results does not explain the near constant filament width of \sim 0.1 pc . However , our model does not include turbulent motions . It is expected that accretion-driven amplification of these turbulent motions provides additional support within the filaments against gravitational collapse . Finally , we interpret the filamentary network of the massive star forming complex G14.225-0.506 in terms of the gravitational instability model and find that the properties of the complex are consistent with being formed out of an unstable layer threaded by a strong , parallel magnetic field .