Finally i have solved my problem. Following things helped:
1. Heap Size 2048 is eough.
2. Another problem was that i was using String.
and String object is immutable
By immutable, we mean that the value stored in the String object cannot be changed. Then the next question that comes to our mind is “If String is immutable then how am I able to change the contents of the object whenever I wish to?” . Well, to be precise it’s not the same String object that reflects the changes you do. Internally a new String object is created to do the changes.
refer Difference between string and stringbuffer, Stringbuilder
So i removed getters and setters for entities other than JPA Entities. And inserted all data directly to Database without setting them to any objects.
3. the third and the Main problem was JPAEntityManager.
My code didn't ensure the EntityManager is always closed when the method finishes. As far as a RuntimeException occurs in the business logic, the em EntityManager remains open!
So Always close this and also you can set your objects to null in finally block like
finally {
Obj1 = null;
Obj2 = null;
if (entityTransaction.isActive())
entityTransaction.rollback();
em.clear();
em.close();
}
refer How to close a JPA EntityManger in web applications
+1 for every Answer guys it helped me a lot. i am not marking any answer because i thought of posting the complete answer for it.Thanx