Threadlocals are used in J2EE application servers to keep track of transaction context and security context. I was thinking of using it to pass execution context (which involves some user login and other context information) around in web application which involves servlets and other server side classes. (As discussed at http://www.theserverside.com/news/thread.tss?thread_id=41473).
I remember reading somewhere that Threadlocals can cause memory leaks if Threads are getting reused through thread pools. (Which is the case in J2EE application servers).
What way can Threadlocal cause memory leak? How Threadlocals behave with Classloaderes in J2EE? In what cases thread local usage is not recommended?
Thanks,
Unmesh
Joshua Bloch 回复
Folks,
This thread is littered with misinformation. Java's ThreadLocal implementation is very, very fast. It has been completely re-written several times, getting faster each time. (Props to Doug Lea.) Larry Riedel's description ("some Java code that does a lookup in a hashtable where the Thread object is a key, which is something I as an application developer could do myself.") is wrong on two counts: Only the 1.2 implementation, which was a stopgap, was implemented as per-ThreadLocal map from thread to value. And practically all application programmers could not "do it themselves." I suggested adding thread locals to the platform only after I saw the fourth broken implementation out of four independent attempts. Without naming names, these implementations were written by elite systems programmers.
There is nothing inherently wrong with thread locals: They do not cause memory leaks. They are not slow. They are more local than their non-thread-local counterparts (i.e., they have better information hiding properties). They can be misused, of course, but so can most other programming tools.
Whether thread locals are in the language or the libraries is largely a matter of syntax. In '97 I proposed adding thread locals to the language (the threadlocal modifier), but there was insufficient support for this, so I wrote the ThreadLocal API that we now use.
Larry says "It is not apparent to me there are many situations for ordinary application programmers where using ThreadLocal would be a better idea than just using some sort of application level mapping." I am flabbergasted. What makes Larry think that Joe Programmer's ThreadLocal implementation will be better than the one that Doug and I wrote and carefully optimized? It most certainly will not.
There are many compelling uses for thread locals. Here are a few off the top of my head:
(1) Genuine per-thread context, such as user id or transaction id. Works great. Easy to clean up when the thread exits the scope. No leaks.
(2) Per-thread instances for performance. Aaron's SimpleDateFormat example (above) is one example of this pattern.
(3) "Sleazing" values through callbacks that you don't control: sometimes you must call a library method that calls back into your package. At this point, you need some context that you were unable to pass to yourself, due to deficiencies in the library. In this rare situation, thread locals can be a lifesaver.
In all of these cases, thread locals are the right thing to use. Can you cause unintended object retention with thread locals? Sure you can. But you can do this with arrays too. That doesn't mean that thread locals (or arrays) are bad things. Merely that you have to use them with some care. The use of thread pools demands extreme care. Sloppy use of thread pools in combination with sloppy use of thread locals can cause unintended object retention, as has been noted in many places. But placing the blame on thread locals is unwarranted. Virtually all threading systems of which I'm aware provide support for thread local variables. There's a reason for this: they are a fundamental part of the thread abstraction.
Uappologetically yours,
Josh
分享到:
相关推荐
### Detected Memory Leaks 简单检测方法 #### 背景介绍 在软件开发过程中,内存泄漏是一个常见的问题,特别是在使用C/C++等需要手动管理内存的语言进行开发时更为突出。内存泄漏不仅会导致程序占用的内存持续增长...
### 消除Symbian OS C++项目中的内存泄漏 #### 概述 在Symbian OS中,内存管理是一项至关重要的任务,特别是在C++项目中。本文将深入探讨如何在Symbian OS环境中检测、避免内存泄漏,并提供具体方法来解决这些问题...
这篇“Finding memory leaks发现内存的泄漏”可能是关于如何定位和解决内存泄漏的技术指南。 在C++编程中,程序员需要手动管理内存,通过`new`关键字申请内存,然后通过`delete`关键字释放内存。如果忘记释放或错误...
标题《论文研究-A Tool for Testing Java Memory Leaks.pdf》和描述《一个测试Java内存泄漏的工具,党万春,钱巨,虽然Java语言有自带的垃圾回收机制,Java程序仍然存在由无效引用带来的内存泄漏。而内存泄漏会导致...
平常我们都会用 Instrument 的 Leaks / Allocations 或其他一些开源库进行内存泄露的排查,但是检查过程非常繁琐,而且不清晰,最主要的是Abandoned memory不会被检测出来。 Leaks 从苹果的开发者文档里可以看到,一...
标题 "How catch memory leaks with very little effort (7KB)" 提示了这是一个关于如何轻松检测内存泄漏的技术分享。内存泄漏是编程中的一个常见问题,尤其是在C++等不自动管理内存的语言中,程序员需要手动分配和...
iOS应用隐私泄露检测知识点: iOS应用隐私泄露检测是指对运行在苹果iOS操作系统上的应用进行审查,以发现它们是否将用户设备上的敏感信息泄露给第三方。随着智能手机的普及,特别是苹果公司的iOS和谷歌公司的...
You will begin with a basic understanding of memory management, and why memory leaks occur in an application, moving on to autorelease pools and object creation/storage to get an idea of how memory ...
### Java内存泄漏详解 #### 摘要 本文旨在探讨如何在IBM WebSphere Application Server (WAS) 3.5、4.01 和 5.0 版本上诊断和定位 Java 内存泄漏问题,包括分布式环境和 z/OS 环境。文章将介绍一种通用的方法论,...
Memory leaks occur when a program allocates memory but fails to release it back to the system, causing the application to consume more and more memory over time. This can lead to poor performance and ...
在压缩包中的文件,如`memory_leaks.shtml.htm`,可能包含了更详细的内存泄漏检测和解决方法。`VC Empire.htm`可能涉及到在Visual C++环境中如何处理内存泄漏的问题。阅读这些文件可以获取更多实用技巧和案例分析。 ...
本程序是一个在模拟器上定位BREW应用... It can give the call stack of memory leaks (including the locations that MALLOC/REALLOC been called, and also the locations that ISHELL_CreateInstance been called).
内存泄漏是程序运行过程中的常见问题,特别是在C和C++这样的低级编程语言中,由于程序员需要手动管理内存,不正确的内存分配和释放可能导致内存泄漏。`__wrap_malloc`是一种调试技术,用于检测由`malloc()`函数引起...
It's a very effective way to quickly diagnose, and fix, memory leaks in C/C++ applications. The main difference between the CRT Debug Library and VLD, is that Visual Leak Detector shows you the ...
- fixed memory leaks in font packing subsystem + Added Embarcadero RAD Studio XE5 support + Added Internal components for FireDac database engine + fixed bug with images in PDF export for OSX viewers ...
- fixed memory leaks in font packing subsystem + Added Embarcadero RAD Studio XE5 support + Added Internal components for FireDac database engine + fixed bug with images in PDF export for OSX viewers ...
- fixed memory leaks in font packing subsystem + Added Embarcadero RAD Studio XE5 support + Added Internal components for FireDac database engine + fixed bug with images in PDF export for OSX viewers ...
The Eclipse Memory Analyzer is a fast and feature-rich Java heap analyzer that helps you find memory leaks and reduce memory consumption. Use the Memory Analyzer to analyze productive heap dumps with...
Learn how to analyze CLR 4 .NET application and service crashes and freezes, navigate through memory dump space (managed and unmanaged code) and diagnose corruption, leaks, CPU spikes, blocked ...
Chapter 12, Audio in Game Development, explains how to add sound and music effects to games, and avoiding memory leaks while playing sounds. Chapter 13, Tips and Tricks, has some neat tips and tricks...