Earlier, the tourists' own spinner Keshav Maharaj picked up career-best figures of 9-129 as Sri Lanka were bowled out for 338 - 10th-wicket pair Herath (35) and Dananjaya (43no) taking their final-wicket partnership to 74.
Sri Lanka surged to 151 for three in its second innings on Saturday - a 365-run overall lead - as it sought a whitewash triumph over South Africa in their two-Test series.
Amla went to lunch at 7 not out, batting alongside captain Faf du Plessis, who had faced nine balls for his single run.
The previous best bowling analysis by a South African since the country's return to test cricket in 1992 was Lance Klusener's eight for 64 against India in Calcutta in 1996. Dimuth Karuwaratne continued his excelled form with his fourth successive score of more than 50 to close the day on 59 not out (92 balls, 8 fours). South Africa were 19-3 at lunch in its first innings and lost the remaining seven wickets in the afternoon session.
However, Sri Lanka are on course for victory after bowling the tourists out for 124 in their first innings.
Sri Lanka didn't enforce the follow-on.
Sri Lanka's Dhananjaya de Silva (R) watches his shot next to South Africa's wicketkeeper Quinton de Kock.
South Africa's reply seemed especially meek after Dananjaya and Herath's valiant last-wicket stand.
Hashim Amla who became Perera's first victim falling to a bat-pad catch became the third South African batsman to cross the 9,000-run mark in Test cricket after Jacques Kallis and Graeme Smith.
Akila Dananjaya and Rangana Herath added 61 runs to the overnight score to frustrate South Africa.
Maharaj's eventual figures were not only the second-best figures for a South Africa spinner ever - just behind Hugh Tayfield's 9 for 113 against England in 1957 - they are also the best figures in Asia for a visiting bowler.