Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/clojure-goes-fast/clj-memory-meter
Measure object memory consumption in Clojure
https://github.com/clojure-goes-fast/clj-memory-meter
clojure heap memory
Last synced: 5 days ago
JSON representation
Measure object memory consumption in Clojure
- Host: GitHub
- URL: https://github.com/clojure-goes-fast/clj-memory-meter
- Owner: clojure-goes-fast
- Created: 2018-03-05T10:23:05.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-10-03T20:30:57.000Z (2 months ago)
- Last Synced: 2024-12-01T14:09:12.961Z (12 days ago)
- Topics: clojure, heap, memory
- Language: Clojure
- Homepage:
- Size: 159 KB
- Stars: 295
- Watchers: 7
- Forks: 11
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: license/APACHE_PUBLIC_LICENSE
Awesome Lists containing this project
- stars - clojure-goes-fast/clj-memory-meter - Measure object memory consumption in Clojure (⭐️296) (Clojure)
- stars - clojure-goes-fast/clj-memory-meter - Measure object memory consumption in Clojure (⭐️294) (Clojure)
README
# clj-memory-meter [![CircleCI](https://img.shields.io/circleci/build/github/clojure-goes-fast/clj-memory-meter/master.svg)](https://dl.circleci.com/status-badge/redirect/gh/clojure-goes-fast/clj-memory-meter/tree/master) ![](https://img.shields.io/badge/dependencies-none-brightgreen) [![](https://img.shields.io/clojars/dt/com.clojure-goes-fast/clj-memory-meter?color=teal)](https://clojars.org/com.clojure-goes-fast/clj-memory-meter) [![](https://img.shields.io/badge/-changelog-blue.svg)](CHANGELOG.md)
**clj-memory-meter** is a Clojure library that allows you to inspect how much
memory an object occupies at runtime, together with all its child fields. It is
a wrapper around [Java Agent for Memory
Measurements](https://github.com/jbellis/jamm).Extra features compared to **jamm**:
1. Can be added to the project as a simple dependency (don't have to provide a
separate agent file and point to it with a JVM option).
2. Loadable at runtime.
3. Human-readable size output.**jamm** JAR file is shipped together with **clj-memory-meter** and unpacked at
runtime.## Usage
**JDK11+:** you must start your application with JVM option
`-Djdk.attach.allowAttachSelf`, otherwise the agent will not be able to
dynamically attach to the running process. For Leiningen, add `:jvm-opts
["-Djdk.attach.allowAttachSelf"]` to `project.clj`. For tools.deps, add the same
`:jvm-opts` to `deps.edn` or write `-J-Djdk.attach.allowAttachSelf` explicitly
in your REPL command.Add `com.clojure-goes-fast/clj-memory-meter` to your dependencies:
[![](https://clojars.org/com.clojure-goes-fast/clj-memory-meter/latest-version.svg)](https://clojars.org/com.clojure-goes-fast/clj-memory-meter)
Once loaded, you can measure objects like this:
```clojure
(require '[clj-memory-meter.core :as mm]);; measure calculates total memory occupancy of the object
(mm/measure "Hello, world!")
;=> "72 B"(mm/measure [])
;=> "240 B"(mm/measure (into {} (map #(vector % (str %)) (range 100))))
;=> "9.6 KiB";; :shallow true calculates only memory occupied by the object itself,
;; without children(mm/measure (object-array (repeatedly 100 #(String. "hello"))) :shallow true)
;=> "416 B"
(mm/measure (object-array (repeatedly 100 #(String. "hello"))))
;=> "2.8 KiB";; :bytes true can be passed to return the size in bytes as a number
(mm/measure (object-array (repeatedly 100 #(String. "hello"))) :bytes true)
;=> 2848;; :debug true can be passed to print the object hierarchy. You can also pass an
;; integer number to limit the number of nested levels printed.(mm/measure (apply list (range 4)) :debug true)
; root [clojure.lang.PersistentList] 256 bytes (40 bytes)
; |
; +--_first [java.lang.Long] 24 bytes (24 bytes)
; |
; +--_rest [clojure.lang.PersistentList] 192 bytes (40 bytes)
; |
; +--_first [java.lang.Long] 24 bytes (24 bytes)
; |
; +--_rest [clojure.lang.PersistentList] 128 bytes (40 bytes)
; |
; +--_first [java.lang.Long] 24 bytes (24 bytes)
; |
; +--_rest [clojure.lang.PersistentList] 64 bytes (40 bytes)
; |
; +--_first [java.lang.Long] 24 bytes (24 bytes);; Custom MemoryMeter object can be passed. See what you can configure here:
;; https://github.com/jbellis/jamm/blob/master/src/org/github/jamm/MemoryMeter.java
```**Note on JDK17+:** Starting with Java 17, JVM no longer allows accessing
private fields of classes residing in external modules. On those versions of
Java, JAMM and clj-memory-meter utilize Unsafe to get into such private fields.
As any Unsafe usage, it can potentially crash the application. Use at your own
risk. Also, the Unsafe itself may go away in the future versions of Java.## License
jamm is distributed under Apache-2.0.
See [APACHE_PUBLIC_LICENSE](license/APACHE_PUBLIC_LICENSE) file. The location of the original
repository
is
[https://github.com/jbellis/jamm](https://github.com/jbellis/jamm).---
clj-memory-meter is distributed under the Eclipse Public License.
See [ECLIPSE_PUBLIC_LICENSE](license/ECLIPSE_PUBLIC_LICENSE).Copyright 2018-2024 Alexander Yakushev