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

Nacho Alonso Portillo

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.

时间: 2024-10-29 19:10:19

Inaccurate values for “Currently allocated space” and “Available free space” in the Shrink File dialog for TEMPDB only的相关文章

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?address?space?(CAS) of a guest operating system (OS) that has implemented?address?space?layout?randomization?in a hypervisor environment, identifying

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 generating for each virtual address an address space identifier (AID) identifying its address space. Then, the virtual address and its AID are used to o

Scale Space(zz Wiki)

Scale space From Wikipedia, the free encyclopedia Scale space Scale-space axioms Scale-space implementation Feature detection Edge detection Blob detection Corner detection Ridge detection Interest point detection Scale selection Affine shape adaptat

Oracle中shrink space命令

shrink_clause:   http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_3001.htm#i2192484 首先oracle shrink 是10g之后才引出的,有shrink table 和shrink space两种,这里介绍shrink space 压缩分两个阶段: 1.数据重组:这个过程是通过一系列的insert delete操作,将数据尽量排在列的前面进行重新组合. 2.HWM调整:这个过程是对

mssql 系统函数 字符串函数 space 功能简介

转自: http://www.maomao365.com/?p=4672  一.space 函数功能简介 space功能:返回指定数量的空格参数简介: 参数1: 指定数量,参数需为int类型 注意事项: 1 如果参数1输入为非varchar或nvarchar类型,那么sql引擎先进行类型转换,如果转换失败,则返回错误信息 否则继续执行此函数 2 如果参数等于零的数值,那么就返回空字符串 3 如果参数小于零,那么就返回null ,会导致字符串叠加操作失败  二.space 函数举例说明例1: /*

Tomcat 优化 java.lang.OutOfMemoryError: Java heap space 的解决方法

java.lang.OutOfMemoryError: Java heap space 的解决方法 关键字: tomcat outofmemoryerror permgen space java heap space 最近在熟悉一个开发了有几年的项目,需要把数据库从mysql移植到oracle,首先把jdbc的连接指向mysql,打包放到tomcat里面,可以跑起来,没有问题,可是当把jdbc连接指向oracle的时候,tomcat就连续抛java.lang.OutOfMemoryError的错

Tomcat – java.lang.OutOfMemoryError: PermGen space Cause and Solution

Read more: http://javarevisited.blogspot.com/2012/01/tomcat-javalangoutofmemoryerror-permgen.html#ixzz3QDWa3Zqi Tomcat web server often suffers from java.lang.OutOfMemoryError: PermGen space whenever you deploy and undeploy your web application coupl

IT忍者神龟之 配额不足的解决方法ORA-01536: space quota exceeded for tablespace

今天有同事反映最近几天的数据在oracle中查不到.检查TT的错误日志显示:TT5211: TT5211: Oracle out of resource error in OCIStmtExecute(): ORA-01536: space quota exceeded for tablespace 'TBSLOG' rc = -1 -- file "bdbTblH.c", lineno 2452, procedure "ttBDbStmtForce()". 明显的

Functional Analysis-Metric Space

Definition A metric space is a pair $(X,d)$ where $X$ is a set and $d$ is a metric(or distance function) $d$ on $X$, that is, a function defined on $X\times X$ such that for all $x,y,z\in X$ we have 4 axioms of a metric: (1) $d$ is real-valued, finit