垃圾收集与非垃圾收集编程语言

Garbage collection vs. non garbage collection programming languages(垃圾收集与非垃圾收集编程语言)

本文介绍了垃圾收集与非垃圾收集编程语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以如果我理解得很好,垃圾收集会自动释放程序不再使用的对象.就像java中的垃圾收集器.

So if I understand well, Garbage collection automatically deallocates objects that are not used by the program anymore. like the garbage collector in java.

我听说在不支持垃圾回收的 C 等语言中,程序可能会发生内存泄漏并随后耗尽内存.

I hear in languages like C that don't support garbage collection the programs can have memory leaks and subsequently exhaust the memory.

那么程序员在不支持垃圾收集的 C 等语言中犯了什么错误?我猜想在不再使用对象后不会释放对象.但是,由于缺少垃圾收集器,我们会犯的唯一错误是这些吗?

So what are the errors that programmer make in languages like C that don't support garbage collection? I would guess not deallocating objects after they're not used anymore. But are these the only errors that we can make because of the lack of a garbage collector?

推荐答案

  • 描述你需要的东西

    • Dellocating things you need

      不释放你不再需要的东西(因为你没有很好地跟踪分配/使用/释放)

      Not deallocating things you no longer need (because you're not tracking allocations/use/frees well)

      重新分配已存在事物的新实例(未正确跟踪的副作用)

      Re-allocating new instances of things that already exist (a side-effect of not tracking properly)

      取消分配你已经释放的东西

      De-allocating something you've already freed

      取消分配不存在的东西(空指针)

      De-allocating something that doesn't exist (a null pointer)

      可能还有更多.关键是:管理内存很棘手,最好使用某种跟踪机制和分配/释放抽象来处理.因此,您不妨将其内置到您的语言中,这样它就可以让您轻松自如.手动内存管理并不是世界末日——它当然是可行的——但现在,除非你正在编写实时代码、硬件驱动程序,或者(也许,可能)最新的超优化核心代码游戏,那么除了作为一项学术练习之外,手动工作是不值得的.

      There are probably more. The point is: managing memory is tricky, and is best dealt with using some sort of tracking mechanism and allocating/freeing abstraction. As such, you might as well have that built into your language, so it can make it nice and easy for you. Manual memory management isn't the end of the world -- it's certainly doable -- but these days, unless you're writing real-time code, hardware drivers, or (maybe, possibly) the ultra-optimised core code of the latest game, then manual effort isn't worth it, except as an academic exercise.

      这篇关于垃圾收集与非垃圾收集编程语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:垃圾收集与非垃圾收集编程语言