Last active
June 17, 2020 06:44
-
-
Save himslm01/124ba642c8486f499b335f77a7ac83b6 to your computer and use it in GitHub Desktop.
Investigation into Java Garbage collection with Kubernetes CPU and memory limits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (c) 2020 Mark Himsley | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
package com.mdsh.jvm_options; | |
import java.io.IOException; | |
import java.lang.management.GarbageCollectorMXBean; | |
import java.lang.management.ManagementFactory; | |
import java.nio.charset.StandardCharsets; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.util.Arrays; | |
import javax.management.InstanceNotFoundException; | |
import javax.management.MBeanException; | |
import javax.management.MalformedObjectNameException; | |
import javax.management.ObjectName; | |
import javax.management.ReflectionException; | |
public class App { | |
public static void main(String[] args) throws InstanceNotFoundException, MalformedObjectNameException, ReflectionException, MBeanException, NullPointerException { | |
// from the answer to: | |
// https://stackoverflow.com/questions/54862460/how-to-determine-which-gc-i-use | |
Object flags = ManagementFactory.getPlatformMBeanServer().invoke( | |
ObjectName.getInstance("com.sun.management:type=DiagnosticCommand"), | |
"vmFlags", new Object[] { null }, new String[] { "[Ljava.lang.String;" }); | |
System.out.println("vmFlags"); | |
for(String f: ((String)flags).split("\\s+")) { | |
System.out.printf(" %s%n",f); | |
} | |
System.out.println("Garbage Collectors"); | |
for(GarbageCollectorMXBean gc: ManagementFactory.getGarbageCollectorMXBeans()) { | |
System.out.printf(" %-45s: %s%n", gc.getName(), Arrays.toString(gc.getMemoryPoolNames())); | |
} | |
// walk through /sys/fs/ directories getting interesting information | |
printSYSFSValues(Path.of("/sys/fs/cgroup/cpu/"), "cpu."); | |
printSYSFSValues(Path.of("/sys/fs/cgroup/memory/"), "memory."); | |
} | |
private static void printSYSFSValues(Path path, String match) { | |
System.out.println("contents of " + path.toString() + "/" + match + "*"); | |
if (path.toFile().exists() && path.toFile().canRead()) { | |
for (String file : path.toFile().list()) { | |
if (!file.startsWith(match)) { | |
continue; | |
} | |
Path p = path.resolve(file); | |
if (p.toFile().canRead()) { | |
try { | |
String content = Files.readString(p, StandardCharsets.US_ASCII); | |
System.out.printf(" %-45s: %s", p.toString(), content); | |
} catch (IOException e) { | |
System.out.printf(" %-45s: <could not read contents>%n", p.toString()); | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM openjdk:11-jre | |
WORKDIR /opt/mdsh | |
COPY jvmoptions.jar lib/ | |
CMD ["sh", "-c", "java $JAVA_OPTS -Dfile.encoding=UTF-8 -Duser.timezone=UTC -jar lib/jvmoptions.jar"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment