- 浏览: 849745 次
- 性别:
- 来自: lanzhou
文章分类
最新评论
-
liu346435400:
楼主讲了实话啊,中国程序员的现状,也是只见中国程序员拼死拼活的 ...
中国的程序员为什么这么辛苦 -
qw8226718:
国内ASP.NET下功能比较完善,优化比较好的Spacebui ...
国内外开源sns源码大全 -
dotjar:
敢问兰州的大哥,Prism 现在在12.04LTS上可用么?我 ...
最佳 Ubuntu 下 WebQQ 聊天体验 -
coralsea:
兄弟,卫星通信不是这么简单的,单向接收卫星广播信号不需要太大的 ...
Google 上网 -
txin0814:
我成功安装chrome frame后 在IE地址栏前加上cf: ...
IE中使用Google Chrome Frame运行HTML 5
This is the fifth post in my Pushing the Limits of Windows series where I explore the upper bound on the number and size of resources that Windows manages, such as physical memory, virtual memory, processes and threads: Pushing the Limits of Windows: Physical Memory Pushing the Limits of Windows: Virtual Memory This time I’m going to go inside the implementation of handles to find and explain their limits. Handles are data structures that represent open instances of basic operating system objects applications interact with, such as files, registry keys, synchronization primitives, and shared memory. There are two limits related to the number of handles a process can create: the maximum number of handles the system sets for a process and the amount of memory available to store the handles and the objects the application is referencing with its handles. In most cases the limits on handles are far beyond what typical applications or a system ever use. However, applications not designed with the limits in mind may push them in ways their developers don’t anticipate. A more common class of problems arise because the lifetime of these resources must be managed by applications and, just like for virtual memory, resource lifetime management management is challenging even for the best developers. An application that fails to release unneeded resources causes a leak of the resource that can ultimately cause a limit to be hit, resulting in bizarre and difficult to diagnose behaviors for the application, other applications or the system in general. As always, I recommend you read the previous posts because they explain some of the concepts this post references, like paged pool. The kernel-mode core of Windows, which is implemented in the %SystemRoot%\System32\Ntoskrnl.exe image, consists of various subsystems such as the Memory Manager, Process Manager, I/O Manager, Configuration Manager (registry), which are all parts of the Executive. Each of these subsystems defines one or more types with the Object Manager to represent the resources they expose to applications. For example, the Configuration Manager defines the key object to represent an open registry key; the memory manager defines the Section object for shared memory; the Executive defines Semaphore, Mutant (the internal name for a mutex), and Event synchronization objects (these objects wrap fundamental data structures defined by the operating system’s Kernel subsystem); the I/O Manager defines the File object to represent open instances of device driver resources, which include file system files; and the Process Manager the creates Thread andProcess objects I discussed in my last Pushing the Limits post. Every release of Windows introduces new object types with Windows 7 defining a total of 42. You can see the objects defined by running the Sysinternals Winobj utility with administrative rights and navigating to the ObjectTypes directory in the Object Manager namespace: When an application wants to manage one of these resources it first must call the appropriate API to create or open the resource. For instance, the CreateFile function opens or creates a file, the RegOpenKeyExfunction opens a registry key, and the CreateSemaphoreEx function opens or creates a semaphore. If the function succeeds, Windows allocates a handle in the handle table of the application’s process and returns the handle value, which applications treat as opaque but that is actually the index of the returned handle in the handle table. With the handle in hand, the application then queries or manipulates the object by passing the handle value to subsequent API functions like ReadFile, SetEvent, SetThreadPriority, and MapViewOfFile. The system can look up the object the handle refers to by indexing into the handle table to locate the corresponding handle entry, which contains a pointer to the object. The handle entry also stores the accesses the process was granted at the time it opened the object, which enables the system to make sure it doesn’t allow the process to perform an operation on the object for which it didn’t ask permission. For example, if the process successfully opened a file for read access, the handle entry would look like this: If the process tried to write to the file, the function would fail because the access hadn’t been granted and the cached read access means that the system doesn’t have to execute a more expensive access-check again. You can explore the first limit with the Testlimit tool I’ve been using in this series to empirically explore limits. It’s available for download on the Windows Internals book page here. To test the number of handles a process can create, Testlimit implements the –h switch that directs it to create as many handles as possible. It does so by creating an event object with CreateEvent and then repeatedly duplicating the handle the system returns using DuplicateHandle. By duplicating the handle, Testlimit avoids creating new events and the only resources it consumes are those for the handle table entries. Here’s the result of Testlimit with the –h option on a 64-bit system: The result doesn’t represent the total number of handles a process can create, however, because system DLLs open various objects during process initialization. You see a process’s total handle count by adding a handle count column to Task Manager or Process Explorer. The total shown for Testlimit in this case is 16,711,680: When you run Testlimit on a 32-bit system, the number of handles it can create is slightly different: Its total handle count is also different, 16,744,448: Where do the differences come from? The answer lies in the way that the Executive, which is responsible for managing handle tables, sets the per-process handle limit, as well as the size of a handle table entry. In one of the rare cases where Windows sets a hard-coded upper limit on a resource, the Executive defines 16,777,216 (16*1024*1024) as the maximum number of handles a process can allocate. Any process that has more than a ten thousand handles open at any given point in time is likely either poorly designed or has a handle leak, so a limit of 16 million is essentially infinite and can simply help prevent a process with a leak from impacting the rest of the system. Understanding why the numbers Task Manager shows don’t equal the hard-coded maximum requires a look at the way the Executive organizes handle tables. A handle table entry must be large enough to store the granted-access mask and an object pointer. The access mask is 32-bits, but the pointer size obviously depends on whether it’s a 32-bit or 64-bit system. Thus, a handle entry is 8-bytes on 32-bit Windows and 12-bytes on 64-bit Windows. 64-bit Windows aligns the handle entry data structure on 64-bit boundaries, so a 64-bit handle entry actually consumes 16-bytes. Here’s the definition for a handle entry on 64-bit Windows, as shown in a kernel debugger using the dt (dump type) command: The output reveals that the structure is actually a union that can sometimes store information other than an object pointer and access mask, but those two fields are highlighted. The Executive allocates handle tables on demand in page-sized blocks that it divides into handle table entries. That means a page, which is 4096 bytes on both x86 and x64, can store 512 entries on 32-bit Windows and 256 entries on 64-bit Windows. The Executive determines the maximum number of pages to allocate for handle entries by dividing the hard-coded maximum,16,777,216, by the number of handle entries in a page, which results on 32-bit Windows to 32,768 and on 64-bit Windows to 65,536. Because the Executive uses the first entry of each page for its own tracking information, the number of handles available to a process is actually 16,777,216 minus those numbers, which explains the results obtained by Testlimit: 16,777,216-65,536 is 16,711,680 and 16,777,216-65,536-32,768 is 16,744,448. The second limit affecting handles is the amount of memory required to store handle tables, which the Executive allocates from paged pool. The Executive uses a three-level scheme, similar to the way that processor Memory Management Units (MMUs) manage virtual to physical address translations, to keep track of the handle table pages that it allocates. We’ve already seen the organization of the lowest and mid levels, which store actual handle table entries. The top level serves as pointers into the mid-level tables and includes 1024 entries per-page on 32-bit Windows. The total number of pages required to store the maximum number of handles can therefore be calculated for 32-bit Windows as 16,777,216/32768, which is 128MB. That’s consistent with the paged pool usage of Testlimit as shown in Task Manager: On 64-bit Windows, there are 512 pointers in a page of top-level pointers. That means the total paged pool usage for a full handle table is 16,777,216/65,536, which is 262MB. A look at Testlimit’s paged pool usage on 64-bit Windows confirms the calculation: Paged pool is generally large enough to more than accommodate those sizes, but as I stated earlier, a process that creates that many handles is almost certainly going to exhaust other resources, and if it reaches the per-process handle limit it will probably fail itself because it can’t open any other objects. A handle leaker will have a handle count that rises over time. The reason that a handle leak is so insidious is that unlike the handles Testlimit creates, which all point to the same object, a process leaking handles is probably leaking objects as well. For example, if a process creates events but fails to close them, it will leak both handle entries and event objects. Event objects consume nonpaged pool, so the leak will impact nonpaged pool in addition to paged pool. You can graphically spot the objects a process is leaking using Process Explorer’s handle view because it highlights new handles in green and closed handles in red; if you see lots of green with infrequent red then you might be seeing a leak. You can watch Process Explorer’s handle highlighting in action by opening a Command Prompt process, selecting the process in Process Explorer, opening the handle-view lower pane and then changing directory in the Command Prompt. The old working directory’s handle will highlight in red and the new one in green: By default, Process Explorer only shows handles that reference objects that have names, which means that you won’t see all the handles a process is using unless you select Show Unnamed Handles and Mappings from the View menu. Here are some of the unnamed handles in Command Prompt’s handle table: Just like most bugs, only the developer of the code that’s leaking can fix it. If you spot a leak in a process that can host multiple components or extensions, like Explorer, a Service Host or Internet Explorer, then the question is what component is the one responsible for the leak. Figuring that out might enable you to avoid the problem by disabling or uninstalling the problematic extension, fix the problem by checking for an update, or report the bug to the vendor. Fortunately, Windows includes a handle tracing facility that you can use to help identify leaks and the responsible software. It’s enabled on a per-process basis and when active causes the Executive to record a stack trace at the time every handle is created or closed. You can enable it either by using the Application Verifier, a free download from Microsoft, or by using the Windows Debugger (Windbg). You should use the Application Verifier if you want the system to track a process’s handle activity from when it starts. In either case, you’ll need to use a debugger and the !htrace debugger command to view the trace information. To demonstrate the tracing in action, I launched Windbg and attached to the Command Prompt I had opened earlier. I then executed the !htrace command with the -enable switch to turn on handle tracing: I let the process’s execution continue and changed directory again. Then I switched back to Windbg, stopped the process’s execution, and executed htrace without any options, which has it list all the open and close operations the process executed since the previous !htrace snapshot (created with the –snapshot option) or from when handle tracing was enabled. Here’s the output of the command for the same session: The events are printed from most recent operation to least, so reading from the bottom, Command Prompt opened handle 0xb8, then closed it, next opened handle 0x22c, and finally closed handle 0xec. Process Explorer would show handle 0x22c in green and 0xec in red if it was refreshed after the directory change, but probably wouldn’t see 0xb8 unless it happened to refresh between the open and close of that handle. The stack for 0x22c’s open reveals that it was the result of Command Prompt (cmd.exe) executing its ChangeDirectory function. Adding the handle value column to Process Explorer confirms that the new handle is 0x22c: If you’re just looking for leaks, you should use !htrace with the –diff switch, which has it show only new handles since the last snapshot or start of tracing. Executing that command shows just handle 0x22c, as expected: Finally, a great video that presents more tips for debugging handle leaks is this Channel 9 interview with Jeff Dailey, a Microsoft Escalation Engineer that debugs them for a living:http://channel9.msdn.com/posts/jeff_dailey/Understanding-handle-leaks-and-how-to-use-htrace-to-find-them/ Next time I’ll look at limits for a couple of other handle-based resources, GDI Object and USER Objects. Handles to those resources are managed by the Windows subsystem, not the Executive, so use different resources and have different limits. read more >> http://blogs.technet.com/markrussinovich/archive/2009/09/29/3283844.aspx
Handles and Objects
Maximum Number of Handles
Handles and Paged Pool
Handle Leaks
发表评论
-
主板上的Debug灯的作用
2009-12-10 23:18 3056适合AwardBIOS AmiBIOS ... -
A平台?I平台?性能对比图!
2009-11-22 06:13 922Intel滴CPU: 奔腾E系列的 ( ... -
Microsoft to Open Source the .NET Micro Framework
2009-11-17 17:30 832I have great news to announce. ... -
揭秘Win7中最受大众喜爱的前五大特性
2009-11-10 08:17 909微软正式发布windows7操 ... -
看看国外媒体怎么评价Windows 7系统
2009-11-06 08:01 1049《纽约时报》:“(微软 ... -
微星AMD 785G主板的BIOS设置详解
2009-11-05 08:14 4795一、BIOS主菜单 1、St ... -
盈通785G显卡超频/开核教程
2009-11-05 08:10 3788HT总线调节——消除超频 瓶颈 HT总线频 ... -
一根铜丝降低北桥温度
2009-11-04 10:49 6231夏天已至,爱机温度也 ... -
微软10款最差产品:Windows ME和Vista上榜
2009-11-02 08:01 1063英国科技网站V3周六刊文,评出了微软历史上最差的10款产品,其 ... -
微软CEO10大经典语录:上网本不是魔鬼
2009-10-31 15:03 1033据国外媒体报道,在上周于纽约召开的“微软战略更新大会”(Str ... -
windows 24年以来的包装设计(图)
2009-10-18 08:34 1783Windows 1.0 - blue is the colou ... -
九款Windows 7替补操作系统
2009-10-13 08:27 985来自国外媒体的消息,微软新一代操作系统Windows 7无疑 ... -
Software Security Errors
2009-10-13 07:58 863This site presents a taxonomy o ... -
小心你ADSL猫的密码
2009-10-13 07:53 1008随着网络的普及,网 ... -
简单的硬盘安装windows7方法
2009-10-11 09:01 1467原系统(2000/XP/2003/VISTA/2008/WIN ... -
超快硬盘安装Windows 7方法
2009-10-11 09:00 1769对于没刻录机的朋友, ... -
我们该放弃Windows XP使用Windows 7吗?
2009-10-10 09:59 1109在 Vista 的黑暗时期,Windows XP 仍旧有许多忠 ... -
Why Windows 7 Won't Save Microsoft
2009-10-10 09:38 824On Wednesday, the Wall Street J ... -
Helios:微软的又一个操作系统?
2009-10-06 08:19 840多核心操作系统Barrelfish尚还历历在目,今天我们又看到 ... -
Windows 7 to usher in crush of cheap laptops
2009-10-06 07:51 788Call it the Netbook halo effect ...
相关推荐
解压后获得文件:《iOS 7 Programming Pushing the Limits》.pdf 全高清文字版非图片扫描版,完整!! 其中的一些目录: Part I What’s New? C h a p t e r 1 The Brand New Stuff The New UI …… Enhancements to...
### iOS7 Programming Pushing the Limits #### 精品iOS开发书籍:内容深入与高端进阶 本书《iOS7 Programming Pushing the Limits》是一本专为iOS开发者设计的专业书籍,旨在帮助开发者掌握iOS7下的高级编程技巧...
标题《iOS 6 Programming Pushing the Limits》明确指出了书籍的内容定位——旨在推动读者对Apple iPhone、iPad和iPod Touch应用程序开发的技能极限。作者是Rob Napier和Mugunth Kumar,由John Wiley & Sons, Ltd....
iOS7 Programming Pushing the Limits epub版本
iOS.5.Programming.Pushing.the.Limits.Developing.Extraordinary.Mobile.Apps.for.Apple.iPhone.iPad.and.iPod.Touch.Wiley
《Android Programming - Pushing the Limits》由Erik Hellman编写,首次出版于2014年,是一本深入探讨Android开发高级技术和最佳实践的专业书籍。本书由John Wiley & Sons Ltd出版,出版社位于英国西苏塞克斯郡...
[Wiley] JavaScript Programming Pushing the Limits (E-Book) ☆ 图书概要:☆ JavaScript has grown up, and it's a hot topic. Newer and faster JavaScript VMs and frameworks built upon them have ...
https://github.com/andyzeng/visual-pushing-grasping/raw/master/images/self-supervision.gif 视觉推送和抓取工具箱 视觉推动和抓取 (VPG) 是一种训练机器人代理学习如何规划互补推动和抓取动作以进行操作的方法...
在普通编程的基础上,如何最大限度的使用ios5和利用资源.
根据提供的文件信息,我们可以总结以下几点关于“iOS 7 Programming: Pushing the Limits”这本书的内容和知识点: 1. 书籍名称和主要内容: 该书籍的全名为《iOS 7 Programming: Pushing the Limits》,由Rob ...
title={ViNet: Pushing the limits of Visual Modality for Audio-Visual Saliency Prediction}, author={Samyak Jain and Pradeep Yarlagadda and Shreyank Jyoti and Shyamgopal Karthik and Ramanathan ...
根据提供的文件信息,我们可以推断出这是一本关于CSS3技术的专业书籍——《CSS3:极限探索》(Pushing the Limits),作者为Stephen Greig。本书首次出版于2013年7月,并由John Wiley & Sons出版社发行。以下是根据...
Analysis of recent advances demonstrating the positive impact of phase-based processing in pushing the limits of conventional methods. Offers unique coverage of the historical context, fundamentals of...
Example: The Dynamic Extension of the Greet Application Example: Unloading Unreachable Greeters On the CD-ROM The Resources Page 9 Garbage Collection Why Garbage Collection? Garbage Collection ...
These results are further pushing the limits of extremely scalable platforms and enabling a higher performing class of tiered storage, ranging from mega datacenter scale-out architectures to ultra ...