In view of the impact and influence of grid-connected distributed energy on power system, this paper studies the capacity optimization of microgrid. First of all, this paper introduces non-cooperative game theory to build a capacity optimization model of microgrid based on non-cooperative game. Then, the whale optimization algorithm is used to configure the capacity of each game player. Finally, using the historical data of a certain region for analysis, the results show that compared with the single stakeholder optimization method, the photovoltaic power station and energy storage system revenue increased by 9% and 2.5%, respectively.