Q:

How can I reduce the memory used for perm gen in web-app

I use tomcat7. My web-app contains:

hibernate
log4j
jdom
upload jar
mysql driver
...

the total of lib is 30 jar files. My hoster says: "A lot of memory is allocated to the your program(for Perm gen and heap). Please reduce the amount of memory consumed". 
How do I reduce the memory requirements of my program?
How can the reduce the perm gen memory?
please help me.

A:

You should probably attach a memory profiler first and see what type of memory is being used and why. The answers to this question list a bunch of profilers you could try.
Heap is all the memory you allocate for storing objects in Java. Every time you use "new" you allocate on the heap and the memory will become garbage collected some time after the last reference to it is cleared. Here are some typical things you might do to reduce heap usage: Switch from an in-memory parsing API to a streaming one, perhaps by using StAX or SAX instead of JDOM. Fix memory leaks or simply longer-than-necessary object retention by cleaning up in a finally block as soon as the object is no longer needed. Optimize SQL queries to make them return only exactly the necessary data to the app, perhaps by adding a max row constraint or a more restrictive where clause. Limit the size of uploads and stream them to temp files on disk instead of keeping them in memory.
Perm gen is "permanent generation". Generally, memory in the perm gen is never freed until the JVM exits. It is mostly used to store loaded classes. So anything you can do to load fewer classes helps here. Here are some ways to load fewer classes: Try not to use multiple versions of the same jars. Try to load jars in the widest context possible, preferring Tomcat's lib directory or a shared lib directory over WEB-INF/lib. Minimize reloading of the webapp because every time you reload a webapp in Tomcat, it doesn't unload the old versions of all classes it simply loads the new ones.

