Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding JARS to the classpath - what are the implications?

 
Greg Soulsby
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20764
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic