编程基础 垃圾回收

什么是垃圾回收? (What is Garbage Collection?)

In general layman's terms, Garbage collection (GC) is nothing but collecting or gaining memory back which has been allocated to objects but which is not currently in use in any part of our program.

用一般的外行术语来说,垃圾回收(GC)就是回收或获取已分配给对象的内存,但该内存当前未在程序的任何部分中使用。

Let's get into more detail. Garbage collection is the process in which programs try to free up memory space that is no longer used by objects.

让我们更详细地讲。 垃圾收集是程序尝试释放对象不再使用的内存空间的过程。

Garbage collection is implemented differently for every language. Most high-level programming languages have some sort of garbage collection built in. Low-level programming languages may add garbage collection through libraries.

每种语言对垃圾收集的实施方式都不同。 大多数高级编程语言都内置了某种垃圾回收。低级编程语言可能会通过库添加垃圾回收。

As said above, every programming language has their own way of performing GC. In C programming, developers need to take care of memory allocation and deallocation using malloc() and dealloc() functions. But, in the case of C# developers don't need to take care of GC and it’s not recommended either.

如上所述,每种编程语言都有其自己的执行GC的方式。 在C编程中,开发人员需要使用malloc()和dealloc()函数来处理内存分配和释放。 但是,就C#而言,开发人员无需照顾GC,也不建议这样做。

内存分配如何发生? (How does memory allocation happen?)

In C#, memory allocation of objects happens in a managed heap, which is taken care of by CLR (common language runtime). Memory allocation for the heap is done through win32 dll in OS and similarly in C.

在C#中,对象的内存分配发生在托管堆中,这由CLR(公共语言运行时)负责。 堆的内存分配是通过OS中的win32 dll和类似的C语言完成的。

But, in C objects are placed in memory wherever there is free space that fits the size of the object. Also, memory mapping works based on Linkedlist concepts. In C#, memory allocation for the heap happens in a linear manner, one after another.

但是,在C语言中,只要有适合对象大小的可用空间,它们就会被放置在内存中。 同样,内存映射基于Linkedlist概念工作。 在C#中,堆的内存分配以线性方式进行,一个接一个。

Whenever a new object is being created, memory is allocated in the heap and the pointer is moved to the next memory address. Memory allocation in C# is faster than in C. This is because in C the memory needs to search and allocate for the object. So it will take a bit more time than C#.

每当创建新对象时,就会在堆中分配内存,并将指针移至下一个内存地址。 C#中的内存分配比C中的内存分配快。这是因为在C中,内存需要搜索并分配对象。 因此,它将比C#花费更多时间。

C GC中的世代 (Generations in C GC)

In .net programming, the heap has three generations called generations 0, 1, and 2. Generation 0 gets filled first whenever a new object is created. Then the garbage collector runs when Generation 0 gets filled. Newly created objects are placed in Generation 0.

在.net编程中,堆具有三个世代,分别称为世代0、1和2。每当创建新对象时,世代0就会首先填充。 然后,当第0代被填满时,垃圾收集器将运行。 新创建的对象放置在第0代中。

While performing garbage collection all the unwanted objects are destroyed, and so memory gets freed and compacted. GC takes care of pointing the pointers of freed memory once GC happens.

在执行垃圾回收时,所有不需要的对象都会被破坏,因此内存将被释放和压缩。 一旦发生GC,GC将负责指向释放的内存的指针。

Generations 1 and 2 contain objects which have longer lifetimes. GC on generations 1 and 2 will not happen until generations 0 has sufficient memory to allocate.

第1代和第2代包含寿命更长的对象。 直到第0代具有足够的内存来分配时,第1代和第2代的GC才会发生。

You shouldn't invoke GC programmatically. It’s good to let it happen on its own. GC gets call whenever generation 0 gets filled.

您不应该以编程方式调用GC。 最好让它自己发生。 每当生成0时,GC都会调用。

GC的优缺点 (Pros and Cons of GC)

Garbage collection is a tool that saves time for programmers. For example it replaces the need for functions such as malloc() and free() which are found in C. It can also help in preventing memory leaks.

垃圾回收是一种为程序员节省时间的工具。 例如,它取代了C语言中对诸如malloc()和free()之类的函数的需求。它还有助于防止内存泄漏。

The downside of garbage collection is that it has a negative impact on performance. GC has to regularly run though the program, checking object references and cleaning out memory. This takes up resources and often requires the program to pause.

垃圾回收的不利之处在于它会对性能产生负面影响。 GC必须定期运行程序,检查对象引用并清除内存。 这会占用资源,并且经常需要程序暂停。

什么时候做 (When to do it)

If an object has no references (is no longer reachable) then it is eligible for garbage collection.

如果对象没有引用(不再可访问),则可以进行垃圾回收。

For example in the Java code below, the Thing object originally referenced by ‘thing1’ has its one and only reference redirected to another object on the heap. This means it is then unreachable and will have its memory unallocated by the garbage collector.

例如,在下面的Java代码中,最初由“ thing1”引用的Thing对象将其引用唯一地重定向到堆上的另一个对象。 这意味着它是不可达的,垃圾回收器将无法分配其内存。

class Useless {public static void main (String[] args) {Thing thing1 = new Thing();Thing thing2 = new Thing();thing2 = thing1; // direct thing2's reference towards thing1// no references access thing2
} }

One example of garbage collection is ARC, short for automatic reference counting. This is used in Swift, for example. ARC boils down to keeping track of the references to all objects that are created. If the amount of references drops to 0, the object will be marked for deallocation.

