Get memory allocated to SQL Server 2008 using C#
I developed an installer and now I want to ad开发者_JS百科d feature (basically a check) that will check the memory allocated to SQL Server.
I can login to SQL Server and get memory details, but on client side they don't provide access to SQL Server and so I need to develop a tool to get this detail. They will provide credentials in tool to connect to the SQL Server.
I know how to change the memory
EXEC master.dbo.sp_configure
GO
EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE
GO
EXEC master.dbo.sp_configure 'min server memory', 1024
RECONFIGURE
GO
EXEC master.dbo.sp_configure
GO
But is there any way I can get how much memory is allocated?
You could inquire with the sys.configurations
catalog view - how about this setting here?
SELECT value_in_use
FROM sys.configurations
WHERE name = 'max server memory (MB)'
Is that the value you're looking for?
Or maybe you're looking for the value for 'min server memory (MB)' ? (as the code in your post seems to imply)
Open a PerformanceCounter
object for the Process category instance corresponding to the SQL Server process on the target host machine. Then look for the memory counters:
- Working Set: Shows the current number of bytes in the working set of this process.
- Private Bytes: Shows the current number of bytes that this process has allocated that cannot be shared with other processes.
- Virtual Bytes: Shows the current size, in bytes, of the virtual address space that the process is using.
You can also query SQL Server's own counters, via a PerformanceCounter
object or via sys.dm_os_performance_counters
. The latter though requires you to know how to compute counter values from successive samples, because the view exposes the raw value. the algorithms to compute the value, based on counter type, are documented at WMI Performance Counters Types.
精彩评论