Java .class文件的最大大小是多少? [英] What is the maximum size of a Java .class file?

查看:466
本文介绍了Java .class文件的最大大小是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.class 文件相当记录完备格式定义了部分和大小,因此也定义了最大尺寸。

A .class file is a rather well documented format that defines sections and size, and therefore maximum sizes as well.

例如, .class 文件包含一个幻数(4个字节),一个版本(4个字节),一个常量池(可变大小)等。但是大小可以在几个级别定义:你可以有65535个方法,每个方法限制为65535个字节。

For instance, a .class file contains a magic number (4 bytes), a version (4 bytes), the constant pool (variable size), etc. But sizes can be defined on several levels: you can have 65535 methods and each is limited to 65535 bytes.

其他限制是什么?并且,如果您可以制作最大的 .class 文件,它的大小是多少?

What are the other limits? And, if you would make the largest .class file possible, what size would it be?

如果需要,限制Java的答案。这意味着如果Scala或Clojure(或......)更改了某些限制,请忽略这些值。

If needed, limit answers to Java. Meaning that if Scala or Clojure (or...) change some limits, disregard those values.

推荐答案

JVM规范没有要求对类文件进行限制,因为类文件是可扩展的容器,支持任意自定义属性,您甚至可以根据需要将其最大化。

The JVM specification doesn’t mandate a limit for class files and since class files are extensible containers, supporting arbitrary custom attributes, you can even max it out as much as you wish.

每个属性的大小字段为<$ c因此,$ c> u4 类型可以指定最多2³²-1 的数字( 4GiB )。实际上,因为JRE API( ClassLoader 方法,Instrumentation API和不安全)都一直使用 byte [] ByteBuffer 来描述类文件,不可能创建一个超过<$的类文件的运行时类c $ c>2³¹-1 字节( 2GiB )。

Each attribute has a size field of the u4 type, thus, could specify a number of up to 2³²-1 (4GiB). Since, in practice, the JRE API (ClassLoader methods, Instrumentation API and Unsafe) all consistently use either byte[] or ByteBuffer to describe class files, it is impossible to create a runtime class of a class file having more than 2³¹-1 bytes (2GiB).

换句话说即使是单个自定义属性的大小也可能超过实际可加载类的大小。但是一个类可以有65535个属性,加上65535个字段,每个属性都有自己的65535个属性,加上65535个方法,每个属性最多也有65535个属性。

In other words, even a single custom attribute could have a size that exceeds the size of actually loadable classes. But a class can have 65535 attributes, plus 65535 fields, each of them having 65535 attributes of its own and plus 65535 methods, each of them having up to 65535 attribute as well.

如果你做数学运算,你会得出这样一个结论:一个仍然很好的类文件的理论最大值可能超过任何实际存储空间(超过2个字节)。

If you do the math, you will come to the conclusion that the theoretical maximum of a still well formed class file may exceed any real storage space (more than 2⁶⁵ bytes).

这篇关于Java .class文件的最大大小是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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