java java中方法的最大大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6570343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maximum size of a method in java?
提问by Reuben
I come to know that the maximum size of a method in java is 64k. And if it exceeds, we'll get a compiler warning like "Code too large to compile". So can we call this a drawback of java with this small amount of memory.
我开始知道java中一个方法的最大大小是64k。如果超过,我们将收到编译器警告,例如“代码太大而无法编译”。那么我们可以将其称为具有如此少量内存的java的缺点吗?
Can we increase this size limit or is it really possible to increase ?
我们可以增加这个大小限制还是真的有可能增加?
Any more idea regarding this method size ?
关于此方法大小的更多想法?
回答by Peter Lawrey
In my experience the 64KB limit is only a problem for generated code. esp. when intiialising large arrays (which is done in code)
根据我的经验,64KB 限制只是生成代码的问题。特别是 初始化大数组时(在代码中完成)
In well structured code, each method is a manageable length and is much smaller than this limit. Large pieces of data, to be loaded into arrays, can be read from a non Java files like a text or binary file.
在结构良好的代码中,每个方法都是一个可管理的长度,并且远小于这个限制。要加载到数组中的大量数据可以从非 Java 文件(如文本或二进制文件)中读取。
EDIT:
编辑:
It is worth nothing that the JIT won't compile methods larger than 8 K. This means the code runs slower and can impact the GC times (as it is less efficient to search the call stack of a thread with methods which are not compiled esp big ones)
JIT 不会编译大于 8 K 的方法是毫无价值的。这意味着代码运行速度较慢并且会影响 GC 时间(因为使用未编译的方法搜索线程的调用堆栈效率较低,尤其是大的)
If possible you want to limit your methods to 8 K rather than 64 K.
如果可能,您希望将方法限制为 8 K 而不是 64 K。
回答by Sergey Aslanov
64k is quite a lot, if you exceed it you may think about reorganizing you code.
64k 相当多,如果超过它,您可能会考虑重新组织您的代码。
In my project I met this constraint once in generated sources. Solved by just splitting one method to several.
在我的项目中,我在生成的源中遇到了这个约束。只需将一种方法拆分为多种方法即可解决。
回答by AlexR
If your method is longer than 50 lines including inside comments - split it. In this case you will never reach any limitation (even if one exists).
如果您的方法超过 50 行,包括内部注释 - 将其拆分。在这种情况下,您永远不会达到任何限制(即使存在)。
I personally saw 1000 lines long methods (written by criminals that call themselves programmers :) ) but did not see such kind of limitation.
我个人看到了 1000 行长的方法(由自称程序员的罪犯编写:)),但没有看到这种限制。