简体   繁体   English

如何在Servlet中集成memcached? Tomcat和内存泄漏

[英]How to integrate memcached in a Servlet? Tomcat & memory leaks

Searching memcached java in google, the first result is Using Memcached with Java . 在Google中搜索memcached java,第一个结果是将Memcached与Java结合使用

The guy (who calls himself Just some Random Asshole in the Internet! ) proposes a Singleton based on net.spy.memcached . 这个家伙(自称为Internet上的随机混蛋! )基于net.spy.memcached提出了一个Singleton。 It basically creates 20 threads and connections by creating 20 instances of MemcachedClient. 它基本上通过创建20个MemcachedClient实例来创建20个线程和连接。 For every request it chooses one at random. 对于每个请求,它都会随机选择一个。

However those threads and connections are never closed and they pile up every time I hot swap the application during development (with warnings from Tomcat 7). 但是,这些线程和连接永远不会关闭,并且每次在开发过程中热交换应用程序时它们就会堆积(带有Tomcat 7的警告)。

SEVERE: The web application [/MyAppName] appears to have started a thread named
[...] but has failed to stop it. This is very likely to create a memory leak.

By looking at MemcachedClient JavaDoc, I see a method called shutdown with the only description being "Shut down immediately." 通过查看MemcachedClient JavaDoc,我看到了一种称为shutdown的方法,唯一的描述是“立即关闭”。 Shut down what? 闭嘴 The client? 客户端? The server? 服务器? I suppose is the client, since it's in MemcachedClient and I suppose that this method would close the connection and terminate the thread. 我想是客户端,因为它在MemcachedClient中,我想这个方法会关闭连接并终止线程。 EDIT : yes, it shuts down the client. 编辑 :是的,它关闭了客户端。

Question 1 How to force the execution of cleanup code in Tomcat 7, before the application is hot swapped? 问题1在热交换应用程序之前,如何在Tomcat 7中强制执行清除代码?

Question 2 Is this approach of using memcached (with cleanup code), correct or is better I start over in a different way? 问题2这种使用memcached(带有清除代码)的方法是否正确,还是更好地以其他方式重新开始?

I think creating 20 memcache clients is silly - that's like creating 20 separate copies of your DB connection pool. 我认为创建20个内存缓存客户端是很愚蠢的-就像创建数据库连接池的20个单独副本一样。 The idea with that client is that it multiplexes a variety of requests with asynch IO. 该客户端的想法是它将多种请求与异步IO复用。 http://code.google.com/p/spymemcached/wiki/Optimizations http://code.google.com/p/spymemcached/wiki/Optimizations

As far as shutting it down, simply call: yourClient.shutdown() to shutdown immediately, or yourClient.shutdown(3, TimeUnit.SECONDS) for example, to allow some time for a more graceful shutdown. 至于关闭它,只需调用:yourClient.shutdown()立即关闭,或例如yourClient.shutdown(3,TimeUnit.SECONDS),以留出一些时间进行更正常的关闭。

That could be called from your Servlet's .destroy method, or a context listener for your whole WAR. 可以从Servlet的.destroy方法或整个WAR的上下文侦听器中调用该方法。

我对memcached一无所知,但您可能可以编写一个自定义上下文侦听器,并在上下文侦听器中放入某种关闭挂钩,以便在上下文关闭时可以遍历单例中的项目并将其关闭。

It turned out that it was a bug of Java AWS SDK and was not related to memcached. 原来,这是Java AWS开发工具包的错误,与memcached无关。 Version 1.2.2 of Java AWS SDK has this bug fixed. Java AWS SDK 1.2.2版已修复此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM