如何减少 Socket 应用程序的 C# 内存使用量
我在Windows中开发了一个简单的UDP消息服务器和客户端应用程序,服务器可以向客户端发送消息,但客户端不能发送任何内容,它们只是监听。问题是客户端应用程序使用相当大的内存使用量,在侦听时大约为 7M,在接收数据包时为 9M。我可以将内存使用量减少到至少 1M 以下吗?
I developed a simple UDP message server and client application in Windows, the server can send a message to the client but the client can't send anything, they are only listening. the problem is the client application is use quite big memory usage is about 7M when it's listening and 9M when it received a packet. Could I reduce the memory usage into at least less then 1M?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您如何测量您的内存占用量?任何托管 .net 应用程序(即使是最小的应用程序)通常也具有大约 50 MB 的共享工作集,应用程序的实际内存占用量远小于此值。
您是否尝试过调用GC.GetTotalMemory来查看实际托管内存使用情况?
How are you measureing your memory footprint? Any managed .net application, even the smallest typically has a shared working set of around 50 MBs, the actual memory footprint of your app is much smaller than that.
Have you tried calling GC.GetTotalMemory to look at the actual managed memory usage?
这种负担的大部分是运行整个 CLR 系统、垃圾收集等的开销。如果您对内存占用(<10-20Mb)超级敏感,那么 CLR 可能不适合您。即使是基本的 HelloWorld 私有工作集也需要超过 4Mb 的 RAM。
如果您对占用空间很敏感,那么寻找真正的提前编译语言(如 C/C++ 等)可能会是最好的选择。
Much of this burden is the overhead of running the whole CLR system, garbage collection etc. If you're super-sensitive to memory footprint (<10-20Mb) then the CLR may not be for you. Even a basic HelloWorld private working set is over 4Mb of RAM.
If you are sensitive to footprint, you might be best served by looking to a true ahead-of-time compiled language like C/C++ etc.