question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Marshalling of byte[] arrays is causing excessive GC load (and gen2 collections)

See original GitHub issue

My app was serving a large file (~2 GB) through Dokan and I was noticing a gen2 garbage collection every second. This is obviously not good for performance - I was seeing 30% of CPU spent in GC. After investigating, it turns out the allocations were coming from marshaling of the byte[] array in ReadFile(). A managed array was being allocated for every ReadFile() call and thrown away after the call completes.

Today’s code looks like this:

public delegate NtStatus ReadFileDelegate(
    [MarshalAs(UnmanagedType.LPWStr)] string rawFileName,
    [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2), Out] byte[] rawBuffer,
    uint rawBufferLength,
...
);

By modifying it to not marshal at all and just pass the IntPtr I was able to completely eliminate the GC load. The implementor of IDokanOperations can then use Marshal.Copy or unsafe code to fill the output.

This is obviously a breaking interface change, but maybe it’s worth adding a new interface, something like IDokanOperationsUnsafe, for those that want the perf?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
hans-olavcommented, Sep 27, 2018

See PR #206 for the implementation of this feature.

0reactions
hans-olavcommented, Oct 1, 2018

#206 merged to master, closing issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Big byte array will cause memory leak
It should be collected by GC. "So, when you go to load a 300MB file into memory, which is larger than the heap...
Read more >
To Heap or not to Heap; That's the Large Object Question?
In this article we explored a theory; would it be better to not utilize a heap construct when dealing with very large objects...
Read more >
What are the GC implications of having a large array ...
They will not affect allocation. They will affect Gen2 collection though, but if GC is doing Gen2 collection, then you have a different...
Read more >
NET Garbage Collection. A Super-Simplified Explanation.
What Triggers a Collection?Permalink · An attempt to allocate exceeds threshold for a generation or the large object heap · A call to...
Read more >
Under the Hood of .NET Memory Management
As mentioned earlier, this is faster than a full collection because, as part of the collection process, the GC only needs to inspect...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found