This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Performance and the fly likes Adding JARS to the classpath - what are the implications? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Adding JARS to the classpath - what are the implications?" Watch "Adding JARS to the classpath - what are the implications?" New topic
Author

Adding JARS to the classpath - what are the implications?

Greg Soulsby
Greenhorn

Joined: Feb 01, 2011
Posts: 9
I want to break my app down into small web-apps that talk to each other via services.

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.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2343
    
  28

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. The only classes that will be loaded when you call them will be the classes that you instantiate using Reflection. Classes that aren't in your "class tree" will never be loaded, even if they are in a JAR in the classpath

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
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding JARS to the classpath - what are the implications?