For every new Class added to a Java application, what is the cost in terms of memory?
- Is it better to have huge 5000+ lines classes or several 500-1000 line classes (if all of them are loaded anyway)
- Each time an Object is instantiated is it true that the only additional memory usage is for the instance variable references
- For a 5000-line class with no instance variables, what is the scale of cost when the class is loaded.. is the size of the class file a rough approximation?
- Is the size of the jar file any indication of either usual or max size of memory that classes will occupy?
Edited after cruftex's answer: This is my understanding of class splitting:
- Splitting into logical blocks could very well improve code reuse and reduce number of lines
- It also makes it easier to understand and maintain code
This is my understanding of class loading now:
- On first use Class is loaded into memory (Memory used is roughly the size of the class file)
- If JIT is used, some additional machine-friendly binary version is created by the JIT compiler which uses a little more memory
- If Hotspot is used only some of the frequently used classes are optimised with machine-friendly versions (to balance memory and speed)
- Once a class is loaded, creating additional instances has negligible overhead (about 50-100 bytes?) (assuming no instance variables)
- Once a class is loaded the class itself is never garbage collected
Is this roughly how it works?