But I am worried about the memory implications of this. (my ISP is charging a lot for memory).
If my guess is correct, an unnecessary jar in the classpath will be taking up unnecessary memory in each web-app?
Right? Or are they only loaded into memory as required?
I am using MyEclipse for Spring which is defaulting jars into the classpath, some of which I cant see how are needed.
Having said that, it's generally very rare for the class definitions to take too much space. Generally, I haven't required the PermGen space to be more that 128M, whereas I've taken the heap upto 2G. Are you sure you aren't concerned about the memory used by the Objects and not Classes?
Remember that when Java uses a class, it loads the definition of the class; ie; the byte code of the class in PermGen. When your code creates objects of the class, the memory for the object is assigned in the heap.
Also, generally breaking down your app into smaller webapps would increase the total memory usage. In most default configurations, each webapp has it's own classloader, and if 2 webapps require the same class, it will be loaded twice by the server. Also, the server itself requires some amount of memory to manage your webapp (might be negligible) You can change this behavior by moving the jar to the server's lib directory instead of keeping it in the webapp's lib. Also, I believe JBoss allows you to share classloaders between webapps
Jayesh A Lalwani wrote:However, IIRC, if you have class A imports class B, class B will be loaded as soon as class A is loaded, even if you don't call any of the methods on B.
I don't think this is correct. "import" is only used by the compiler to allow the code to refer to "List" rather than "java.util.List" but the compiled code always refers to the fully-qualified class name.
This generally means that most of the classes that you need are loaded at load time.
I don't think this is correct either. Classes are loaded at the time they are first referenced; if you have ever had a NoClassDefFoundError -- and who hasn't? -- then you will have seen that happening.
Jayesh A Lalwani wrote:The bytecode of the classes are loaded lazily. However, IIRC, if you have class A imports class B, class B will be loaded as soon as class A is loaded, even if you don't call any of the methods on B. This generally means that most of the classes that you need are loaded at load time.
Definitely false. Imports are a compile-time-only construct. They have absolutely no bearing whatsoever on the runtime. You can import the universe, but only those classes that are actually referenced in your code (or referenced by something your code references, and so on, recurseively) will ever be loaded.