In Los Angeles, it has been a different story: Under Ms. Lacey, Los Angeles, which accounts for almost a third of the state’s prison population of close to 130,000 inmates, has sent people to prison or jail at more than four times the rate of San Francisco. Yet in both cities, crime is at historic lows, with the fewest murders in nearly a half century last year.

California has passed several measures in recent years to reduce prison populations and change the criminal justice system.

Mr. Gascon joins two other candidates, both prosecutors, who are also challenging Ms. Lacey from the left: Richard Ceballos, 57, a former defense lawyer who now focuses on prosecuting organized crime cases; and Joseph Iniguez, 33, who was a schoolteacher before he became a prosecutor.

Voters will have their first say on March 3, when California holds its presidential primary. If one of the candidates receives more than 50 percent of the vote, then he or she will win. If not, the top two finishers will advance to a general election in November 2020.

“I have a tremendous amount of passion for what happens in L.A., and for the last few years I have become increasingly more uneasy seeing the backwardness of the criminal justice system in L.A.,” Mr. Gascon said in an interview last week, before he announced his candidacy.

Mr. Gascon, a Cuban émigré, moved to Los Angeles with his family as a boy. He attended Bell High School there but dropped out and entered a three-year stint in the Army, where he earned his high school degree.

Mr. Gascon was a beat cop in South Los Angeles in the 1980s and 1990s — he eventually rose to assistant chief of the department — a time of soaring crime rates, gang violence and the crack epidemic, and a time when Los Angeles and the nation were locking up more and more people, particularly black men.