This is a very well documented issue with the 3GS after the upgrade to OS4.
Apple claim it results from the multitasking and you must ensure you have very few apps running in the background. Double click the home btton and then hold down on the first App icon. You will get a minus sign ag
Eachicon will display a minus sign, touch each and the App will close.
This never truely resolved the problem for me although it improved it somwhat. If the phone is only 5 months old take it back to an Apple Store, it doesnot matter if you did not buy it from them. Thry have been known to change the phone.