Middle
How can you set the garbage collector (GC) configuration parameters in Java?
sobes.tech AI
Answer from AI
GC parameters are set using command-line options when launching the JVM. The most common options start with -XX:.
Examples of options:
-Xms<size>: Initial heap size.-Xmx<size>: Maximum heap size.-XX:+<GCName>: Selecting the garbage collector. For example,-XX:+UseG1GCto enable G1 GC.-XX:NewRatio=<number>: Ratio of young generation to old generation (also "how many parts of old generation correspond to one part of young"). For example,-XX:NewRatio=2means the old generation is twice as large as the young (1/3 heap - young, 2/3 - old).-XX:SurvivorRatio=<number>: Ratio of Eden space to each Survivor space. For example,-XX:SurvivorRatio=8means the Eden space is 8 times larger than each Survivor space (1/10 of young generation - each Survivor).-XX:MaxMetaspaceSize=<size>: Maximum size of Metaspace.-XX:+PrintGCDetails: Output detailed information about each garbage collection.-XX:+PrintGCTimeStamps: Add timestamps to GC output.-XX:+UseConcMarkSweepGC: Use CMS collector (Deprecated in recent versions).-XX:+UseParallelGC: Use parallel collector for the young generation.
Example of launching an application with GC parameters:
java -Xms512m -Xmx2g -XX:+UseG1GC -XX:+PrintGCDetails -jar myapp.jar
Parameters may vary depending on the specific garbage collector and Java version. Oracle documentation is the primary source of information on available options.