alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Akka/Scala example source code file (MemoryUsageSnapshotting.scala)

This example Akka source code file (MemoryUsageSnapshotting.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Akka and Scala source code examples by using tags.

All credit for the original source code belongs to akka.io; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Akka tags/keywords

akka, double, heapmemoryusage, long, memoryusagesnapshotting, metrics, metricsprefix, nonheapmemoryusage, string, test, testing, testkit, totalmemoryusage

The MemoryUsageSnapshotting.scala Akka example source code

/**
 * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
 */
package akka.testkit.metrics

import com.codahale.metrics._
import com.codahale.metrics.jvm

private[akka] trait MemoryUsageSnapshotting extends MetricsPrefix {
  this: jvm.MemoryUsageGaugeSet ⇒

  // accessing metrics in order to not to duplicate mxBean access too much

  def getHeapSnapshot = {
    val metrics = getMetrics
    HeapMemoryUsage(
      metrics.get(key("heap-init")).asInstanceOf[Gauge[Long]].getValue,
      metrics.get(key("heap-used")).asInstanceOf[Gauge[Long]].getValue,
      metrics.get(key("heap-max")).asInstanceOf[Gauge[Long]].getValue,
      metrics.get(key("heap-committed")).asInstanceOf[Gauge[Long]].getValue,
      metrics.get(key("heap-usage")).asInstanceOf[RatioGauge].getValue)
  }

  def getTotalSnapshot = {
    val metrics = getMetrics
    TotalMemoryUsage(
      metrics.get(key("total-init")).asInstanceOf[Gauge[Long]].getValue,
      metrics.get(key("total-used")).asInstanceOf[Gauge[Long]].getValue,
      metrics.get(key("total-max")).asInstanceOf[Gauge[Long]].getValue,
      metrics.get(key("total-committed")).asInstanceOf[Gauge[Long]].getValue)
  }

  def getNonHeapSnapshot = {
    val metrics = getMetrics
    NonHeapMemoryUsage(
      metrics.get(key("non-heap-init")).asInstanceOf[Gauge[Long]].getValue,
      metrics.get(key("non-heap-used")).asInstanceOf[Gauge[Long]].getValue,
      metrics.get(key("non-heap-max")).asInstanceOf[Gauge[Long]].getValue,
      metrics.get(key("non-heap-committed")).asInstanceOf[Gauge[Long]].getValue,
      metrics.get(key("non-heap-usage")).asInstanceOf[RatioGauge].getValue)
  }

  private def key(k: String) = prefix + "." + k

}

private[akka] case class TotalMemoryUsage(init: Long, used: Long, max: Long, comitted: Long) {

  def diff(other: TotalMemoryUsage): TotalMemoryUsage =
    TotalMemoryUsage(
      this.init - other.init,
      this.used - other.used,
      this.max - other.max,
      this.comitted - other.comitted)

}

private[akka] case class HeapMemoryUsage(init: Long, used: Long, max: Long, comitted: Long, usage: Double) {

  def diff(other: HeapMemoryUsage): HeapMemoryUsage =
    HeapMemoryUsage(
      this.init - other.init,
      this.used - other.used,
      this.max - other.max,
      this.comitted - other.comitted,
      this.usage - other.usage)
}

private[akka] case class NonHeapMemoryUsage(init: Long, used: Long, max: Long, comitted: Long, usage: Double) {

  def diff(other: NonHeapMemoryUsage): NonHeapMemoryUsage =
    NonHeapMemoryUsage(
      this.init - other.init,
      this.used - other.used,
      this.max - other.max,
      this.comitted - other.comitted,
      this.usage - other.usage)
}

Other Akka source code examples

Here is a short list of links related to this Akka MemoryUsageSnapshotting.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.