The aim of this paper is to consider, by means of the numerical investigation, the geometric optimization of a cavity that intrudes into a solid with internal heat generation. The objective is to minimize the maximal dimensionless excess of temperature between the solid and the cavity. The cavity is rectangular, with fixed volume and variable aspect ratio. The cavity shape is optimized for two sets of boundary conditions: isothermal cavity and cavity cooled by convection heat transfer. The optimal cavity is the one that penetrates almost completely the conducting wall and proved to be practically independent of the boundary thermal conditions, for the external ratio of the solid wall smaller than 2. As for the convective cavity, it is worthy to know that for values of H/L greater than 2, the best shape is no longer the one that penetrates completely into the solid wall, but the one that presents the largest cavity aspect ratio H0/L0. Finally, when compared with the optimal cavity ratio calculated for the isothermal C-shaped square cavity, the cavities cooled by convection highlight almost the same optimal shape for values of the dimensionless group λ ≤ 0.01. Both cavities, isothermal and cooled by convection, also present similar optimal shapes for ϕ0 < 0.3 and ϕ0 > 0.7. However, in the range 0.3 ≤ ϕ0 ≤ 0.7, the ratio (H0/L0)opt calculated for the cavities cooled by convection is greater than the one presented by isothermal cavities. This difference is approximately 17% when λ = 0.1 and ϕ0 = 0.7, and 20% for λ = 1 and ϕ0 = 0.5.