khalidabuhakmeh,
@khalidabuhakmeh@mastodon.social avatar

Hey folks, I need your help.

In your opinion, what are the core concepts of memory management in the .NET space?

Please boost for reach.

darrel_miller,
@darrel_miller@mastodon.social avatar

@khalidabuhakmeh I see you are getting a whole lot of sophisticated suggestions. I am here for the more fundamental issues I see. e.g. Don't use ReadAsStringAsync and then Deserialize<T>(string)

khalidabuhakmeh,
@khalidabuhakmeh@mastodon.social avatar

@darrel_miller this is a solid tip

joaofbantunes,
@joaofbantunes@mastodon.social avatar

@khalidabuhakmeh inspired by this "back to basics" tip: don't call ToList/ToArray blindly everywhere, creating a crap ton of collection copies.

For example when R#/Rider warns about possible multiple enumerations of an IEnumerable, folks response to it is adding a ToList/ToArray in there, very likely, unnecessarily.

mfe_,

@darrel_miller @khalidabuhakmeh I dont understand. What should be used instead of ReadAsStringAsync and then Deserialize<T>(string)?

skolima,
@skolima@hachyderm.io avatar

@mfe_ @darrel_miller @khalidabuhakmeh Deserialize directly from the stream/span.

darrel_miller,
@darrel_miller@mastodon.social avatar

@skolima @mfe_ @khalidabuhakmeh

Sorry about that, I should have been clear to my reasoning. Also note, a while back @davidfowl had a great insight on this topic about the fact that if the response size was more than 80K then that string would end up on the large object heap. It doesn't take much to make 80KB of JSON payload.

mfe_,

@darrel_miller @skolima @khalidabuhakmeh @davidfowl thanks for the explanation

peteri,

@khalidabuhakmeh
Concepts wise:
GC phases
Large Object Heap
Events raised by the GC system
Memory dumps
Bonus points if I'm interviewing someone and they mention the GC card table (although I've never really found a use for that knowledge).
Watch out for Linq / Closures burning memory.

vekzdran,
@vekzdran@hachyderm.io avatar

@khalidabuhakmeh

  1. Understand GC, basics at least, collection generations
  2. Understand work modes of GC server vs worker and bg processing
  3. Configuring GC with ton of its flags
  4. Reference and value types in C#
  5. What "zero allocation means", test in practice with some sample code (I'd love to see this from both .NET docs and JBR docs)
  6. Understand that the compiler will generate more code than the user writes eg. async state machine, closures
  7. Use memory profiler & snapshots
ICooper,
@ICooper@hachyderm.io avatar

@khalidabuhakmeh

Allocations:

  • What is allocated where: stack vs heap.

Collection:

  • What happens during Garbage Collection (generations, unmanaged vs. manged resources, performance impact etc.)

Availability & Performance:

  • How do we know if allocation => collection has become a performance or availability issue?
  • How do we locate the specific problem?
  • How can we reduce allocations.
csnetprogrammer,
@csnetprogrammer@awscommunity.social avatar

@khalidabuhakmeh Garbage Collector is King. All Hail.

khalidabuhakmeh,
@khalidabuhakmeh@mastodon.social avatar

@csnetprogrammer which one?

codemonkey85,
@codemonkey85@mastodon.social avatar

@khalidabuhakmeh Honestly, I’ve been a .NET developer for over 8 years and I’ve never been taught to (or felt a compelling need to) think hard about memory, aside from some occasional dispose patterns. Sometimes I come back to this topic and feel like I’m missing a big chunk of important things.

khalidabuhakmeh,
@khalidabuhakmeh@mastodon.social avatar

@codemonkey85 that's fair. @maartenballiauw and I joke that folks only want to learn about memory when their house is burning down. 😅

khalidabuhakmeh,
@khalidabuhakmeh@mastodon.social avatar

@codemonkey85 @maartenballiauw I will say though, .NET is typically pretty good at keeping you safe most of the time and it's getting better.

Also tooling is better to flag really big problems early on. (Shameless plug for JetBrains products)

codemonkey85,
@codemonkey85@mastodon.social avatar

@khalidabuhakmeh Yeah, I do pay attention to ReSharper and Roslyn suggestions too.

devlead,
@devlead@mastodon.social avatar

@khalidabuhakmeh there's some good reading on JetBrains dotMemory page 😎

https://www.jetbrains.com/help/dotmemory/NET_Memory_Management_Concepts.html

khalidabuhakmeh,
@khalidabuhakmeh@mastodon.social avatar

@devlead I've been reading a lot of memory stuff (including JetBrains stuff) but I wanted your opinion. The opinion of the people.

glennsills,
@glennsills@dotnet.social avatar

@khalidabuhakmeh @devlead Sorry, I forgot my opinion.

khalidabuhakmeh,
@khalidabuhakmeh@mastodon.social avatar

@glennsills @devlead your opinion is "Khalid is great and deserves all my money."

Wow, I'm flattered. I'll look forward to your lathe novelty-sized check.

devlead,
@devlead@mastodon.social avatar

@khalidabuhakmeh got opinions, but it varies greatly on type of app, mostly on how long process is running, a console app and just runs and dies less worried about memory management. Strategies varies based on how many instances per "user" too, i.e. one instance serving many over time can cache things more aggressively, whereas cache less beneficial for many instances serving few.
But in general the fewer objects you new up, the less memory you use.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • dotnet
  • rosin
  • Youngstown
  • khanakhh
  • ngwrru68w68
  • slotface
  • ethstaker
  • mdbf
  • everett
  • kavyap
  • DreamBathrooms
  • thenastyranch
  • cisconetworking
  • magazineikmin
  • Durango
  • megavids
  • GTA5RPClips
  • Leos
  • tester
  • tacticalgear
  • InstantRegret
  • normalnudes
  • osvaldo12
  • cubers
  • anitta
  • modclub
  • provamag3
  • JUstTest
  • lostlight
  • All magazines