执行命令后 SQL Server 物理内存增长

发布于 2024-08-18 19:35:52 字数 696 浏览 3 评论 0原文

我正在 Windows Server 2008 和 SQL Server 2008 上运行服务器应用程序,现在我的情况如给定。

  1. 我已经实现了自定义连接池(为什么我这样做是另一个很长的故事)。因此我不会在每个请求上打开或关闭连接。

  2. 服务器应用程序使用池中的 10 个可用连接在一分钟内执行数千个 DBCommand。

  3. 执行命令后,我没有清理连接(因为我不知道要清理什么以及如何在不关闭并重新打开它的情况下清理),而且我也没有处理命令对象本身。 。

  4. 当服务器应用程序出现故障时,它会通过调用 close 方法释放所有连接。

现在我观察到,经过 1 小时或 2 个 sqlserver 进程的测试后,内存达到了 3Gig 左右,然后我关闭了我的服务器应用程序,即使占用的内存没有释放或减少(根据任务管理器和资源监视器),毕竟我重新启动后sqlserver释放内存。

现在以下是我的问题。

  1. 我是否需要每次调用DBCommand对象的Dispose,如果是的话会对连接对象产生影响。

  2. 上述问题是导致我观察到的情况还是还有其他原因。

  3. 有没有办法在不关闭连接的情况下清理连接,以及每次 DbCommand 执行后需要清理什么样的垃圾。

谢谢 穆巴沙尔

I am running Server Application on Windows Server 2008 with SQL Server 2008, Now My scenario is as Given.

  1. I have implemented a custom Connection Pooling (why I did that its another long story). therefore I am not opening or closing connection on each request.

  2. Server Application executes more than thousands DBCommand in a minute using 10 connections available in the Pool.

  3. after Execution of the command and I am not cleaning connection(becuase i have no idea what to clean and how to clean without closing and reopening it) and also i am not disposing the Command Object itself.

  4. When server application goes down it releases all the connection by call close method on them.

Now I observed that the After a test of 1 hour or 2 process of sqlserver goes around 3Gig Memory, Then I shut down my server application even then the occupied memory was not released or reduced (according to task manager and Resource Monitor) afterall i restarted sqlserver to release the memory.

Now following are my question.

  1. Do i need to call Dispose of DBCommand object each time, if yes then will it leaves impact on connection object.

  2. Is above mentioned problem is causing what I observed or there are other reasons as well.

  3. Is there any way to clean the connection without closing it and what kind of garbage it need to clean after each DbCommand execution.

Thanks
Mubashar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

横笛休吹塞上声 2024-08-25 19:35:52

然后我关闭了我的服务器应用程序,即使占用的内存没有释放或减少(根据任务管理器和资源监视器)[...]

由于关闭应用程序会处理所有对象,这一观察结果清楚地表明内存丢失问题不是由于您的物品没有被处理而引起的。 (尽管如此,正如 David 正确指出的那样,始终处理 IDisposables 是一种好的做法。)

因此,您应该查看 SQL Server 的内存配置。请注意,如果内存可用,SQL Server 使用该内存是一件好事。未使用的内存是浪费金钱。通常,一旦其他进程需要内存,SQL Server 就会释放内存:

当 SQL Server 动态使用内存时,它会定期查询系统以确定可用的空闲物理内存量。 SQL Server 会根据服务器活动增加或缩小缓冲区高速缓存,以将可用物理内存保持在 4 MB 到 10 MB 之间。这会阻止 Microsoft Windows NT® 4.0 或 Windows® 2000 进行分页。如果可用内存较少,SQL Server 会将内存释放给通常位于空闲列表中的 Windows NT 4.0 或 Windows 2000。如果有更多可用内存,SQL Server 会将内存重新提交到缓冲区高速缓存。 SQL Server仅在其工作负载需要更多内存时才向缓冲区高速缓存添加内存;静态服务器不会增加其缓冲区缓存。

因此,除非您的服务器开始过度交换,否则我不会太担心 SQL Server 内存消耗。

Then I shut down my server application even then the occupied memory was not released or reduced (according to task manager and Resource Monitor) [...]

Since closing your application disposes all objects, this observation make it quite clear that the memory loss problem is not caused by your objects not being disposed. (Nevertheless, as David correctly pointed out, always Disposing IDisposables is good practice.)

