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. 英语gzibeads天珠gzibeads单词

    天珠英语是gZiBeads,藏语叫(si , 斯)汉语译为“斯”或“瑟”,又称“天降石”.在<藏汉大辞典>里天珠的解释为:“亚玛瑙,猫睛石,一种宝石,俗称九眼珠.入药能治脑溢血”.最早的天 ...

  2. Shell 冒泡排序

    举例 #!/bin/bash echo "please input a number list:" read -a arrs for((i=0;i<${#arrs[@]};i ...

  3. 5.2 odex文件

    odex是OptimizedDEX的缩写,是优化过的dex文件 odex两种存在方式: 1. 从apk程序中提取,和apk文件放在一起,后缀 odex,此类文件多是AndroidRom系统文件 2.  ...

  4. 一步一步实现kbmmw的httpsys使用https功能

    kbmmw的httpsys的功能已经实现了好长时间,但是现在各个平台都要求使用https来提供服务. 今天一步一步来说一下如何使用kbmmw 的httpsys功能支持https. 首先为了获得证书,我 ...

  5. Golang: 读写之外的其他文件操作

    在上一篇文章中,我们介绍了常用的文件读写操作,今天接着来研究一下,除了读写以外的其他常见文件操作. 一.创建目录: package main import ( "fmt" &quo ...

  6. Kali linux network is unreachable

    使用树莓派4 安装完kali linux后,配置好ip,能ping通上级路由. 但ping公网地址显示网络不可达. Ping: connect: network is unreachable 解决方法 ...

  7. Go编程基础(介绍和安装)

    Michaelhbjian 2018.10.07 19:41 字数 892 阅读 317评论 0喜欢 0 Go(又称Golang[3])是Google开发的一种静态强类型.编译型.并发型,并具有垃圾回 ...

  8. 爬取前程无忧网站上python的招聘信息。

    本文获取的字段有为职位名称,公司名称,公司地点,薪资,发布时间 创建爬虫项目 scrapy startproject qianchengwuyou cd qianchengwuyou scrapy g ...

  9. 四川第十届省赛 A.Angel Beats bitset

    四川第十届省赛 A.Angel Beats bitset 题目链接 题解参考:http://www.cnblogs.com/Aragaki/p/9142250.html 考虑用bitset来维护对于所 ...

  10. Locust性能测试7-分布式执行

    前言 使用Locust进行性能测试时,当一台单机不足以模拟所需的用户数量的时候,可以在多台机器上分布式的执行性能测试. locust分布式启动场景有2种,一种是单机设置master和slave模式,另 ...