Skip to content

Periodically Log JVM Memory and GC Usage#3523

Open
alexanderkiel wants to merge 1 commit intomainfrom
3522-periodically-log-jvm-memory-and-gc-usage
Open

Periodically Log JVM Memory and GC Usage#3523
alexanderkiel wants to merge 1 commit intomainfrom
3522-periodically-log-jvm-memory-and-gc-usage

Conversation

@alexanderkiel
Copy link
Copy Markdown
Member

Closes: #3522

@alexanderkiel alexanderkiel linked an issue Apr 1, 2026 that may be closed by this pull request
5 tasks
@alexanderkiel alexanderkiel self-assigned this Apr 1, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 1, 2026

Codecov Report

❌ Patch coverage is 98.21429% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 95.93%. Comparing base (f6d9752) to head (56db06d).

Files with missing lines Patch % Lines
...trics-logger/src/blaze/jvm_metrics_logger/impl.clj 97.29% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3523      +/-   ##
==========================================
- Coverage   95.94%   95.93%   -0.01%     
==========================================
  Files         405      407       +2     
  Lines       25282    25338      +56     
  Branches      658      657       -1     
==========================================
+ Hits        24257    24309      +52     
  Misses        489      489              
- Partials      536      540       +4     
Files with missing lines Coverage Δ
...vm-metrics-logger/src/blaze/jvm_metrics_logger.clj 100.00% <100.00%> (ø)
...trics-logger/src/blaze/jvm_metrics_logger/impl.clj 97.29% <97.29%> (ø)

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@alexanderkiel alexanderkiel force-pushed the 3522-periodically-log-jvm-memory-and-gc-usage branch from 576da9d to 5db3365 Compare April 1, 2026 15:21
@knoppiks knoppiks force-pushed the 3522-periodically-log-jvm-memory-and-gc-usage branch from 65c77be to e189f67 Compare April 2, 2026 08:04

(deftest log-metrics-test
(testing "logs at info level when below threshold"
(is (nil? (jml/log-metrics! 100))))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log-metrics-test only asserts the return value is nil, and the scheduled-*-task-test tests assert (is true) after a sleep. None of these verify that:

  • The correct log level (INFO vs WARN) was used
  • Logging actually occurred
  • The rate-limiting logic throttles info logs to default-interval

Since this feature is specifically about logging at the right level under the right conditions, capturing log output (e.g., via a Timbre appender or with-redefs) would make these tests much more meaningful.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

(let [heap ^MemoryUsage (.getHeapMemoryUsage (ManagementFactory/getMemoryMXBean))
pct (heap-pct heap)
now (System/nanoTime)]
(cond
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: last-info-log is initialized to 0, relying on (- (System/nanoTime) 0) being large enough to trigger the first log. System/nanoTime has no guaranteed epoch — its contract only says differences between two calls are meaningful. Initializing to (- (System/nanoTime) default-nanos) would make the "log immediately on first tick" intent explicit and correct by contract.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done via a combination of interval and warn-factor

(let [heap ^MemoryUsage (.getHeapMemoryUsage (ManagementFactory/getMemoryMXBean))
non-heap ^MemoryUsage (.getNonHeapMemoryUsage (ManagementFactory/getMemoryMXBean))
pct (heap-pct heap)
gcs (seq (ManagementFactory/getGarbageCollectorMXBeans))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: ManagementFactory/getMemoryMXBean is called twice here (returns the same singleton). Binding it once would be slightly cleaner:

(let [mbean    (ManagementFactory/getMemoryMXBean)
      heap     ^MemoryUsage (.getHeapMemoryUsage mbean)
      non-heap ^MemoryUsage (.getNonHeapMemoryUsage mbean)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

java-time.api/duration?
"PT1M"]
:warn-threshold #blaze/cfg ["JVM_METRICS_LOGGER_WARN_THRESHOLD" pos-int? 80]}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: The coercer here is pos-int? which accepts any positive integer, but the spec constrains to 1–99. Setting JVM_METRICS_LOGGER_WARN_THRESHOLD=100 would pass coercion but fail with a spec error at startup. Not blocking (the spec catches it), but the resulting error message won't be as friendly as a dedicated bounds check.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@alexanderkiel alexanderkiel force-pushed the 3522-periodically-log-jvm-memory-and-gc-usage branch 2 times, most recently from 6af1eb5 to 502831c Compare April 2, 2026 15:44
@alexanderkiel alexanderkiel force-pushed the 3522-periodically-log-jvm-memory-and-gc-usage branch from 502831c to 56db06d Compare April 3, 2026 08:52
@alexanderkiel alexanderkiel requested a review from trobanga April 3, 2026 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Periodically Log JVM Memory and GC Usage

2 participants