垃圾收集的一个示例是ARC,它是自动引用计数的缩写。 例如,这在Swift中使用。 ARC归结为跟踪对创建的所有对象的引用。 如果引用数量降至0,则该对象将被标记为释放。

更多信息: (More Information:)

  • https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals - To know more about garbage Collection

    https://docs.microsoft.com/zh-cn/dotnet/standard/garbage-collection/fundamentals-了解有关垃圾收集的更多信息

翻译自: https://www.freecodecamp.org/news/a-guide-to-garbage-collection-in-programming/

编程基础 垃圾回收

编程基础 垃圾回收_编程中的垃圾回收指南相关推荐

  1. python编程基础知识体系_最新版 17 幅思维导图:Python 编程之核心知识体系

    原标题:最新版 17 幅思维导图:Python 编程之核心知识体系 导读:本文主要涵盖了 Python 编程的核心知识,展示了一系列思维导图,主要就 Python 核心基础知识进行了细致梳理.无论你是 ...

  2. 迈入JavaWeb第一步,Java网络编程基础,TCP网络编程URL网络编程等

    文章目录 网络编程概述 网络通信要素 要素一IP和端口号 要素二网络协议 TCP网络编程 UDP网络编程 URL网络编程 Java网络编程基础 网络编程概述 Java是Internet上的语言,它从语 ...

  3. c语言编程基础心得,C语言编程学习心得体会

    C语言是在国内外广泛使用的一种计算机语言.其语言功能丰富.表达能力强.使用灵活方便.既具有高级语言的优点,又具有低级语言的许多特点,适合编写系统软件.本文是C语言编程学习心得,希望对大家有帮助. C语 ...

  4. 编程就是python吗_编程python是什么

    编程python是什么? Python 是一门有条理的和强大的面向对象的程序设计语言,Python 已经成为最受欢迎的程序设计语言之一,本文带你简单入门Python编程基础. 推荐:<Pytho ...

  5. 编程猫python怎么样_编程猫怎么样?没学过编程的家长如何给孩子选课?一张图讲清楚...

    前不久鱼sir被一位家长问到:这么小的孩子真的有必要学编程课吗? 其实,鱼sir认为,学习Python语言一定程度上确实能够提升孩子的逻辑思维能力,还能让孩子从一个个编程作品中享受到乐趣. 但编程课也 ...

  6. 简述python垃圾回收机制_python中的垃圾回收机制简述

    2020年12月5日21:47:35 王凯玉 python中的垃圾回收机制 引用计数 # 引用计数 引用计数是编程语言中的一中内存管理技术,可以将资源的被引用次数保存起来. 当引用计数为0时,资源将被 ...

  7. java不同垃圾回收器_细述 Java垃圾回收机制→Types of Java Garbage Collectors

    本文非原创,翻译自Types of Java Garbage Collectors 在Java中为对象分配和释放内存空间都是由垃圾回收线程自动执行完成的.和C语言不一样的是Java程序员不需要手动写垃 ...

  8. python网络编程基础语法_python网络编程

    知识内容: 1.socket语法及相关 2.黏包 3.struct模块 4.subprocess模块 5.socketserver模块 6.验证客户端连接的合法性 参考: 一.socket语法及相关 ...

  9. WebFlux响应式编程基础之 2 函数式编程 工具jclasslib bytecode viewer

    函数式编程:告诉他的功能是什么,而不是告诉他怎么做 命令式编程:怎么去做 函数式编程:不需要关注细节,利用系统已经有的API 使用jdk8自带函数接口的好处 函数接口减少接口定义 函数式接口链式操作 ...

最新文章

  1. Uva10191 复合词
  2. 如何在JavaScript中反转字符串?
  3. kubernetes log 流式数据处理
  4. 御术:比能力更重要的是你的底层操作系统
  5. QQ相册后台存储架构重构与跨IDC容灾实践
  6. Sharepoint学习笔记—Site Definition系列-- 3、创建ListDefinition
  7. Java 反射机制你还不会?那怎么看 Spring 源码?
  8. Quartz.NET 入门
  9. 编程语言 - PHP
  10. linux跳过文件系统检查,centos 文件系统检测错误
  11. HTML: 和 是何方神圣
  12. MTK驱动代码流程介绍:
  13. STM8S103K3和STM8S105K4原理图
  14. JSON格式校验工具
  15. AVR单片机ATMEGA16编译软件ICCAVR的使用技巧与应用示例
  16. excel怎么设置自动计算_EXCEL内输入起始时间,如何自动计算小时时间差?
  17. 常用网络ip地址有哪些
  18. 双稳态电子开关、单按键自锁电路仿真
  19. Android 基于Zxing的扫码功能实现(二)
  20. java基础笔记系列_Day04

热门文章

  1. 【今日CV 视觉论文速览】14 Nov 2018
  2. 【java web】java执行预编译Groovy脚本
  3. 例子 冒泡排序五位数版本 理解过程抓捕数据
  4. HTML演练 0917 需求说明 我喜欢的影视剧
  5. git-文本内容的回退-缓冲区退到工作区-工作区改动后改为改动前
  6. django-QueryDict对象
  7. linux批量替换文件夹中所有文件内容
  8. mysql数据库引擎InnoDB和MyISAM
  9. 建站利器 | 阿里巴巴上线静态开源站点搭建工具 Docsite
  10. 创建局域网内远程git仓库,并将本地仓库push推到远程仓库中