编程基础 垃圾回收_编程中的垃圾回收指南

 2023-09-06 阅读 21 评论 0

摘要:编程基础 垃圾回收 什么是垃圾回收? (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 par

编程基础 垃圾回收

什么是垃圾回收? (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/

编程基础 垃圾回收

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/7125.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息