Java のヒープメモリ使用量を CloudWatch で計測する方法です。
// CloudWatch クライアント作成 AmazonCloudWatch client = new AmazonCloudWatchClient(new AWSCredentials() { @Override public String getAWSSecretKey() { return awsSecretKey; } @Override public String getAWSAccessKeyId() { return awsAccessKey; } }); MemoryMXBean mBean = ManagementFactory.getMemoryMXBean(); MemoryUsage heapUsage = mBean.getHeapMemoryUsage(); double used = heapUsage.getUsed() / 1024; double max = heapUsage.getMax() / 1024; double committed = heapUsage.getCommitted() / 1024; Dimension dimension = new Dimension().withName("InstanceId").withValue("01"); PutMetricDataRequest request = new PutMetricDataRequest().withNamespace("JVM").withMetricData( new MetricDatum() .withDimensions(dimension) .withMetricName("HeapMemoryUsed") .withUnit(StandardUnit.Kilobytes.toString()) .withValue(used), new MetricDatum() .withDimensions(dimension) .withMetricName("HeapMemoryMax") .withUnit(StandardUnit.Kilobytes.toString()) .withValue(max), new MetricDatum() .withDimensions(dimension) .withMetricName("HeapMemoryCommitted") .withUnit(StandardUnit.Kilobytes.toString()) .withValue(committed)); client.putMetricData(request);
このコードを定期的に実行するようにすれば、CloudWatch で閾値など監視することができるようになります。