We calculate the defect formation energies and the concentrations of native and Mg-related defects in wurtzite and zincblende GaN by using a first-principles pseudopotential method. For both the wurtzite and the zincblende structures, vacancies are found to be more stable than other intrinsic defects, such as interstitials and antisites. Under the Ga-rich condition, the concentration of V-N(+) is estimated to be about 10(17) cm(-3) in the absence of other impurities, such as Si and O, giving rise to n-type conductivity. As a p-type dopant, Mg has the lowest formation energy when occupying a Ga-sublattice site, while the formation energy of substitutional Mg-N is much higher due to the large strain energy caused by the difference of the covalent radii between N and Mg. In this case, the N vacancies significantly compensate for the Mg accepters, especially, in a Ga-rich condition.