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.
Each attribute has a size field of the u4
type, thus, could specify a number of up to 232-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 231-1
bytes (2GiB
).
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.
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).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…