2

I have an unmanaged method that when executed takes high CPU. Is it safe to say that unmanaged calls naturally take high CPU?

Following is the code:

 public void ReadAt(long ulOffset, IntPtr pv, int cb, out UIntPtr pcbRead)
 {
        Marshal.Copy(buffer, 0, pv, bytesRead);
        pcbRead = new UIntPtr((uint)bytesRead);
        bytesRead = 0;

        if (streamClosed)
            buffer = null;
 }       
3
  • 10
    Rarely is it safe to make general statements like that. CPU usage is determined more by what the method is doing, rather than whether it's managed or unmanaged code that is being called. In this case, it's also likely that marshalling the data is causing some CPU utilization. Why are you optimizing this? Dec 24, 2010 at 6:11
  • A client has reported high CPU usage of almost 80-90 % in his environment. When profiled, we monitored that this method was taking almost 60% of total CPU usage.
    – Faisal
    Dec 24, 2010 at 6:31
  • 2
    Without the context provided by the rest of your code, that tells us almost nothing. Are you calling this method in a tight loop? What does this method do in your application? What is its function? It's difficult to propose alternative approaches without such information. Dec 24, 2010 at 7:09

1 Answer 1

2

No it's not safe to generalize this. Both managed and unmanaged methods take whatever CPU they need to execute their code.

When someone says unmanaged calls may be expensive they usually mean the overhead from switching between managed and unmanaged. This particular cost will only matter if you do unmanaged calls in tight loops like per-pixel processing on a large image.

Some of the overhead of unmanaged calls can be removed by proper attributes, in particular it is possible to move the security checks from per-call to assembly-load-time. This is of course already done for all unmanaged functions in the .NET framework.

The best guess (without more context) about why you are spending so much time in that function is that you are either (a) copying a very large array or (b) you are calling the method very often in a loop.

In the first case the overhead from switching between managed-unmanaged for Marshal.Copy is negligible, copying a large memory block will always saturate the CPU (ie. 100% usage of one core). There is nothing you can do except eliminating the copy operation completely (which may or may not be possible, depending on how you use the buffers).

If you are in the second case and the arrays are very small it may be worth switching to a purely managed loop. But don't do this without measuring, it's easy to guess wrong, and the unmanaged implementation of Marshal.Copy can pull more tricks than the managed JIT to make up for the overhead.

PS: You might want to read this - high CPU usage by itself is no bad thing, the computer is just trying to get things done as fast as possible. Managed or unmanaged does not matter, if your usage is below 100% (per core) it just means your computer got nothing to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.