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.

Why you need to understand garbage collection的更多相关文章

  1. How Garbage Collection Really Works

    Java Memory Management, with its built-in garbage collection, is one of the language's finest achiev ...

  2. Java Garbage Collection Basics--转载

    原文地址:http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html Overview Purpose ...

  3. The Impact of Garbage Collection on Application Performance

    As we’ve seen, the performance of the garbage collector is not determined by the number of dead obje ...

  4. Garbage Collection Optimization for High-Throughput and Low-Latency Java Applications--转载

    原文地址:https://engineering.linkedin.com/garbage-collection/garbage-collection-optimization-high-throug ...

  5. Unity性能优化(3)-官方教程Optimizing garbage collection in Unity games翻译

    本文是Unity官方教程,性能优化系列的第三篇<Optimizing garbage collection in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...

  6. AutoReleasePool 和 ARC 以及Garbage Collection

    AutoReleasePool autoreleasepool并不是总是被auto 创建,然后自动维护应用创建的对象. 自动创建的情况如下: 1. 使用NSThread的detachNewThread ...

  7. [翻译]Java垃圾收集精粹(Java Garbage Collection Distilled)

    source URL: http://www.infoq.com/articles/Java_Garbage_Collection_Distilled Name: Java Garbage Colle ...

  8. [Java] 垃圾回收 ( Garbage Collection ) 的步骤演示

    关于 JVM 垃圾回收机制的基础内容,可参考上一篇博客 垃圾回收机制 ( Garbage Collection ) 简介 上一篇博客,介绍了堆的内存被分为三个部分:年轻代.老年代.永生代.这篇博文将演 ...

  9. [Java] 垃圾回收机制 ( Garbage Collection ) 简介

    自动垃圾回收( Automatic Garbage Collection ) 自动垃圾回收,是指在堆(Heap)内存上分辨哪些对象还在被使用,哪些对象没有被使用,并清除没有被使用的对象.所以,这里的垃 ...

随机推荐

  1. Python如何去实际提高工作的效率?也许这个会有用!

    4月初,班主任的某次周会议上,华华关切的问了一下:最近班主任们有什么难题吗?就是花费了你们大部分时间的工作!我们Python天团可以帮你们解决问题. 班主任大主管星星说:有.目前有一个大难题.我们每天 ...

  2. IVS_原理

    智能视频分析技术指计算机图像视觉分析技术,是人工智能研究的一个分支,它在图像及图像描述之间建立映射关系,从而使计算机能够通过数字图像处理和分析来理解视频画面中的内容.智能视频分析技术涉及到模式识别.机 ...

  3. python绘图 转

    Python有很多可视化工具,本篇只介绍Matplotlib. Matplotlib是一种2D的绘图库,它可以支持硬拷贝和跨系统的交互,它可以在Python脚本.IPython的交互环境下.Web应用 ...

  4. MySQL--mysqldmp命令参数set-gtid-purged

    在MySQL 5.7版本中使用mysqldump导出数据时,如果未显式指定set-gtid-purged参数,会报下面错误: Warning: A partial dump from a server ...

  5. Mac OS 查看端口和杀死进程

    查看sudo lsof -i:port (端口号) sudo lsof -i:9501 COMMAND PID USER FD TYPE DEVICESIZE/OFF NODE NAME php 77 ...

  6. Linux命令groupadd

    groupadd [选项] 组 创建一个新的组.Groupadd命令使用命令行中指定的值加上系统默认值创建新的组账户.新组将根据需要输入系统. (1).选项 -f,--force 如果指定的组已经存在 ...

  7. win7 创建vhd提示“找不到指定文件的虚拟磁盘支持提供程序”解决方法

    本文参照:https://itbbs.pconline.com.cn/diy/16509116_2.html 14楼中"坚持不放弃"用户的答案得到解决 问题复现: 1.window ...

  8. Unity 渲染教程(三):使用多张纹理贴图

    对多个纹理进行采样 应用一张细节贴图 在线性空间中处理颜色 使用一张splat纹理 这是关于渲染的教程系列的第三部分. 前面的部分介绍了着色器和纹理. 我们已经看到如何使用单个纹理来使平坦表面看起来更 ...

  9. dfs 解决(隐式)图搜索问题

    132. 单词搜索 II 中文 English 给出一个由小写字母组成的矩阵和一个字典.找出所有同时在字典和矩阵中出现的单词.一个单词可以从矩阵中的任意位置开始,可以向左/右/上/下四个相邻方向移动. ...

  10. cmake 编译windows程序

    cmake 编译windows程序 cmake 编译windows程序 cmake 编译windows程序 尽量使用  尽量使用 尽量使用 https://www.cnblogs.com/liujia ...