-
Notifications
You must be signed in to change notification settings - Fork 1
Tomcat Server
Memory management is all about allocation of objects. Java objects reside in an area called the heap. The heap is created when the JVM starts up and may increase or decrease in size while the application runs. the initial heap size, is allocated during JVM initialization. The heap size has an impact on the JVM’s performance, and thus also on the Java application’s performance.
When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects.
Default Heap Size: Unless the initial and maximum heap sizes are specified on the command line, they are calculated based on the amount of memory on the machine.
For example, if your computer has 128 MB of physical memory, then the maximum heap size is 64 MB, and greater than or equal to 1 GB of physical memory results in a maximum heap size of 256 MB. You can use g or G for GB, m or M for MB, k or K for KB.
java -Xms:1g -Xmx:1g MyApplicationThis starts up the JVM with a heap size fixed to 1 GB.
If necessary, adjust the heap size to improve performance by Selecting a Collector:
Java Virtual Machine -XX Options
-XX:+HeapDumpOnOutOfMemoryError
-XX:+DisableExplicitGC-
If the application has a small data set (up to approximately 100 MB), then select the serial collector with the option
-XX:+UseSerialGC. -
If the application will be run on a single processor and there are no pause time requirements, then let the VM select the collector, or select the serial collector with the option
-XX:+UseSerialGC. -
If (a) peak application performance is the first priority and (b) there are no pause time requirements or pauses of 1 second or longer are acceptable, then let the VM select the collector, or select the parallel collector with
-XX:+UseParallelGC.
- If response time is more important than overall throughput and garbage collection pauses must be kept shorter than approximately 1 second, then select the concurrent collector with
-XX:+UseConcMarkSweepGCor-XX:+UseG1GC.
The java.lang.OutOfMemoryError: PermGen space message indicates that the Permanent Generation’s area in memory is exhausted.
The java.lang.OutOfMemoryError: Metaspace message indicates that the Metaspace area in memory is exhausted.
java -XX:MaxPermSize=512m -XX:MaxMetaspaceSize=512m
Tomcat:
set JAVA_OPTS=%JAVA_OPTS% -Xms512m -Xmx1g -XX:+UseParallelOldGC -XX:MaxMetaspaceSize=512m -XX:PermSize=128m -XX:MaxPermSize=512mJava HotSpot(TM) Client VM warning: ignoring option PermSize=128m; support was removed in 8.0
Java HotSpot(TM) Client VM warning: ignoring option MaxPermSize=512m; support was removed in 8.0###Increasing JVM Heap size to a Tomcat Instance(Catalina Daemon) for serving different Applications on Same PORT:
To increase java heap size in Tomcat :
Stop Tomcat server, set environment variable CATALINA_OPTS, and then restart Tomcat. In catalina.bat or catallina.sh, you may have noticed CATALINA_OPTS, JAVA_OPTS, or both can be used to specify Tomcat JVM options.
- CATALINA_OPTS is specific for Tomcat servlet container
set CATALINA_OPTS=-Xms512m -Xmx512m (Windows, no "" around the value)
export CATALINA_OPTS="-Xms512m -Xmx512m" (ksh/bash, "" around the value)
setenv CATALINA_OPTS "-Xms512m -Xmx512m" (tcsh/csh, "" around the value)- JAVA_OPTS may be used by other java applications (e.g., JBoss)
$JBOSS_HOME/bin/run.conf « JAVA_OPTS="-server -Xms128m -Xmx128m"java.lang.OutOfMemoryError:The parallel collector throws an OutOfMemoryError if too much time is being spent in garbage collection (GC): If more than 98% of the total time is spent in garbage collection and less than 2% of the heap is recovered, then an OutOfMemoryError is thrown.
- Large Applications « Check why it consumes that much memory
- After Several Requests « then you could have a memory leak - where objects are not being reclaimed by the garbage collector, use jprofiler
I prefer to use CATALINA_OPTS. Either you can change in catalina.bat/catalina.sh, but its recommended to maintain in new file setenv.bat/setenv.sh as per CATALINA Server.
NOTE: If you know how much heap your application needs to work well, you can set -Xms and -Xmx to the same value. If not, the JVM will start by using the initial heap size and will then grow the Java heap until it finds a balance between heap usage and performance. Examples are heap size, GC logging, JMX ports etc
rem Do not set the variables in this script. Instead put them into a script
rem setenv.bat in CATALINA_BASE/bin to keep your customizations separate.Tomcat main class which accepts command line arguments like -Xmx, -Xms, -Xmn, -XX:PermSize ...
Setting the Heap Size Command line options: -Xms:<min size> -Xmx:<max size>
catalina.bat « Windows
rem Tomcat server main class
set MAINCLASS=org.apache.catalina.startup.Bootstrap
rem Get standard environment variables
if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat"
if "%TITLE%" == "" set TITLE=MyLocalTomcat
set _EXECJAVA=start "%TITLE%" %_RUNJAVA%catalina.sh « Linux/Mac/Ubuntu
export CATALINA_OPTS="-Xms512M -Xmx1024M"setenv.bat - To increase tomcat's heap size to 1024MB & add environmental variables path.
rem Their should be no spaces in between property and its value. Ex: property=value
rem -XX:PermSize It’s used to set size for Permanent Generation. It is where class files are kept.
rem CATALINA_OPTS (Optional) Java runtime options used when the "start", "run" or "debug" command is executed.
rem Include here and not in JAVA_OPTS all options, that should only be used by Tomcat itself,
rem Examples are heap size, GC logging, JMX ports etc.
set CATALINA_OPTS = "$CATALINA_OPTS -server -Xms256m -Xmx1024m"
="-Xms512m -Xmx1024m"
=-server -Xmx1024m -Xmx1024m -XX:PermSize=512m -XX:MaxPermSize=512m
rem JAVA_OPTS (Optional) Java runtime options used when any command is executed.Most options should go into CATALINA_OPTS.
set JAVA_OPTS =%JAVA_OPTS% -Xms256m -Xmx1100m -XX:+UseParallelOldGC -XX:PermSize=128m -XX:MaxPermSize=512m
="-Djava.awt.headless=true -Xmx1280m -XX:+UseConcMarkSweepGC"
rem TITLE (Optional) Specify the title of Tomcat window. The default TITLE is Tomcat if it's not specified.
set TITLE = LocalTomcat
REM Using CATALINA_BASE: "D:\Yashwanth\Apache\apache-tomcat-7.0.37"
REM Using CATALINA_HOME: "D:\Yashwanth\Apache\apache-tomcat-7.0.37"
REM Using CATALINA_TMPDIR: "D:\Yashwanth\Apache\apache-tomcat-7.0.37\temp"
REM Using JRE_HOME: "C:\Program Files\Java\jdk1.6.0_45\jre"
REM Using CLASSPATH: "D:\Yashwanth\Apache\apache-tomcat-7.0.37\bin\bootstrap.jar;D:\Yashwanth\Apache\apache-tomcat-7.0.37\bin\tomcat-juli.jar"
set CATALINA_HOME = "D:\Yashwanth\Apache\apache-tomcat-7.0.37"
set "JRE_HOME=%ProgramFiles%\Java\jdk1.6.0_45\jre"
set "JAVA_HOME=%ProgramFiles%\Java\jdk1.6.0_45"
rem setenv.sh
export CATALINA_OPTS = "$CATALINA_OPTS -server -Xms512m -Xmx8192m -XX:MaxPermSize=256m"From Windows « Advanced System Parameters « Environment Variables
KEY: CATALINA_OPTS
Val: -Xms512m -Xmx1024meclipse-home/eclipse.ini to be something like the following and restart Eclipse.
-vmargs
-Xms64m
-Xmx256mIn a class-based object-oriented language, in general, state is carried by instances, methods are carried by classes, and inheritance is only of structure and behavior. Basic, Refactoring Techniques
Method signature: It consists of method name and parameter list (number/type/order of the parameters). methodName(parametersList y). An instance method in a subclass with the same signature and return type as an instance method in the super-class overrides the super-class's method.
Java OOP concepts
Class - Collection of a common features of a group of object [static/instance Fields, blocks and Methods]
Object - Instance of a class (instance fields)
Abstraction - Process of hiding complex info and providing required info like API, Marker Interfaces ...
Encapsulation(Security) - Class Binding up with data members(fields) and member functions.
Inheritance (Reusability by placing common code in single class)
1. Multilevel - {A -> B -> C} 2. Multiple - Diamond problem {A <- (B) -> C} [Java not supports] 3. Cyclic {A <-> B} [Java not supports]
* Is-A Relation - Class A extends B
* Hash-A Relation - Class A { B obj = new B(); } - (Composition/Aggregation)
Polymorphism (Flexibility) 1. Compile-Time Overloading 2. Runtime Overriding [Greek - "many forms"]
int[] arr = {1,2,3}; int arrLength = arr.length; // Fixed length of sequential blocks to hold same data type
String str = "Yash"; int strLength = str.length(); // Immutable Object value can't be changed.
List<?> collections = new ArrayList<String>(); int collectionGroupSize = collections.size();
Map<?, ?> mapEntry = new HashMap<String, String>();
Set<?> keySet = mapEntry.keySet(); // Set of Key's
Set<?> entrySet = mapEntry.entrySet(); // Set of Entries [Key, Value]
// Immutable Objects once created they can't be modified. final class Integer/String/Employee
Integer val = Integer.valueOf("100"); String str2 = String.valueOf(100); // Immutable classes
final class Employee { // All Wrapper classes, java.util.UUID, java.io.File ...
private final String empName; // Field as Final(values can be assigned only once) Only getter functions.
public Employee(String name) { this.empName = name; }
} Native Java Code for Hashtable.h, Hashtable.cpp
SQL API.
You can check your current JDK and JRE versions on your command prompt respectively,
- JDK
javac -version [C:\Program Files\Java\jdk1.8.0_121\bin]o/p:javac 1.8.0_121 - JRE
java -version[C:\Program Files\Java\jdk1.8.0_121\bin]o/P:java version "1.8.0_102"
JAVA_HOME - Must be set to JDK otherwise maven projects leads to compilation error. [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? C:\Softwares\OpenJDK\, 7-zip
Fatal error compiling: invalid target release: JRE and JDK must be of same version
1.8.0.XXX
Disable TLS 1.0 and 1.1
security-libs/javax.net.ssl: TLS 1.0 and 1.1 are versions of the TLS protocol that are no longer considered secure and have been superseded by more secure and modern versions (TLS 1.2 and 1.3).
Core Java
-
Java Programming Language Basics
- Object, Class, Encapsulation, Interface, Inheritance, Polymorphism (Method Overloading, Overriding)
- JVM Architecture, Memory Areas
- JVM Class Loader SubSystem
- Core Java Interview Questions & Programs
- Interview Concepts
Stack Posts
- Comparable vs Comparator
- Collections and Arrays
-
String, StringBuffer, and StringBuilder
- String reverse
- Remove single char
- File data to String
- Unicode equality check Spacing entities
- split(String regex, int limit)
- Longest String of an array
-
Object Serialization
- Interface's Serializable vs Externalizable
- Transient Keyword
-
implements Runnablevsextends Thread - JSON
- Files,
Logging API- Append text to Existing file
- Counting number of words in a file
- Properties
- Properties with reference key



