开发者

Strange memory leak when reading serialized data in Tomcat web app

开发者 https://www.devze.com 2023-02-16 09:53 出处:网络
I\'m deploying a relatively simple web application to Tomcat 7.0.8 (JVM 1.6). The app registers a ServletContextListener and has a single resource called \"data\" located in WEB-INF.The \"data\" file

I'm deploying a relatively simple web application to Tomcat 7.0.8 (JVM 1.6).

The app registers a ServletContextListener and has a single resource called "data" located in WEB-INF. The "data" file contains a single serialized in开发者_运维问答stance of a simple class called Data. Data has a few public String and Integer fields but nothing else. It implements Serializable and defines its own serialVersionUID.

In the contest listener, contextInitialized() does the following:

  1. Get an InputStream to "data" from the ServletContext,
  2. Create an ObjectInputStream from the InputStream,
  3. Read a single object from the ObjectInputStream,
  4. Cast it to the type "Data",
  5. Write the field values to System.out,
  6. Close the ObjectInputStream and its backing InputStream.

Everything seems to work fine. However when I stop the webapp through Tomcat's manager application, then check for memory leaks, Tomcat informs me that my app leaked memory when it was stopped.

I'm positive the issue is with the Data class, since when I serialize a String or Integer I don't see the leak.

Any thoughts?


If this file is read in once on initialization of the web app, how is a memory leak significant? Does it even merit the title?

Memory leaks are significant to Tomcat and all app servers because they're intended to stay alive for a long time. Memory leaks are an issue only if they show up over a longer period of time.

Your instincts are good, but your procedures are flawed in my opinion.

I'd recommend that you use a better tool - the Visual VM that's bundled with JDK 6 and higher - and that you monitor your Tomcat while it's being used in a manner that's closer to your real production needs.

You can't draw any conclusion regarding memory leaks from a single request.

0

精彩评论

暂无评论...
验证码 换一张
取 消