David was no fool - as well as an effective general he was a wise and able political ruler. In ancient times as well as today, politics involves the unstated "social contract" between the government and the people - the people give loyalty and some powers/.freedoms to the government and the government protects the people. That is, the government of any people is only such while the people give it legitimacy.
Therefore, in 2 Sam 15:14,
And David said to all his servants that were with him at Jerusalem,
Arise, and let us flee; for we shall not else escape from Absalom:
make speed to depart, lest he overtake us suddenly, and bring evil on
us, and smite the city with the edge of the sword.
There are several reasons why David immediately decided to leave.
David instantly recognized that to stay and defend the city against a rebellious population led by his son and heir apparent immediately means that the king is fighting against the people that are supposed to give the king legitimacy! That is, if the king is fighting the people he is supposed to protect and govern, he is obviously not their king!
The ancient name of Jerusalem means, "place of peace". In the NT, one of the spellings is Ἱεροσόλυμα (Hierosoluma), meaning, priestly peace, or, peace of the priests; sometimes more loosely render "holy peace" or even more loosely, "holy city". David did not wish to desecrate his beloved city of peace with terrible bloodshed and endanger the fete of the sanctuary and and its staff.
3. Self Preservation
While David's army and personal body guard were supremely loyal (2 Sam 15:15 - The king’s servants replied, “Whatever our lord the king decides, we are your servants.”), and he could have ably defended the city from attack, the siege would eventually take it toll and most inside the city would eventually perish. Escaping the city would permit some time to plan the next phase. In any case, there would have been no significant supplies (in the city) for an extended siege.
David must have also realized that an extended civil war would impoverish the nation and the people. In the event, there was a brief battle that resolved the problem quickly.
5. Absalom's Inexperience and Vanity
Despite his love for his eldest handsome son, David was till aware of Absalom's limitations - he was extremely vane, completely inexperienced in government, politics and battle leadership. David must have known this and knew that with Absalom as king, he would not last long. This is evidenced by David deliberately sending Hushai the Arkite back to Jerusalem to "frustrate" the advice of Ahithophel (2 Sam 15:32-37).
Vindication of Decision to Vacate
All this turned out to be correct - it was Absalom's vanity and inexperience that actually was his undoing. He foolishly took the loaded (trapped) advice of Hushai the Arkite over the much wiser advice of Ahithophel. [Hushai's advice was designed to appeal to Absalom's vanity.] The results were catastrophic - "the forest devoured more people than the sword" (2 Sam 18:8) precisely because Absalom's over-bloated army was unwieldy, untrained and totally inexperienced; it lacked any credible leadership. Even Ahithophel had foreseen this and committed suicide earlier (2 Sam 17:23).
In the end, David's evasive action resulted in only 20,000 deaths (2 Sam 18:7). I am sure that if he had stayed and defended Jerusalem, the deaths would have been more than 10 times as great.
Lastly, the ultimate vindication of David's decision to vacate Jerusalem came when, a few days later, he was welcomed back as their beloved king by all the people.