Foam injection is one efficient way to mitigate gravity segregation during CO2 injection into porous media. The effect of gravity segregation on foam propagation in heterogeneous porous media is not yet fully resolved. To assess CO2 foam transport for enhanced oil recovery (EOR) and for CO2 storage processes in heterogeneous reservoirs, an accurate prediction of foam behavior is essential. In this study, we investigate the effect of heterogeneity on gravity segregation in the presence of foam. For nonlinear analysis, we use an extension of an Operator-Based Linearization (OBL) approach proposed recently. The OBL approach helps to reduce the nonlinearity of complex physical problems by transforming the discretized nonlinear conservation equations into a quasi-linear form based on state-dependent physical operators. The state-dependent operators are approximated by discrete representation on a uniform mesh in parameter space. In our study, foam in porous media is described using an implicit-Texture (IT) foam model with two flow regimes. We first validate the numerical accuracy of the foam simulation with OBL by comparing segregation length using the IT foam model with Newtonian rheology to analytical solutions. Next, the foam-model parameters are fit to foam-quality scan data for four sandstone formations ranging in permeability by an order of magnitude using a least-squares optimization approach. We then construct several hypothetical models containing two communicating layers with different permeability and thickness ratios to examine foam s effect on gravity segregation. The numerical results of the segregation length in homogeneous domains show good agreement with analytical solutions, except in a transition zone beneath the override zone which is not included in the analytical model. Through fractional-flow theory, we find that the transition zone is not a numerical artefact, but caused by low gas relative-mobility during the transient displacement process. Permeability affects both the mobility reduction of wet foam in the low-quality regime and the limiting capillary pressure at which foam collapses. Thus the segregation length varies with permeability and foam strength. In two-layer models, the thickness of the top layer plays an important role in the ultimate segregation length. A thin top layer does not affect segregation in the bottom layer, while a thicker top layer dominates the segregation length, with less influence of the bottom layer.