Thus, you should look at the memory configuration of your SQL Server. Note that, if memory is available, SQL Server using that memory is a good thing. Unused memory is a waste of money. Usually, SQL Server frees memory once some other process needs it:

When SQL Server is using memory dynamically, it queries the system periodically to determine the amount of free physical memory available. SQL Server grows or shrinks the buffer cache to keep free physical memory between 4 MB and 10 MB depending on server activity. This prevents Microsoft Windows NT® 4.0 or Windows® 2000 from paging. If there is less memory free, SQL Server releases memory to Windows NT 4.0 or Windows 2000 that usually goes on the free list. If there is more memory free, SQL Server recommits memory to the buffer cache. SQL Server adds memory to the buffer cache only when its workload requires more memory; a server at rest does not grow its buffer cache.

So, unless your server starts swapping excessively, I would not worry too much about SQL Server memory consumption.

草莓味的萝莉 2024-08-25 19:35:52

SQL Server 将使用大量内存,直到必要时才会将其归还,因此您应该始终在专用计算机上运行 SQL。如果您确实必须在共享环境中使用最大内存限制,则可以配置最大内存限制,但您的应用程序不会导致 SQL 释放内存。

SQL Server will use lots of memory and not give it back until necessary, hence you should always run SQL on a dedicated machine. You can configure a maximum memory limit if you do have to use it in a shared environment, but there is no way your application will cause SQL to release memory.

回眸一笑 2024-08-25 19:35:52
  1. 始终Dispose() IDispose-ables。

  2. 不知道。

  3. 使用 sp_reset_connection 重置连接,然后将其返回到池中。

  1. Always Dispose() IDispose-ables.

  2. No idea.

  3. Use sp_reset_connection to reset the connection before returning it to the pool.

凑诗 2024-08-25 19:35:52

连接池是受控制的,传递给连接字符串的参数基本上包括以下内容:

· 连接超时

· 最小池大小

· 最大池大小

· 池

如果您发现应用程序在很短的时间内生成大量连接,那么您首先检查应用程序中打开的连接,并确保在使用完它们后立即关闭它们。关闭的连接不会真正关闭,并且可供向服务器发出的另一个请求使用。特别检查您的 DataReader 对象,并在它们脱离上下文后立即将其关闭。有时,开发人员在嵌套循环中使用多个DataReader,并在finally子句中关闭所有DataReader。不要这样做,并在退出循环后立即尝试停止 DataReader。是的,你总是可以在finally子句中再次检查DataReader对象的状态,如果没有关闭,你可以关闭它。

现在检查将 ConnectTimeout 调整到最小值,例如 1 秒。这设置了应用程序关闭的池中连接的生命周期,并将 MaxPoolSize 设置为服务器的最大限制。我猜想,如果没有其他应用程序连接到同一服务器数据库,则该数量将是 100。将 MinPoolSize 值设置为 5,因为您会将 ConnectTimeOut 设置为最小值,在这种情况下,为了使连接池有效工作,您将需要一些连接始终可用。

尝试这些设置和更改。希望这能让您满意。

谢谢,

拉吉夫·兰詹·拉尔

Connection Pooling is controlled and the parameters passed to a connection string that basically comprises the following:

· Connect Timeout

· Min Pool Size

· Max Pool Size

· Pooling

If you see that your application generates a huge number of connections in a very short span, then you first check your application about the open connections and make sure to close them as soon as you finish working with them. The closed connection will not be actually closed and will be available for use by another request to the server. Specially check your DataReader objects and close them as soon as they go out of context. Sometimes, developer uses multiple DataReaders in nested loop and close all the DataReader in finally clause. Don't do this and try to stop the DataReader as soon as you come out of loop. Yes, you can always check the status of a DataReader object in finally clause again, and if not closed, you can close it.

Now check with tweaking the ConnectTimeout to a minimum, say 1 second. This sets the lifespan of a connection in pool closed by the Application, and set the MaxPoolSize to maximum limit of your server. I guess, it would be 100, if no other application would be connected to the same server database. Put MinPoolSize value to 5, as you would set the ConnectTimeOut to a minimum and in this case, connection pooling to work efficiently, you would need some connections always available.

Try these settings and changes. Hope this will work to your satisfaction.

Thanks,

Rajeev Ranjan Lall

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文