# clj-memory-meter
This library is a thin wrapper
around [Java Agent for Memory Measurements](https://github.com/jbellis/jamm). It
allows to inspect at runtime how much memory an object occupies together with
all its child fields.
Extra features compared to **jamm**:
1. Easy runtime loading (you can start using it at any time at the REPL,
providing additional startup parameters is not necessary).
2. Human-readable size output.
**jamm** JAR file is shipped together with **clj-memory-meter** and unpacked at
runtime.
## Usage
**JDK9+:** you must start the JVM with 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 Boot, start the process with environment variable
`BOOT_JVM_OPTIONS="-Djdk.attach.allowAttachSelf"`.
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 KB"
;; :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 KB"
;; :bytes true can be passed to return the raw number of bytes
(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
```
## 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-2020 Alexander Yakushev
快撑死的鱼
- 粉丝: 2w+
- 资源: 9157
最新资源
- 【无人机】四旋翼飞行器目标分配、全局路径规划和局部路径规划附Matlab代码.rar
- 【无人机三维路径规划】基于PSO无人机路径规划3D城市附Matlab代码.rar
- 【无人机路径规划】粒子群优化和遗传算法实现有效的水陆两栖无人机任务规划和执行Matlab实现.rar
- 基于mediapipe和KNN分类算法的健身计数器引体向上-深蹲-俯卧撑计数器源码+项目文档说明.zip
- 【无人机路径规划】用于无人机路径规划的多目标 PSO实现Matlab代码.rar
- 【无线传感器】基于 Mamdani 模糊推理系统改进无线传感器网络路由和数据包传递附Matlab代码.rar
- 【物理应用】基于Matlab计算并绘制一维量子和经典谐振子的波函数和概率分布.rar
- 【物理应用】使用提升算子计算量子谐振子的激发态研究附Matlab代码.rar
- 【物理】弹簧-质量-阻尼器系统行为分析附Matlab代码.rar
- 【物理应用】基于Zernike 多项式在圆形、六边形、椭圆形、矩形或环形瞳孔上应用Matlab代码实现.rar
- 【物理应用】基于物理场的动态模式分解(piDMD)研究附Matlab代码.rar
- 【信号处理】天线分集与空时编码技术——空时格码matlab代码.rar
- 【信道估计】基于鲸鱼优化算法的5G信道估计Matlab代码.rar
- 【物流选址】基于免疫优化算法的物流配送中心选址规划研究Matlab实现.rar
- proteus图,重庆邮电大学,单片机实验
- 【信号去噪】基于马氏距离和EDF统计IEE-TSP小波的多元信号去噪方法研究附Matlab代码.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