Why you need to understand garbage collection

I’ve been interviewing lots of C# developers recently, and one of my stock questions is “how does the .NET garbage collector know when you’ve finished using an object?” The reason I ask is this because having an understanding of the underlying workings of .NET such as the garbage collector generally indicates that the candidate is a good developer. It shows that they care about how the tools and technologies they are using actually work, and this enables them to write better code.

However, I have been surprised at how many .NET developers, even ones who give good answers to other technical questions, really seem to struggle with this topic. This is particularly noticeable with younger C# developers – those who have only used garbage collected languages in their entire career.

I have a theory as to why this is: when older developers who got started in C/C++ started to try out garbage collected languages like C# and Java, we were initially very suspicious. Freeing memory was such a critical part of how we were used to writing code, that we wanted to know exactly how the garbage collector knew what to collect, when it would run, and we even wanted to take control ourselves and force it to run on demand.

But people who have only ever programmed with garbage collected languages have not been scarred by the horrors of debugging memory leaks, or dereferencing pointers to already freed memory. And so there isn’t such a strong incentive to delve into the inner workings of the garbage collector.

This is unfortunate, as having a solid understanding of the garbage collector is extremely useful when trying to write high performing code. It can be particularly valuable in applications like digital audio, where you are working at low latencies in real-time, and even a small delay due to the garbage collection could cause a glitch in playback. You learn techniques such as reusing byte arrays rather than repeatedly creating new ones, in order to minimise the amount of work the garbage collector needs to do. It will also help you understand why certain objects need to be “pinned” to allow you to use them in conjunction with unmanaged code.

So if you’re a C# developer and you don’t yet have a good understanding of what the garbage collector is and how it works, why not do yourself a favour and educate yourself a bit? It won’t take a huge amount of time, and there are plenty of good articles on the subject. And if you’ve got a Pluralsight subscription (which I highly recommend!), then my friend Elton has a whole module explaining the garbage collector in his IDisposable best practices course.

最新文章

  1. [PHP源码阅读]array_pop和array_shift函数
  2. Java Web 项目获取运行时路径 classpath
  3. hdu 5934 Bomb
  4. js 各种数值类型正则匹配检测
  5. hiho #1050 : 树中的最长路 树的直径
  6. 修饰模式(Decorator结构型)C#简单例子
  7. Vim 第一天
  8. ASP.NET 给Web中的网页添加Loading进度条形式
  9. MongoDB远程维护客户端工具的使用!
  10. [NOIP2014]飞扬的小鸟 D1 T3 loj2500 洛谷P1941
  11. Java反射、反射练习整理
  12. nginx 正则及rewrite常用规则实例
  13. webpack4 系列教程(二): 编译 ES6
  14. HBase官方文档 之 Region的相关知识
  15. C#利用QRCoder生产二维码
  16. C++ 复习要点、面试常见问题总结
  17. HTML/CSS学习(二)
  18. 安装 vagrant homestead步骤
  19. Java 判断是否包含指定的子串 contains()
  20. UISwitch的常见属性

热门文章

  1. Beego 学习笔记11:文件的上传下载
  2. Vue学习之过滤器和自定义指令小结(三)
  3. .Net core 如何生成Nuget包
  4. Ubuntu中如何为wine程序创建快捷方式
  5. go build -tags 的使用
  6. 【Netty】初识Netty
  7. c#时间与时间戳互转13位
  8. PAT 乙级 1004.成绩排名 C++/Java
  9. input事件在进行模糊搜索时,用到的即时监测input的值变化的方法(即时搜索的input和propertychange方法)
  10. egg 连接 mysql 的 docker 容器,报错:Client does not support authentication protocol requested by server; consider upgrading MySQL client