Secondary ion mass spectroscopy (SIMS) and capacitance-voltage measurements were combined to thoroughly study Mg doping in GaN layers grown by metal organic vapor phase epitaxy. First we found that the Mg steady-state incorporation regime occurs for a surface coverage of 0.3 monolayer. Additionally SIMS indicates that H incorporates proportionally with Mg until a certain [Mg] where [H] saturates. After thermal activation, [H] while being much lower still scales with [Mg]. These results suggest that H combines with Mg to form two different types of Mg-H complexes: a metastable one leading to the Mg acceptor after annealing, the other one (dominating at high [Mg]) being stable and electrically inactive. The obtained results allowed us optimizing doping conditions for blue laser diodes. (C) 2011 American Institute of Physics. [doi: 10.1063/1.3593964]