Inaccurate values for “Currently allocated space” and “Available free space” in the Shrink File dialog for TEMPDB only
转载自:http://blogs.msdn.com/b/ialonso/archive/2012/10/08/inaccurate-values-for-currently-allocated-space-and-available-free-space-in-the-shrink-file-dialog-for-tempdb-only.aspx
Inaccurate values for “Currently allocated space” and “Available free space” in the Shrink File dialog for TEMPDB only
RATE THIS 









8 Oct 2012 4:47 AM
Last week I went to a customer who showed me the following weird information.
He opened SSMS (SQL Server Management Studio) 2008 R2 and connected to one particular instance of SQL Server 2008 R2 in which he observed this behavior we wasn’t able to reproduce with any other.
From the Object Explorer window, he expanded the Databases node, then expanded System Databases. Right clickedtempdb, selected Tasks menu option, then Shrink, and finally Files. Such action brought up the following dialog, which as you can see, was already reporting something strange: a negative amount of free space.
![]()
Not only that didn’t fit my customer, he also knew for a fact that the data file wasn’t 8.00 MB but 11.75 MB, because that was what the file system reported as occupied space by that file.
![]()
So, it seemed both values were wrong.
Customer also mentioned he was only seeing this in that instance of SQL Server and only for tempdb data file. You will see later on why it only occurred with tempdb’s data files.
By looking into the source code of this piece of UI, I realized that in order to populate the Currently allocated space text box, it was using the value returned by Size property of the DataFile class (SMO).
Up until the version of SMO that comes with SQL Server 2008 R2, the Size property of an instance of the DataFile class, was being populated with the value returned in the size column of the corresponding row from the master.sys.master_files system table.
So I ran the following query:
select name, size, physical_name
from master.sys.master_files
where database_id = 2 and file_id = 1
and noticed it returned the following results:
name size physical_name
---------- ------ ------------------
tempdev 1024 C:\...\tempdb.mdf
(1 row(s) affected)
As per the documentation of sys.master_files, we know size is expressed in 8KB pages. Therefore, the 1024 we obtained as a result corresponds to 1024 pages of 8KB, which results in 8.00 MB. Just what we saw in the UI.
However, the following query that used tempdb.sys.database_files instead:
select name, size, physical_name from tempdb.sys.database_files where file_id = 1
Reported the actual, most current, data file size:
name size physical_name
---------- ------ ----------------------------------------------------------------------------------------------------
tempdev 1504 C:\...\tempdb.mdf
(1 row(s) affected)
That being 1504 pages of 8KB, resulting in 11.75 MB (or 12032 KB, just what we saw reported by the file system).
But where was that inconsistency coming from in the first place? Why the size reported in master_files didn’t match that reported in database_files?
Well, it happens that when SQL Server autogrows a file which is part of TEMPDB, the size change is not reflected in sys.master_files (or sys.sysaltfiles for that matter). As per the functional specifications of the storage engine, for TEMPDB the change in the size of one of its files is only reflected in those system tables for explicit grows and shrinks, not for those triggered by the automatic mechanisms.
Having said that, it makes the current size information in sys.master_files (sys.sysaltfiles) potentially staled for TEMPDB files.
Starting with SQL Server 2012, what the DataFile class delivers through its Size property is the value the storage engine exposes via the size column of the sys.database_files of the specific database.
So, because that value of that table is updated even in the described corner case, and since the code that implements this part of the UI hasn’t changed in 2012 (it still uses the same SMO DataFile.Size property), the information you see from the same dialog when invoked from the SSMS version that comes with SQL Server 2012 is not incorrect anymore.
By the way, the reason why the available space (obtained from the AvailableSpace property of the DataFile class) could show negative is because it is the result of substracting the value returned by the SpaceUsed property of the DataFile class (always accurate) to the value returned by the Size property (potentially staled as we’ve seen). If it happens that the amount of used space from your TEMPDB data file is larger than the outdated size reported, you get the negative.
The SpaceUsed property is always accurate because its value is whatever returns the SpaceUsed property of theFILEPROPERTY function for that given file. When you invoke that function to find out the space used for one file, the storage engine scans the GAM pages in that file and counts from them how many allocated extents there are. It multiplies that number by 8 (because each extent contains 8 pages), and that is the result you get.
Be aware that since the problem was present in SMO’s DataFile class, not only this dialog of SQL Server Management Studio could be affected, but any other program that relies on those two properties (AvailableSpace and Size) of the DataFile class.
Inaccurate values for “Currently allocated space” and “Available free space” in the Shrink File dialog for TEMPDB only的更多相关文章
- PermGen space 与 Java heap space
1.java.lang.OutOfMemoryError: PermGen spacePermGen space的全称是Permanent Generation space,是指内存的永久保存区域Ou ...
- System and method for critical address space protection in a hypervisor environment
A system and method in one embodiment includes modules for detecting an access attempt to a critical ...
- Multiple address space mapping technique for shared memory wherein a processor operates a fault handling routine upon a translator miss
Virtual addresses from multiple address spaces are translated to real addresses in main memory by ge ...
- Disk Space Usage 术语理解:unallocated, unused and reserved
通过standard reports查看Disk Usage,选中Database,右击,选择Reports->Standard Reports->Disk Space Usage,截图如 ...
- Hilbert space
Definition A Hilbert space H is a real or complex inner product space that is also a complete metric ...
- 【转载】alter table move 和 alter table shrink space的区别
move 和shrink 的共同点1.收缩段2.消除部分行迁移3.消除空间碎片4.使数据更紧密 shrink 语法: alter table TABLE_NAME shrink space [com ...
- [中英对照]Device Drivers in User Space: A Case for Network Device Driver | 用户态设备驱动: 以网卡驱动为例
前文初步介绍了Linux用户态设备驱动,本文将介绍一个典型的案例.Again, 如对Linux用户态设备驱动程序开发感兴趣,请阅读本文,否则请飘过. Device Drivers in User Sp ...
- How To Add Swap Space on Ubuntu 16.04
Introduction One of the easiest way of increasing the responsiveness of your server and guarding aga ...
- SHRINK SPACE Command : Online Segment Shrink for Tables, LOBs and IOTs
ORACLE-BASE - ALTER TABLE ... SHRINK SPACE Command : Online Segment Shrink for Tables, LOBs and IOTs ...
随机推荐
- linux 切换c++版本
删除gcc-4.6的软连接文件/usr/bin/gcc.(只是删除软连接) 命令:sudo rm /usr/bin/gcc 然后建一个软连接,指向gcc-4.4. 命令:sudo ln -s /usr ...
- Spark RDD概念学习系列之Spark的算子的作用(十四)
Spark的算子的作用 首先,关于spark算子的分类,详细见 http://www.cnblogs.com/zlslch/p/5723857.html 1.Transformation 变换/转换算 ...
- canvas绘制清晰的方法
很早就开始使用canvas,包括自己绘制各种图形,以及作为画布提供给诸如echarts,当canvas绘制细线条,特别是关于文字绘制会出现很模糊或者锯齿的感觉. <canvas ref=&quo ...
- linux下登陆用户的行为信息—w和who命令详解
查看用户的操作系统管理员若想知道某一时刻用户的行为,只需要输入命令w 即可,在SHELL终端中输入如下命令: [root@localhost ~]# w 可以看到执行w命令及显示结果. 命令信息含义上 ...
- C#Windows窗体界面设计_01_绘制三角函数_附强制类型转换
binzhouweichao@163.com 今天开始学习C#windows窗体界面设计. 首先说一下类型转换. 参考http://www.csharpwin.com/csharpspace/6848 ...
- eclipse scons 使用指南
http://sconsolidator.com/projects/sconsolidator/wiki/Getting_Started Add SCons support to an existin ...
- 基于EF的数据外键关联查询
现在很多ORM不自带外键关联的实体查询,比如我查询用户,用时将关联的角色信息查询出来,那么就要进行2次查询,很麻烦.而我现在要做的就是基于EF的外键关联查询.很方便的. 首先,创建基础查询的BaseS ...
- C#中反射的使用(How to use reflect in CSharp)(3)Emit的使用
Emit意在动态构建一个可以执行(当然也就可以反射)或者只可以反射的动态库. 个人认为在不得不使用反射的情况下,使用Emit会使得效率提升空间很大.亦或者动态插件模式的软件设计中会用到. 依然2%的废 ...
- [ALGO-3] K好数
算法训练 K好数 时间限制:1.0s 内存限制:256.0MB 问题描写叙述 假设一个自然数N的K进制表示中随意的相邻的两位都不是相邻的数字,那么我们就说这个数是K好数.求L位K进制数中K好数 ...
- Android Activity切换动画overridePendingTransition
Activity在切换或者是退出的时候能够使用渐入,滑动,缩放等动态效果.使用的就是方法overridePendingTransition,能够直在Activity其中直接调用. overridePe ...