Last active
October 2, 2024 11:03
-
-
Save Ranjandas/448c41a54460fdf570559715327af36d to your computer and use it in GitHub Desktop.
Setting JAVA_OPTS to set Heap Memory arg
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 | |
COPY . /usr/src/myapp | |
WORKDIR /usr/src/myapp | |
RUN javac HeapMemoryHttpServer.java | |
ENV JAVA_OPTS="" | |
CMD java $JAVA_OPTS -cp . HeapMemoryHttpServer |
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
job "example" { | |
group "java" { | |
network { | |
port "db" { | |
to = 8080 | |
} | |
} | |
task "app" { | |
driver = "docker" | |
config { | |
image = "ranjandas/heapmeminfo" | |
ports = ["db"] | |
auth_soft_fail = true | |
} | |
identity { | |
env = true | |
file = true | |
} | |
env { | |
JAVA_OPTS = "-Xms50M -Xmx50M" | |
} | |
resources { | |
cpu = 500 | |
memory = 2048 | |
} | |
} | |
} | |
} |
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
import com.sun.net.httpserver.HttpServer; | |
import com.sun.net.httpserver.HttpHandler; | |
import com.sun.net.httpserver.HttpExchange; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.net.InetSocketAddress; | |
public class HeapMemoryHttpServer { | |
public static void main(String[] args) throws IOException { | |
// Set up the HTTP server on port 8080 | |
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0); | |
server.createContext("/memory", new MemoryHandler()); | |
server.setExecutor(null); // Default executor | |
server.start(); | |
System.out.println("Server started on port 8080"); | |
} | |
// Handler that processes HTTP requests | |
static class MemoryHandler implements HttpHandler { | |
@Override | |
public void handle(HttpExchange exchange) throws IOException { | |
// Get the runtime instance | |
Runtime runtime = Runtime.getRuntime(); | |
// Get initial heap size (Xms) in bytes and convert to megabytes | |
long xms = runtime.totalMemory(); | |
// Get maximum heap size (Xmx) in bytes and convert to megabytes | |
long xmx = runtime.maxMemory(); | |
// Convert bytes to megabytes | |
long xmsInMb = xms / (1024 * 1024); | |
long xmxInMb = xmx / (1024 * 1024); | |
// Prepare response string | |
String response = "Initial Heap Size (Xms): " + xmsInMb + " MB\n" + | |
"Maximum Heap Size (Xmx): " + xmxInMb + " MB\n"; | |
// Print memory info to stdout | |
System.out.println(response); | |
// Respond over HTTP | |
exchange.sendResponseHeaders(200, response.getBytes().length); | |
OutputStream os = exchange.getResponseBody(); | |
os.write(response.getBytes()); | |
os.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment