Apache Tomcat's new memory leak prevention and detection

posted by mthomas on April 6, 2010 10:24 AM

Tomcat has long been blamed for causing memory leaks in the permanent generation (PermGen) that lead to OutOfMemoryErrors when re-loading web applications. I was discussing this at the eJUG days conference last year when I made an off-the-cuff remark that these errors were nearly always application bugs rather then Tomcat bugs. This generated a fair amount of debate that ended up with me doing some impromtu debugging of a couple of web applications provided by the audience.

The result of the debugging was that we found a number of root causes for the memory leaks that were fairly evenly split between application code, library code and JVM code. As I was coding some fixes for these issues, I realised that Tomcat could prevent a number of these memory leaks and detect even more. Not only that, in a number of cases it looked to be possible for Tomcat to actually fix the memory leak when it detected it. This lead, ultimately, to the new memory leak prevention and detection in Tomcat 6.

There are two parts to the new memory leak features. The first part handles memory leak prevention and is implemented as a life-cycle listener, the JreMemoryLeakPreventionListener. This listener prevents memory leaks by calling various parts of the Java API that are known to retain a reference to the current context class loader. If a web application is the first code to call one of these standard Java APIs, the web application class loader will be pinned in memory which will cause a memory leak. By making sure that Tomcat code makes the calls first, the memory leaks are prevented. If you want to know the gritty details of what this listener actually does, the source code is pretty well commented.

The second part of the new features handles detection. This code executes when a web application is stopped, un-deployed or reloaded. It checks, via a combination of standard API calls and some reflection tricks, for known causes of memory leaks and - where it can - fixes them. These checks are implemented in the WebappClassLoader. A nice explanation of what these checks do has been written by Sylvain Laurent on the Tomcat Wiki. As always with Tomcat, if you want the details you can look at the source code. Start with the clearReferences() method.

The memory leak detection and prevention code is still a work in progress. More detections and preventions will be added as they are identified. If you are aware of a category of memory leaks that aren't currently detected or protected against, I encourage you to bring this to the attention of the Tomcat developers via the Tomcat developer mailing list.

Mark Thomas is a Senior Software Engineer for the SpringSource Division of VMware, Inc. (NYSE: VMW). Mark has been using and developing Tomcat for over six years. He first got involved in the development of Tomcat when he needed better control over the SSL configuration than was available at the time. After fixing that first bug, he started working his way through the remaining Tomcat bugs and is still going. Along the way Mark has become a Tomcat committer and PMC member, volunteered to be the Tomcat 4 & 7 release manager, created the Tomcat security pages, become a member of the ASF and joined the Apache Security Committee. He also helps maintain the ASF's Bugzilla instances. Mark has a MEng in Electronic and Electrical Engineering from the University of Birmingham, United Kingdom.


What about a lot (10 000+) of jsp pages without reloading?

Hi Mark,

I read your article with a lot of interest and I have two questions:

First of all, I suppose you mean prevention and detection in Tomcat 7 instead of 6 ?

Secondly, I recently contacted Spring TC support about this (see my to them mail below). They told me Tomcat 7 would be better able to cope with such an amount of data. Does this new enhancements fixes our problem?

=== start mail to Spring TC Support ===
The project/website we implemented is mostly half-static data: textual content managed through a CMS by non-technical users. The content changes, but very rarely, except for some important pages like the homepage. The servers are expected to be under normal load.

The CMS publishes its contents into concrete files, which are then served by the front-end, in this case Spring TC server. The idea is that this reduces the database load, because content is only re-published when it is changed, instead of queried on every single page access.

However, since there is some dynamic data in each page (an overview of the latest news, for example), the published pages are in turn dynamic.
The choice for the technology for these front-end dynamic pages fell on JSP because it is known to scale well.

But, while it looks like JSP scale well under load, it appears not to scale well under the number of JSP pages. With JSP pages published based on templates into many separate files, each one of these files was compiled into a separate class, leading to a very, very large number of classes. (Under Tomcat running on a Sun JVM, not only does every JSP become a class, but there are tag support classes and method accessors, too.)

