If a product is added in the cart, it automatically gets empty after 2-3 hours. why does that happens? thanks
Logged in customers get their cart quote saved and the quotes are retrievable because the they are indexed to a user ID independent of session.
Guest customer and not-logged-in registered customer carts only last as long as the session cookie hasn't expired. Once expired, there is no session to match the guest quote which causes it to be unfindable even though it still exists.
You'll probably find if you actually test it, that a guest or nli cart only lasts about 20 minutes.
It's why newer versions of Magento have the Persistent Carts feature.
You can play with
System > Configuration > Web > Session and Cookie Management to see if you can change it, but you probably will find that you need to actually read up on php
session.gc_maxlifetime and its various siblings to get it to stick.