C#和Java垃圾收集之间的根本区别是什么? [英] What are the fundamental differences between garbage collection in C# and Java?

查看:75
本文介绍了C#和Java垃圾收集之间的根本区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从一位高级开发人员/同事那里得到了一些关于C#垃圾收集器的声音建议,例如...




  • 您需要在C#中无处不在使用析构函数
    ,因为不能依赖垃圾
    收集器。 C#垃圾收集器不能像$ Java
    收集器那样被认为是




这听起来对我来说非常可怕,据我所知,C#和Java垃圾收集器之间的区别如下所示:




  • C#是代代垃圾
    收集器,Java是并发标记
    ,其中G1是新的
    默认(世代)垃圾
    收集器,具有Java 7并且具有
    自〜1.6.21以来一直是可选的。至于
    ,据我所知

  • C#作为一种语言可以手动处理
    实现 IDisposable 。 Java必须
    总是使用垃圾回收,
    ,尽管像SWT
    这样的框架需要您手动调用方法来
    释放底层
    本机代码中的内存。



我意识到Java和C#只是语言,垃圾收集器是运行时的一个组件,但是对于这种情况,我特别谈论Sun / Oracle JVM和Microsoft .NET Runtime。



有人有反馈吗? 解决方案



C#和Java都有GC来尝试优化快速恢复大量的小物体。它们旨在解决相同的问题,它们以稍微不同的方式实现,但作为用户,您使用方法的技术差异很小,即使大多数用户不存在。



IDisposable 与GC没有任何关系。这是命名方法的一种标准方式,否则将被称为 close destroy , dispose 等等,并且通常在Java中被调用。有人建议Java 7使用关键字添加一些与非常相似的关键字,该关键字将调用类似的 close 方法。

C#中的析构函数指的是终结器 - 这是故意混淆C ++程序员的。 :) CLR规范自己调用它们的终结器,正如JVM所做的那样。

Java和C#/ CLR有许多不同的方式(用户值类型,属性,泛型以及称为Linq的全部相关功能),但是GC是一个可以开发大量软件的领域,在您需要考虑它们之间的差异之前,您可以开发大量的软件。

I had some very wrong sounding advice recently from a "senior" developer/coworker regarding the C# garbage collector such as...

  • "You need to use destructors everywhere in C# because the garbage collector cannot be relied upon."

  • "The C# garbage collector cannot be thought of like the Java garbage collector".

This sounds extremely fishy to me, as far as I know the differences between the C# and Java garbage collectors are as follows...

  • The C# is a Generational garbage collector, Java is concurrent mark sweep in 1.6 with G1 being the new default (generational) garbage collector featuring Java 7 and has been optional since ~1.6.21. As far as I know
  • C# as a language has the ability to manually dispose of objects that implement IDisposable. Java must always use garbage collection, although some frameworks like SWT require you manually call methods to release memory in the underlying native code.

I realize that Java and C# are just the languages and the garbage collectors are a component of the runtime, however for this case I am specifically speaking about the Sun/Oracle JVM and the Microsoft .NET Runtime.

Does anybody have feedback?

解决方案

The advice you've been given is, broadly speaking, a load of hooey.

Both C# and Java have GCs that attempt to optimise the fast recovery of lots of small objects. They're designed to solve the same problem, they do it in slightly different ways but as a user the technical differences in your approach to using them is minimal, even non-existent for the majority of users.

IDisposable is nothing to do with the GC as such. It's a standard way of naming methods that would otherwise be called close, destroy, dispose, etc., and often are called that in Java. There is a proposal for Java 7 to add something very similar to the using keyword that would call a similar close method.

"Destructors" in C# refers to finalizers - this was done deliberately to confuse C++ programmers. :) The CLR spec itself calls them finalizers, exactly as the JVM does.

There are many ways in which Java and C#/CLR differ (user value types, properties, generics and the whole family of related features known as Linq), but the GC is one area where you can develop a substantial amount of software before you need to worry much about the difference between them.

这篇关于C#和Java垃圾收集之间的根本区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