aspose file tools*
The moose likes Tomcat and the fly likes PermGen error: Can't find a source of the leak Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "PermGen error: Can Watch "PermGen error: Can New topic
Author

PermGen error: Can't find a source of the leak

Viktor Grafskiy
Greenhorn

Joined: Jul 26, 2012
Posts: 7

Hi.
I'm struggling to find a leak in my application. After just three redeploys I always get PermGen Space error and I cannot understand why that happens. I see in a profiler that after undeploy my classes are still loaded and each next deploy adds more duplicates(please see the screenshot), but there are no GC roots from my classes. I've also tried to find GC roots in tomcat's classloaders, but unfortunately without success. On another forum people suggested that I do not close jpa connection properly, but I still have no clue where is the bug is hiding.
In my app the EntityManager is instantiated and closed by the ContextListener class:

I use the following libraries:
JSF 2.2
Primefaces 3.5
Eclipselink 2.3.2
Mysql-connector-java 5.1.26

Tomcat server version 7.0.42(also tested with 7.0.32)

I will greatly appriciate any help or tip, I've lost almost a week with this issue and almost gave up.

Thank you in advance.

Just in case here is a link on my heap dump http://yadi.sk/d/ByzcOFmM8Vysr



[Thumbnail for deploys.JPG]

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10113
    
165

Are you sure the contextDestroyed method is being called?

[My Blog] [JavaRanch Journal]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10113
    
165

The image you attached shows a EntityManagerBean. What is that class and how is it being used?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16070
    
  21

Unfortunately, that's normal behavior and getting 3 redeploys out of the JVM means you're doing better than I typically do.

I'm not totally clear on the details, but PermGen space is different from normal memory space, and ORM systems are especially noted for consuming a lot of it. Something in how static resources are allocated makes it essentially impossible for Tomcat to ever free much of the PermGen memory, and it's not having to do with applications failing to release resources.

I wish I could give better news, but about the best I can say is that at least Tomcat can restart rapidly.


Customer surveys are for companies who didn't pay proper attention to begin with.
Viktor Grafskiy
Greenhorn

Joined: Jul 26, 2012
Posts: 7

Jaikiran Pai wrote:Are you sure the contextDestroyed method is being called?
I've just double checked it in a debugger and i see that the method is exectuted.

Jaikiran Pai wrote:The image you attached shows a EntityManagerBean. What is that class and how is it being used?
It is used as a manager of blog recordsThe EntryManagerBean class is marked as servlet for instantiating connection to a database when the application is loading, its configuration is stored in web.xml


Tim Holloway wrote:Unfortunately, that's normal behavior and getting 3 redeploys out of the JVM means you're doing better than I typically do.

I'm not totally clear on the details, but PermGen space is different from normal memory space, and ORM systems are especially noted for consuming a lot of it. Something in how static resources are allocated makes it essentially impossible for Tomcat to ever free much of the PermGen memory, and it's not having to do with applications failing to release resources.

Not sure that it is normal Tim. Probably there are thousands of 3d party libraries in your projects and you basically don't have time to find all leaks and it is easier to restart Tomcat. But my project consists of few classes and i see there is a leak, and i'm afraid that it is my fault and i'm trying to understand where I made the mistake to avoid it the future.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16070
    
  21

If you are using JPA, I can virtually guarantee that PermGen space will be consumed and not returned. In the specific case of Hibernate, it's a known issue. There are other PermGen eaters out there as well, but ORMs are infamous for it.

Usually if you have sloppy memory management it's not going to be PermGen you run out of, it's going to be heap space. And undeploying will undo anything that didn't get allocated to an external classloader such as the one managing Tomcat's lib directory.

But, as I said, when you're using an ORM, the dice are loaded and no matter how meticulous you are, it's not going to redeploy very many times before all the PermGen memory is gone.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10113
    
165

Your load-on-startup servlet implementation doesn't look right in terms of resource management and lifecycle. Here's one thing you could try:



Notice the use of init() and destroy() method instead of using the constructor.
Viktor Grafskiy
Greenhorn

Joined: Jul 26, 2012
Posts: 7

Jaikiran Pai wrote:Your load-on-startup servlet implementation doesn't look right in terms of resource management and lifecycle. Here's one thing you could try:
Notice the use of init() and destroy() method instead of using the constructor.
Unfortunately that didn't help. I've tried to play with these references, and I see in the debugger that they are pointing to null when the app is closing, but something else continues to hold Tomcat's classloader maybe it's a working thread, not just a reference, but i'm still unable to find it.

Tim Holloway wrote:But, as I said, when you're using an ORM, the dice are loaded and no matter how meticulous you are, it's not going to redeploy very many times before all the PermGen memory is gone.
Now it seems to me that you are right Tim.

Thank you very much for your help gentlemen, i will try to use tomcat JNDI Datasource to open the DB connection. Will let you know if that helps.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16070
    
  21

JNDI won't help. I use JNDI, so I know.

Light at the end of the tunnel, though. Java 8 is beginning to shape up and PermGen goes away in Java 8.

Some interesting reading:

http://java.dzone.com/articles/java-8-permgen-metaspace

http://www.infoq.com/news/2013/03/java-8-permgen-metaspace
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: PermGen error: Can't find a source of the leak