My iPhone often restarts automatically. It happens when I charge for long time and sometimes, it happens when I'm talking on phone .
The best thing to do is to first try a backup and restore as new as detailed in
A lot of the time a device restarting multiple times can be caused by a software issue, and frankly getting all your data on the computer is always a a good idea.
This will also make sure you are updated to the most recent version in case you are working with old software. If it doesn't work it is probably time to get a new one or go for a repair.
Since it's a 3G, it could be that your phone is just old and the battery has gone bad.
If your device is jailbroken, that could also be causing the problem.
I suggesting checking with Apple to diagnose the problem. Then, decide if its worth fixing yourself or upgrading/replacing your iPhone.
Get an application like AppSwitch which can show you the Console logs. There are several on the App store and some might even be free.
Look for the last messages just before it rebooted, they may give you a clue as to what is happening. Look for a pattern of behavior that precedes the reboot.
If they always mention the same App, then uninstall that App for a while.
If the messages mention hardware related matters then it you had better take it to the Apple store to get it fixed.
Also before spending a lot of time debugging, make sure that you are using the latest version of iOS. The problem you are encountering may have already been solved and support people are more likely to want to help if it is occurring with the latest version.