简体   繁体   English

为什么有.Net的Debug和Release版本,但Java没有?

[英]Why are there Debug & Release builds for .Net, but not for Java?

In .Net, you can specifically compile your projects under "Debug" or "Release" settings, Release having more optimizations. 在.Net中,您可以在“调试”或“发布”设置下专门编译项目,发布具有更多优化。 I know that this is deemed unnecessary in Java, because the jitter does these optimizations. 我知道这在Java中被认为是不必要的,因为抖动会进行这些优化。 What is the reason for the difference? 差异的原因是什么? (meaning, why is a pre-"jitter" compilation required/helpful) (意思是,为什么需要/有帮助预先“抖动”编译)

Why is it deemed necessary in .Net/CLR, but not in Java/JDK? 为什么在.Net / CLR中认为有必要,而在Java / JDK中却没有?

Earlier Java compilers had an -O flag to enable (source code) compilation optimizations. 早期的Java编译器有一个-O标志来启用(源代码)编译优化。 Since JDK 1.2, the -O flag had no effect and I believe the flag was removed with JDK 1.4. 从JDK 1.2开始,-O标志没有效果,我相信该标志已被JDK 1.4删除。 As the Java runtime improved, it probably turned more and more reasonable to delegate the optimization to the JRE, since the source code compiler has absolutely no knowledge about the hardware, which will eventually execute the code. 随着Java运行时的改进,将优化委托给JRE可能变得越来越合理,因为源代码编译器完全不了解硬件,最终将执行代码。

Articles like this one and the documentation of the csc /optimize flag indicate that the optimization has very little effect (if any at all?) on the actual generation of the CLR code. 这样的文章和csc / optimize标志文档表明优化对CLR代码的实际生成几乎没有影响(如果有的话?)。 The /optimize flag does however set a flag in the compiled assembly, which controls the level of optimization allowed to be applied by the runtime. 然而,/ optimize标志在编译的程序集中设置了一个标志,该标志控制允许运行时应用的优化级别。 I haven't tried it, but I"ve read that runtime optimized code is not necessarily debuggable, although debug information is included (the /optimize and /debug flags can be enabled or disabled independently for the C# compiler). 我没有尝试过,但我已经读过运行时优化代码不一定是可调试的,尽管包含了调试信息(可以为C#编译器独立启用或禁用/ optimize和/ debug标志)。

Ι don't really see the point in controlling the runtime optimization level at compile time. Ι在编译时没有真正看到控制运行时优化级别的重点。 The Java runtime has several detailed options to control runtime performance and optimization, but these must be defined when starting the JRE and not at compile time. Java运行时有几个用于控制运行时性能和优化的详细选项 ,但这些选项必须在启动JRE时定义,而不是在编译时定义。

Sun's javac compiler does have the concept of debugging information which can be (optionally) omitted from the compiled class output. Sun的javac编译器确实具有调试信息的概念,可以(可选)从编译的类输出中省略。

Look at the documentation , and check out the -g flag options: 查看文档 ,并查看-g标志选项:

-g
  Generate all debugging information, including local 
  variables. By default, only line number and source 
  file information is generated.
-g:none
  Do not generate any debugging information.
-g:{keyword list}
  Generate only some kinds of debugging information, 
  specified by a comma separated list of keywords. 
  Valid keywords are:
    source
      Source file debugging information
    lines
      Line number debugging information
    vars
      Local variable debugging information

These are perhaps not quite as extensive as the bytecode optimisations the .NET compilers might perform (which I'm not familiar with, sorry), but I think in theory they are there for performance reasons (smaller classfiles, etc). 这些可能不像.NET编译器可能执行的字节码优化那样广泛(我不熟悉,对不起),但我认为理论上它们是出于性能原因(较小的类文件等)。

In practise I strongly suspect they wouldn't make much difference to runtime performance on modern hardwares + JVMs, but they are there. 在实践中,我强烈怀疑它们对现代硬件+ JVM上的运行时性能没有太大影响,但它们就在那里。

I think they can easily be introduced in Java. 我认为它们可以很容易地用Java引入。 Basically Debug build means: Include debug symbols and disable optimization. 基本上,Debug构建意味着:包含调试符号并禁用优化。 Release is vice versa. 发布则反之亦然。 Debug and Release targets are generated by Visual Studio and are not mandatory. 调试和发布目标由Visual Studio生成,不是必需的。 You can write your MSBuild script without VS. 您可以在没有VS的情况下编写MSBuild脚本。 So you can create build script for Java with Debug and Release targets. 因此,您可以使用Debug和Release目标为Java创建构建脚本。

http://blogs.msdn.com/b/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx has a nice list of optimisations that are performed when the optimize switch is on (it's on for release, off for debug). http://blogs.msdn.com/b/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx有一个很好的优化列表,当优化开关打开时执行(它是发布,关闭调试)。

The Java compiler could also do any of these, and it might do some of them by default anyway. Java编译器也可以执行其中的任何操作,并且无论如何它可以默认执行其中一些操作。 They're mostly small wins. 他们大多是小胜利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM