South African soldiers are to be deployed in Johannesburg to quell anti-immigrant violence that has killed at least seven people in several weeks of unrest.

Police have struggled to contain mobs who have been attacking foreigners from Zimbabwe, Malawi, Mozambique and other African countries in both the economic capital Johannesburg and in the port city of Durban.

Johannesburg's Alexandra township has seen much of the unrest, with a Mozambican national who was killed in the area on Saturday becoming the seventh victim.

The government had vowed to crack down strongly on the unrest, but the decision to put soldiers on the streets came after two nights of relative quiet in both cities.

"We come in as the last resort, the army will serve as a deterrent against the crime that we see," Defence Minister Nosiviwe Mapisa-Nqakula said.

Ms Mapisa-Nqakula declined to give details on how many troops would be involved.

"There are people who will be critical but those who are vulnerable will appreciate this decision," she said.

"Now we deploying because there is an emergency.

"We are not here to take over the work of the police. We are simply here to give support to what the police are trying to do in their efforts to prevent a continuation with what we have seen."

The spate of attacks has revived memories of xenophobic bloodshed in 2008, when 62 people were killed in Johannesburg's townships, shaking South Africa's post-apartheid image as a "rainbow nation" of different ethnic groups.