[英]Should a process always consume the same amount of memory if executed in the same way?
Hi folks and thanks for your time in advance. 大家好,谢谢您的宝贵时间。
I'm currently extending our C# test framework to monitor the memory consumed by our application. 我目前正在扩展C#测试框架,以监视应用程序消耗的内存。 The intention being that a bug is potentially raised if the memory consumption significantly jumps on a new build as resources are always tight. 目的是如果由于资源总是紧张而在新的版本上内存消耗大大增加时,可能会引发错误。
I'm using System.Diagnostics.Process.GetProcessByName and then checking the PrivateMemorySize64 value. 我正在使用System.Diagnostics.Process.GetProcessByName,然后检查PrivateMemorySize64值。
During developing the new test, when using the same build of the application for consistency, I've seen it consume differing amounts of memory despite supposedly executing exactly the same code. 在开发新测试的过程中,当使用相同版本的应用程序以保持一致性时,尽管执行了完全相同的代码,但我看到它消耗了不同数量的内存。
So my question is, if once an application has launched, fully loaded and in this case in it's idle state, hence in an identical state from run to run, can I expect the private bytes consumed to be identical from run to run? 所以我的问题是,一旦应用程序启动,完全加载并且在这种情况下处于空闲状态,从而在运行之间处于相同状态,我是否可以期望消耗的私有字节在运行之间是相同的?
I need to clarify that I can expect memory usage to be consistent as any degree of varience starts to reduce the effectiveness of the test as a degree of tolerance would need to be introduced, something I'd like to avoid. 我需要澄清的是,我可以期望内存使用情况保持一致,因为任何程度的差异都会开始降低测试的有效性,因为需要引入一定程度的容忍度,我想避免这种情况。
So... 所以...
1) Should the memory usage be 100% consistent presuming the application is behaving consistenly? 1)假设应用程序表现一致,内存使用率是否应该100%一致? This was my expectation. 这是我的期望。
or 要么
2) Is there is any degree of variance in the private byte usage returned by windows or in the memory it allocates when requested by an app? 2)Windows返回的专用字节使用量或在应用程序请求时分配的内存中是否有任何程度的差异?
Currently, if the answer is memory consumed should be consistent as I was expecteding, the issue lies in our app actually requesting a differing amount of memory. 目前,如果答案是内存消耗应该与我预期的一致,那么问题出在我们的应用程序实际请求的内存量不同。
Many thanks 非常感谢
H H
Almost everything in .NET uses the runtime's garbage collector, and when exactly it runs and how much memory it frees depends on a lot of factors, many of which are out of your hands. .NET中的几乎所有内容都使用运行时的垃圾收集器,确切的运行时间以及释放的内存量取决于许多因素,其中许多是您无法控制的。 For example, when another program needs a lot of memory, and you have a lot of collectable memory at hand, the GC might decide to free it now, whereas when your program is the only one running, the GC heuristics might decide it's more efficient to let collectable memory accumulate a bit longer. 例如,当另一个程序需要大量内存,而您手头有很多可收集的内存时,GC可能会决定立即释放它,而当您的程序是唯一正在运行的程序时,GC启发式方法可能会决定它的效率更高让可收集的内存积累更长的时间。 So, short answer: No, memory usage is not going to be 100% consistent. 因此,简短的回答:不,内存使用率不会保持100%一致。
OTOH, if you have really big differences between runs (say, a few megabytes on one run vs. half a gigabyte on another), you should get suspicious. OTOH,如果两次运行之间确实存在很大差异(例如,一次运行有几兆字节,而另一次运行有几兆字节),则应该变得可疑。
If the program is deterministic (like all embedded programs should be), then yes. 如果程序是确定性的(就像所有嵌入式程序一样),则可以。 In an OS environment you are very unlikely to get the same figures due to memory fragmentation and numerous other factors. 在OS环境中,由于内存碎片和许多其他因素,您不太可能获得相同的数字。
Update: 更新:
Just noted this a C# app, so no, but the numbers should be relatively close (+/- 10% or less). 刚刚注意到这是一个C#应用程序,所以没有,但是数字应该相对接近(+/- 10%或更少)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.