Junior — Middle
How is the automatic memory management mechanism configured in the system?
sobes.tech AI
Answer from AI
In Java, automatic memory management is performed using a garbage collector (GC). The configuration of GC mechanisms can be done through JVM startup parameters.
Main configuration methods:
- Choosing the type of garbage collector: for example,
-XX:+UseG1GCto enable G1 GC,-XX:+UseConcMarkSweepGCfor CMS. - Heap size configuration: parameters
-Xms(initial size),-Xmx(maximum size). - Parameters for fine-tuning GC behavior: for example,
-XX:MaxGCPauseMillisto limit pauses.
Example of JVM startup with G1 GC and heap size:
java -Xms512m -Xmx2g -XX:+UseG1GC -jar app.jar
It is also possible to use System.gc() methods in code for explicit garbage collection requests, but this is not recommended for regular use.
It is important to understand that optimal configuration depends on the specific application and load, so profiling and testing are often required.