Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when do classes in jars enter the PermGen

My understanding is that the PermGen (in some sense) holds the class code in memory. Often we have lots of jar files referenced our classpath. When a jar file is included in the classpath (say in the lib directory of tomcat), are all the classes of all those jars automatically loaded into the PermGen?

In a similar question, once a class of a jar file is used, does PermGen load all the classes in that jar file, or just the class that is used (and then later load the rest of the class files when necessary)?

like image 635
adamSpline Avatar asked May 21 '12 18:05

adamSpline


People also ask

How do I know if a jar is classpath?

A pragmatic way: Class. forName("com. myclass") where com. myclass is a class that is inside (and only inside) your target jar; if that throws a ClassNotFoundException , then the jar is not on you current classpath.

What is PermGen space in Java?

lang. OutOfMemoryError: PermGen Space is a runtime error in Java which occurs when the permanent generation (PermGen) area in memory is exhausted. The PermGen area of the Java heap is used to store metadata such as class declarations, methods and object arrays.

How do I increase my PermGen space?

To fix it, increase the PermGen memory settings by using the following Java VM options. -XX:PermSize<size> - Set initial PermGen Size. -XX:MaxPermSize<size> - Set the maximum PermGen Size. In the next step, we will show you how to set the VM options in Tomcat, under Windows and Linux environment.

What is ClassLoader in Java with example?

The Java ClassLoader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. The Java run time system does not need to know about files and file systems because of classloaders. Java classes aren't loaded into memory all at once, but when required by an application.


1 Answers

This depends to some degree on the implementation of the classloader and the JVM - the Java Virtual Machine specification says this:

This specification allows an implementation flexibility as to when linking activities (and, because of recursion, loading) take place, provided that the semantics of the Java programming language are respected, [...]

For example, an implementation may choose to resolve each symbolic reference in a class or interface individually, only when it is used (lazy or late resolution), or to resolve them all at once while the class is being verified (static resolution). This means that the resolution process may continue, in some implementations, after a class or interface has been initialized.

In practice, no sane implementation should automatically load everything in a JAR file just because one class in the file is loaded, let alone just because it's on the classpath.

like image 108
Michael Borgwardt Avatar answered Sep 30 '22 01:09

Michael Borgwardt