if this error occured after adding sme piece of code to an already running application, then your new code have created a lot of objects and filled the heap size and lead to outof memory err.\
1. Any recursive calls which create a lot of objects 2. Check for the scope of the objects, may be you were trying to add objects to a static container object. Hence objects were not eligible for Garbage collection. 3. Any socket related operations that lead to consuming a lot of resources.
In simple, your code is stopping GC from doing its work !!(by keeping live references for unnecessary objects)