Cost per class in a Java application – fewer large classes or several smaller classes

What is the memory cost for each new class added to a Java application?

>Is it better to have 5000 large lines or several 500-1000 line classes (if all these are loaded)? > Each time an object is instantiated, the only additional memory usage is instance variable reference > for 5000 rows of classes without instance variables, what is the cost ratio when loading classes? Class file size is a rough approximation? > Does the size of the jar file represent the normal or maximum size of memory that the class will occupy?

After cruftex's answer, edit: This is my understanding of classroom Division:

>Splitting into logical blocks can improve code reuse and reduce the number of lines > it also makes it easier to understand and maintain code

This is my understanding of class loading:

>Load class into memory for the first time (the memory used is roughly the size of the class file) > If JIT is used, the JIT compiler will create some additional machine-friendly binary versions, which use more memory > If hotspot is used, only use the machine-friendly version to optimize some common classes (to balance memory and speed) > after loading a class, The cost of creating other instances is negligible (about 50-100 bytes?) (assuming no instance variables) > once the class is loaded, the class itself will not be garbage collected

How does this work?

Solution

Usually it doesn't matter Generally speaking, only a small part (say 5%) of the overall memory usage is occupied by code in various forms Therefore, even if you do halve the code size, the overall memory usage will only decrease slightly

In contrast, too long source files make it difficult to navigate the code base, and a large range makes it more difficult to fully understand the function of the class and whether a change is safe Therefore, long source files make modifying code more expensive and error prone

correct.

Hotspot is a JIT, so you repeat it here But, yes, JIT does increase code size (but speed)

This is JVM specific On the Oracle hotspot JVM, the memory overhead of each object is about 8 bytes, as shown in the following program:

public class Test {
    public static void main(String[] args) {
        Object[] array = new Object[10_000_000];
        Runtime rt = Runtime.getRuntime();
        long usedBefore = rt.totalMemory() - rt.freeMemory();
        for (int i = 0; i < array.length; i++ ) {
            array[i] = new Object();
        }
        long usedAfter = rt.totalMemory() - rt.freeMemory();
        System.out.println(usedBefore);
        System.out.println(usedAfter);
        System.out.println((double)(usedAfter - usedBefore) / array.length);
    }
}

Although it is not a mandatory requirement of the Java language specification, each JVM I use will release a class when the classloader cannot be accessed (after grant, the bootstrap class loader will always remain accessible, but the custom classloader may not be accessible)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>