The material flow balance in extrusion die cavity (e.g., porthole, welding chamber, etc.) plays a crucial role in the extrusion process and product quality of hollow aluminum alloy profiles. Therefore, the structures of porthole and welding chamber are always the keys for designing extrusion dies. Based on non-orthogonal structured Euler grids, an automatic optimization program for porthole structure was developed, where the consistency ratio of mass flux in each porthole to corresponding area of filled part of the profile was used as the criterion for evaluating the material flow balance. With the developed codes, the extrusion process of an aluminum square tube with multihole porthole die has been simulated and finally the porthole structures were optimized automatically. Compared with the initial scheme, the material flow balance in the optimized die cavity was greatly improved, and the velocity difference and the standard deviation of the velocity in the cross-section of the profile reduced by 55 and 69 %, respectively. Comparison of the results obtained from the developed codes and those by the commercial software HyperXtrude showed a very good agreement, which verified the optimization algorithm developed in this paper.