程序中方法的进程详细信息(CPU 时间、分配的内存)
我想计算整个程序中为特定方法分配的CPU时间和内存。我可以获得整个程序的 CPU 时间和内存。如何获取特定方法的详细信息。
另外,如何在从磁盘获取值/信息的同时让程序对辅助存储进行磁盘访问。
我尝试创建线程并使该方法由创建的线程运行,但它不起作用。它给出了完整过程的信息。我正在寻找具体的方法。
添加更多细节希望这有助于理解这个问题。
好的。我正在编写 C# 代码
public class Program
{
public static void Main(string[] args)
{
**checkPoint_1_start**
//
// Some code here
//
**checkPoint_2_start**
calling_Method();
**checkPoint_2_end**
//
// Some code here
//
**checkPoint_1_end**
}
public void calling_Method()
{
//
// Code of this method is very long and contains many variables.
//
}
}
我能够找到从 checkPoint_1_start 到 checkPoint_1_end 使用的内存。我想如何找到仅在checkPoint_2_start和checkPoint_2_end之间使用的内存。
我使用以下内容来查找内存使用情况的详细信息,它给出了从 checkPoint_1_start 到 checkPoint_1_end 的整个过程。如何获取 checkPoint_2_start 和 checkPoint_2_end 之间使用的内存。
私有内存:System.Diagnostics.Process.GetCurrentProcess().PrivateMemorySize64 / 1024 工作集内存:System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 / 1024 分配内存:GC.GetTotalMemory(false) / 1024
I want to calculate the CPU time and the memory allocated for a specific method in the whole program. I am able to get the CPU Time and the memory for the complete program. How to get the details for a specific method.
Also how to get the disk access the program made to the secondary storage while getting the values/information from the disk.
I tried to make the thread and make the method run by the created thread, but it didn't work. It is giving the information of the complete process. I am looking for a specific method.
Adding more details hope this helps in understanding the question.
ok. i am writing a C# code
public class Program
{
public static void Main(string[] args)
{
**checkPoint_1_start**
//
// Some code here
//
**checkPoint_2_start**
calling_Method();
**checkPoint_2_end**
//
// Some code here
//
**checkPoint_1_end**
}
public void calling_Method()
{
//
// Code of this method is very long and contains many variables.
//
}
}
I am able to find the memory used from checkPoint_1_start to checkPoint_1_end. I want to how to find the memory used only between the checkPoint_2_start checkPoint_2_end.
I am using the below things to find the details of memory usage, it is giving for the entire process from checkPoint_1_start to checkPoint_1_end. How to get memory used between the checkPoint_2_start checkPoint_2_end.
Private Memory: System.Diagnostics.Process.GetCurrentProcess().PrivateMemorySize64 / 1024
Working set Memory: System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 / 1024
Allocated Memory: GC.GetTotalMemory(false) / 1024
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 checkPoint_2_start checkPoint_2_end (或您需要的指标的任何其他函数)处使用 GC.GetTotalMemory(false) 。这两个检查点之间使用的额外内存量是 checkPoint_2_end 返回的值减去 checkPoint_2_start 返回的值。
如果您只想要单个线程的内存使用情况,我认为您不能这样做。您必须以某种方式跟踪您的所有分配。
使用 Process.TotalProcessorTime 作为 CPU 时间。
Use GC.GetTotalMemory(false) at checkPoint_2_start checkPoint_2_end (or any other function for the metric you need). The additional amount of memory used between those two checkpoints is the value returned at checkPoint_2_end, minus the value returned at checkPoint_2_start.
If you want only the memory usage for a single thread, you cannot do that I think. You would have to track all your allocations somehow.
Use Process.TotalProcessorTime for CPU time.