You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by vicky b <vi...@gmail.com> on 2011/05/23 15:42:53 UTC

difference in behaviour of tomcat 6 and tomcat 7 very imp

Hi,

      I have done analysis on static references and found a interesting
point al;so i find a difference in behaviour in tomcat 6 andtomcat 7

I created a class (TestStatic) which has a static reference to VO(java bean)
in my servlet i declared TestStatic as member variable and initialized it
there and i deployed my app and hit my servlet and then undeployed it ... i
used eclipse MAT and saw that both TestStatic and VO objects are not getting
garbage collected

then i changed the code in my servlet and removed TestStatic from member
variable i used spring beanfactory to get TestStatic object in my service
method and repeated step of deploying hitting my servlet and undeploying ..
i used eclipse MAT and saw that only VO object are not getting garbage
collected

so what can be concluded static references are never garbage collected ? or
even objects holding static references are not GC'ed.

In tomcat static refrence are getting garbage collected but in weblogic it
is not can any body help

below is code for reference
\





*package* com.leak;
 *public* *class* TestEnum {

	*private*   BeanName name;
	*private*  *static*  VO vo = *new* VO();

	*public* *void* printNames() {
		System.out.println(name.beanName);
	}}

 *package* com.leak;
 *import* org.springframework.beans.factory.BeanFactory;*import*
org.springframework.context.support.ClassPathXmlApplicationContext;

 *public* *class* UtilsN1 {
		
	*public*  *static*   BeanFactory factory=*null*;
	*public*  *static*  *void* setContext(BeanFactory factory ){
		UtilsN1.factory = factory;

		System.err.println( "Utils.factory>>" + factory );

	}

	*public*  *static* BeanFactory getFactory() {
		*return* factory;
	}}
 *package* com.leak;
 *import* java.io.IOException;*import* java.io.PrintWriter;
 *import* javax.security.auth.Destroyable;*import*
javax.servlet.ServletConfig;*import*
javax.servlet.ServletException;*import*
javax.servlet.http.HttpServlet;*import*
javax.servlet.http.HttpServletRequest;*import*
javax.servlet.http.HttpServletResponse;
 *import* org.springframework.beans.factory.BeanFactory;*import*
org.springframework.beans.factory.xml.XmlBeanFactory;*import*
org.springframework.core.io.ClassPathResource;*import*
org.springframework.core.io.Resource;
 *public* *class* Leak *extends* HttpServlet {
	*private* *static* *final* *long* serialVersionUID = 1L;
	TestEnum a = *new* TestEnum();

	@Override
	*public* *void* init(ServletConfig config) *throws* ServletException {
		super.init(config);

		Resource res = *new* ClassPathResource("applicationContext.xml");
		BeanFactory factory = *new* XmlBeanFactory(res);
		System.out.println("factory:::" + factory.getBean("test"));

		UtilsN1.setContext(factory);

	}

	*protected* *void* doGet(HttpServletRequest request,
			HttpServletResponse response) *throws* ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println("<html><body><pre>");

		Test t = (Test) UtilsN1.getFactory().getBean(
				BeanName.TEST.getBeanName());
		System.out.println(" test " + t);
		//TestEnum a = (TestEnum) UtilsN1.getFactory().getBean("testEnum");
		a.printNames();

		out.println("</pre>gg</body></html>");
		out.close();
	}

	*protected* *void* doPost(HttpServletRequest request,
			HttpServletResponse response) *throws* ServletException, IOException {
	}

	*public* *void* destroy() {
		UtilsN1.factory = *null*;
	}
 }

 *package* com.leak;
 *public* *class* VO {
 String  name;
 *public* String getName() {
	*return* name;}
 *public* *void* setName(String name) {
	this.name = name;}
 }


-- 
*Thanks & Regards
 Vickyb

*

Re: difference in behaviour of tomcat 6 and tomcat 7 very imp

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Vicky,

On 5/23/2011 9:42 AM, vicky b wrote:
>       I have done analysis on static references and found a interesting
> point al;so i find a difference in behaviour in tomcat 6 andtomcat 7

Um, okay.

> I created a class (TestStatic) which has a static reference to VO(java bean)
> in my servlet i declared TestStatic as member variable and initialized it
> there

Let's see that mess of language above shown in code, shall we?

>  public class VO {
>     String  name;
>     public String getName() {
> 	return name;}
>       public void setName(String name) {
> 	this.name = name;}
>  }

No code for a servlet called "TestStatic" was included. Assuming you
aren't being very careful to document a problem that relies on very
detailed information, let's move-on with the code you *did* post.

Your servlet has a reference to TestEnum, not VO, but let's just keep
rollin'.

I've removed some of the code that doesn't affect usage of these
classes, etc.

>  *public* *class* TestEnum {
> 	*private*   BeanName name;
> 	*private*  *static*  VO vo = *new* VO();
> 	}}
>
>  *public* *class* Leak *extends* HttpServlet {
> 	TestEnum a = *new* TestEnum();
>  }

So, after undeploy, TestEnum.vo remains reachable?

It's possible that you have another leak that is causing the
WebappClassLoader to remain in memory, which will prevent your webapp's
java.lang.Class objects from being collected by the GC. In that case,
the static members will also remain in memory.

As suggested in another thread, you might want to look at a memory
profiler to see what is keeping your webapp's classes in memory.

Yes, things have changed from Tomcat 6 to Tomcat 7, but none of them
affect the JVM's GC algorithm(s). Recent Tomcat 6 and all Tomcat 7
releases include some speculative leak-prevention strategies that can
help reduce ClassLoader-pinning leaks that are out of your webapp's
control, so it's possible that's affecting your observations.

> so what can be concluded static references are never garbage collected ? or
> even objects holding static references are not GC'ed.

Something tells me it's more complicated than that.

> In tomcat static refrence are getting garbage collected but in weblogic it
> is not can any body help

If that's the case, I suspect Tomcat's leak-prevention code is helping
you out. You're welcome to read the code: it's quite straightforward.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk3azOEACgkQ9CaO5/Lv0PAxbgCeOrmCQlN/t1tgsTjHIeM1LtOQ
Z0QAmwZzzfAPqzRbEqpp7R6DDW41XLKC
=ZWjs
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org