Not at all surprisingly, the system eats up the permanent generation heap for breakfast. Not only does the class code get loaded into PermGen, but if I'm not mistaken, all string literals in the code end up there, too. With the content in the JSP page and the JSP page compiled into a class, this means we have the entire web page in PermGen.

This problem is pretty much inherent in the system. Because Java doesn't unload classes (at least not that I've noticed or heard of) unless the class loader is destroyed (e.g. on webapp reload), every class ever loaded stays in memory forever. Since the JSP specification requires that JSP pages are turned into classes, every JSP page accessed stays in memory forever. Eventually, all JSP pages will have to find space in the server's main memory.

Combine this with a number and size of JSP pages that grows linearly with the amount of content in the site, and you'll soon hit the limits of the hardware (or even software, on 32-bit systems).

large number of jsps and permgen

Sounds familiar. Does it have anything to do with a certain OpenCMS based solution for newspapers?

We inherited maintenance of the newspaper portal that did the same thing and the quick fix was: restart app server when probe hits the mark (you can use JMX to detect the moment when you should restart JVM).

The solution is rather simple: do NOT generate static content into JSP, instead, use templating framework like freemarker.

It has nothing to do with Tomcat, it was simply lack of knowledge of the developers and architects of the original portal/CMS. Unfortunately, nobody was willing to pay for the rewrite and we decided to keep the status quo.

Two years later we lost the contract due to some other "political" reasons (the newspaper bought a company which developed it's own CMS and they decided to switch to the new system).

JreMemoryLeakPreventionListener: how-to

Hi all,
i've read code of JreMemoryLeakPreventionListener. If i would to use that what steps must i follow ? i suppose to create a jar with only that listener (named, for example _patch.jar) and put it in common library folder (apache-tomcat-6.0.18\lib), right ? Then ?
Thanks ...

memory leak

Nice article,

Did you get a chance to try the tool from appdynamics, it is super cool and easy to use, gives all info for memory leaks


That’s really appreciable! I think it will be a great achievement in the history of Tomcat and thus they can increase the number of their users. So I am now happy knowing this tremendous alteration of Tomcat. anti snoring devices

All are considering your

All are considering your essay writing dreams yourself skills really fantastic write up, if you think about it your hope is really awesome. Whatever it may be just I am suggested to my friends it is best site.

Really impressive post. I

Really impressive post. I read it whole and going to share it with my social circules. I enjoyed your article and planning to rewrite it on my own blog.
mahjong | agario | minecraft | pacman

mahjong minecraft agario pacman


real racing 3 hack This is helpful, nonetheless it can be crucial so that you can check out the following website: clash of clans triche


My personal look at of the weblogs appears to speak out loud along with a lot of of the visitors I'm really amazed. I've discovered personally looking for this time around. I'd usually do that personally however the purchase is actually fairly large as well as my personal authors tend to be not available right now. Any kind of recommendations? upload images to instagram

download game

Game Network was a European online game and television network company. It was initially owned by Digital Bros. group, but was later sold to Cellcast Group. Game Network was, during its broadcast, the only television channel in Europe dedicated to covering multimedia entertainment, and generally focused on video games.

Play GTA Vice City Online
Play GTA San Andreas Online
Jugar Vice City
Jugar San Andreas
pes 2007 download
pes 2010 pc
Telecharger Minecraft Gratuit
Telecharger Subway Surfers Gratuit
Telecharger FIFA 12 PC
Telecharger IDM Gratuit
Comment Posted on November

thanks you

I found a lot of information here to create this actually best for all newbie here. Thank you for this information.
i like play games happy wheels online and play happy wheels 2 games

fancy pants 3

Thanks for your sharing, have made me very happy. Spare time you can also participate in our game to feel and experience them. Thank you!
fancy pants 3
fancy pants 3
fancy pants 3
fancy pants
fancy pants 4
fancy pants 2
fancy pants adventure 3
fancy pants adventure


I have read your article several times. It really made me feel important, I think people would not ignore it. Thanks for sharing it. descargar facebook , descargar facebook , baixar whatsapp , descargar whatsapp , baixar whatsapp plus , baixar mobogenie gratis , mobogenie


This article is very interesting. Thank you for sharing it with everyone. I think that your blog will be much loved and they will visit it often. baixar facebook , baixar facebook , descargar facebook , descargar whatsapp gratis , descargar mobogenie gratis , baixar mobogenie gratis , messenger whatsapp

Nice post

Posts shared useful information and meaningful life, I'm glad to be reading this article and hope to soon learn the next article. thank you
Geometry Dash 2.0 |Dr Driving | Trollface Quest 2 | Square Quick

Geometry Dash 2.0


Thank you very much. I'm really impressed with this article, it is very professional. I think you have to learn about this issue is very carefully and have good knowledge. baixar facebook , whatsapp gratis , jogos de futebol , baixar facebook , baixar facebook

return man 3

Thanks for your sharing. We feel very happy about that. You should also try my best to play our FREE today to get the sense of fun that brings. Thank you!
return man 3
return man 2
return man

return man 3
return man 2
return man
return man 3 unblocked
return man 3 hacked
return man 3 zombies
return man 3 the season
return man 5
return man 6

Jual Laptop Notebook PC HP Lenovo Printer Tinta Toner Epson Mura

Their work you will is amazingly readily shown ınside your blog articles; you may be an actual competent. I would prefer to supply an extra blog with the preferred varieties one shown yesterday morning. Require ones own people to look into online world.for a lot of stuff tailor-made article. Jual tinta Printer

thanks you

This is a great article. It gave me a lot of useful information. thank you very much.
Download retrica online includes more than eighty different filters with many different styles and include retrica indir , and zombie tsunami is the ideal game for anyone who loves the running game genre

Very nice article. I

Very nice article. I definitely love this website. Thanks!
Mortgage Broker Calgary

Thank you for sharing valuable information

The share your really gives us excitement. Thanks for your sharing. If you feel tired at work or study try to participate in our games to bring the most exciting feeling. Thank you!
Red Ball is a online, platform games series, posted by King. It involves a red ball 4 stuck in a world of various levels, and hola launcher is a freemium web and mobile application which claims to provide a faster, private and more secure Internet . Whether you are looking to improve your culinary skills or you simply love whipping up tasty treats, this simulation game named cooking fever will surely leave your mouth watering. Sun, sand, surf, and all the fish you can eat! Sail away to paradise bay, where you'll help Keani and friends restore the island to its former glory. The and, agario is a fun addicting MMO game in which you have to eat or be eaten while you strive to dominate the World of colorful cells. Good happy !

[url=]hola launcher[/url]

thanks you

Thanks for providing us this great knowledge, i think i need this. Keep it up.
happy wheels , agario , happywheels , agar ,

Thank you for sharing valuable information

Thank you for such a sweet tutorial - all this time later, I've found it and love the end result. I appreciate the time you spent sharing your skills.
messenger indir | messenger download |messenger indir | messenger | messenger indir | download messenger | messenger indir | messenger indir

[url=]messenger indir[/url]

I'd like to thank you for the

I'd like to thank you for the efforts you have put in writing this website. I am hoping to see the same high-grade blog posts by you in the future as well. In fact, your creative writing abilities has encouraged me to get my own, personal website now ;)
Edmonton Mortgage Broker
Life Insurance Vancouver
Life Insurance Calgary

Very interesting! Thanks you

Very helpful advice in this particular post! It’s the little changes that make the largest changes. Thanks for sharing!
swords and souls 2 | subway surfers download | goodgame big farm | ninjago games | agario game

Our watch of your respective

Our watch of your respective websites usually speak out loud using countless of your respective audience We are truly satisfied. I have discovered me wanting on this occasion. I'd personally commonly make this happen me though the get can be very enormous along with our freelance writers are generally out of stock currently International Sms Providers.

But sometimes due to monitory

But sometimes due to monitory sources one has to discontinue his or her education but now you can freely get education as much as you want, you just have to get a financial loan and study to your fill click this.

This is such an incredible asset

This is such an incredible asset, to the point that you are giving and you give it away for nothing. I cherish seeing blog that comprehend the benefit of giving a quality asset to free.
Giochi Angry Birds - rio angry birds - truck giochi - subway surfers giochi - baby hazel giochi - sonic giochi - mario giochi - looney tunes dash giochi - minecraft giochi - subway surfers giochi - super smash flash 2 giochi

Thank you for another

Thank you for another fantastic posting. Where else could anyone get that kind of information in such a perfect way of writing? I have a speech next week, and I was looking for more info.

What a good blog you have

What a good blog you have here. Please update it more often. This topics is my interest. Thank you.

Great! Nice! thank so much! i like this!

I found lots of interesting information here.Great work. The post was professionally written and I feel like the author has extensive knowledge in this subject. Nice post. Swords And Souls, swords and souls 2, Potty Racers 3

Happy Wheels, happy wheels, Happy Wheels 4, Happy Wheels

dipecah sebesar kepalan tangan, dipilih yang mengandung unsur logam, dicuci dengan Daftar Harga Besi Wf Dijual Dari Pabrik Lewat Supplier Info Harga Welded Beam T beam King Queen Cross Castellated Beam

konstruksi baja palembang konstruksi bangunan pantai
air unruk mengeluarkan kotoran dan terakhir dikeringkan dengan cara dipanggang untuk mengeluarkan uap yang mengandung air.air unruk mengeluarkan kotoran dan terakhir dikeringkan dengan cara dipanggang untuk mengeluarkan uap yang mengandung air.air unruk mengeluarkan kotoran dan terakhir dikeringkan dengan cara dipanggang untuk mengeluarkan uap yang mengandung air.air unruk mengeluarkan kotoran dan terakhir dikeringkan dengan cara dipanggang untuk mengeluarkan uap yang mengandung air.


I get back to the program by the love of people. That is quite a surprise and I feel happy. But I have not had much time to prepare the repertoire, dance and costumes as you for the finals. But I tried my best and try to finish the contest with a fierce determination to live up to the expectations of people.
baixar b612 l baixar retrica l baixar banana kong l baixar sonic dash l baixar subway surf l baixar piano tiles 2 l baixar dr driving


Everyone has flaws, but it was in the past. I feel sad when people look back to the old state of me before, and at that time I speak somewhat childish. I honestly do not remember and do not have any impression about that at all. But I will overcome this to build yourself a more complete picture.
retrica photo l baixar slitherio l baixar b612

baixar musicas gratis- baixar snapchat

Do you love music? If you have the same interests as me, I believe you will like the new application that I discovered. Try to download, because it's very interesting. Listen to music and chat with friends via social networking, feel life is colorful
baixar snapchat , baixar musicas gratis


I appreciate your article, to me this is good and quality article. I regularly to your blog to read the post. I hope you will be more and more blog entries to add richer. You can also visit my website at any time.
pokemon go game , download hola launcher

strike force heroes 2 - square quick

Life becomes more interesting and wonderful when you share your memorable moments with friends and family through unique photographs. You can create your own unique style impressed with image editing software. And after hours of work stress you can also come up with a game certain to entertain and relax
strike force heroes 2 , square quick

Life becomes more interesting and wonderful when you share your memorable moments with friends and family through unique photographs. You can create your own unique style impressed with image editing software. And after hours of work stress you can als

Look advanced to far added agreeable from you.

You need to have time to take care of the active. It in fact was a amusement account it. Look advanced to far added agreeable from you.
download lucky patcher l temple run 2 game


First meet, I took the initiative to ask her phone number. Whenever there is a chance spare her appointment to talk, chat. Furthermore is my home, and she's pretty close together that when she encountered difficulties problem is that I will be present. Initially, the feelings of both stop at a friend, but it is true that a long straw near the fire, and now I also sharp and she has a twin.
baixar facebook gratis l baixar slitherio l baixar whatsapp gratis

i like this topic

I see nowadays don't generally give anything that I'm keen on, however I'm most definitely intrigued by this one.
lucky patcher app , wings io game , geometry dash game , launcher app , minecraft , download temple run 2 , sonic dash game

Five Nights at Freddy's

It’s really a great and helpful piece of information. I’m glad that you shared this helpful info with us. Please stay us up to date like this. Thank you for sharing.
Kizi | Friv | Happy Wheels | Five Nights at Freddy’s


Post new comment

This question is for testing whether you are a human visitor and to prevent automated spam submissions.