如何将memcached集成到Servlet中?雄猫内存泄漏
在google中搜索memcached java,第一个结果是Using Memcached with Java。
这个家伙(自称只是互联网上的某个随机混蛋!)提出了一个基于net.spy.memcached的单例。它基本上通过创建 20 个 MemcachedClient 实例来创建 20 个线程和连接。对于每个请求,它都会随机选择一个。
然而,这些线程和连接永远不会关闭,每次我在开发过程中热交换应用程序时,它们都会堆积起来(来自 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.
通过查看 MemcachedClient JavaDoc,我看到一个名为 shutdown 的方法,唯一的描述是“立即关闭”。关闭什么?客户?服务器?我想是客户端,因为它在 MemcachedClient 中,我想这个方法会关闭连接并终止线程。 编辑:是的,它会关闭客户端。
问题1 如何在应用程序热插拔之前强制执行Tomcat 7中的清理代码?
问题 2 这种使用 memcached 的方法(带有清理代码)正确吗?或者我以不同的方式重新开始会更好?
Searching memcached java in google, the first result is Using Memcached with Java.
The guy (who calls himself Just some Random Asshole in the Internet!) proposes a Singleton based on net.spy.memcached. It basically creates 20 threads and connections by creating 20 instances of MemcachedClient. 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).
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." 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. 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?
Question 2 Is this approach of using memcached (with cleanup code), correct or is better I start over in a different way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为创建 20 个内存缓存客户端很愚蠢 - 这就像创建数据库连接池的 20 个独立副本一样。该客户端的想法是通过异步 IO 复用各种请求。
http://code.google.com/p/spymemcached/wiki/Optimizations
至于关闭它,只需调用:
yourClient.shutdown() 立即关闭,或者
例如,yourClient.shutdown(3, TimeUnit.SECONDS),以便留出一些时间进行更正常的关闭。
这可以从 Servlet 的 .destroy 方法调用,或者从整个 WAR 的上下文侦听器调用。
I think creating 20 memcache clients is silly - that's like creating 20 separate copies of your DB connection pool. The idea with that client is that it multiplexes a variety of requests with asynch IO.
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.
That could be called from your Servlet's .destroy method, or a context listener for your whole WAR.
我对 memcached 一无所知,但您可能可以编写一个自定义上下文侦听器,并在上下文侦听器中放置某种关闭挂钩,以便当上下文关闭时,您可以循环访问单例中的项目并将其关闭。
I don't know anything about memcached, but you could probably write a custom context listener and put some kind of shutdown hook in the context listener so that when the context shutdown you could loop through the items in your singleton and shut them down.
原来是Java AWS SDK的一个bug,与memcached无关。 Java AWS SDK 版本 1.2.2 已修复此错误。
It turned out that it was a bug of Java AWS SDK and was not related to memcached. Version 1.2.2 of Java AWS SDK has this bug fixed.