Skip to content
A Java agent that rewrites bytecode to instrument allocation sites
Java Python Shell
Branch: master
Clone or download
jhmanson Update maven build for 2018.
Until we can push to Maven repos via Bazel, we need to keep the Maven
build going.

PiperOrigin-RevId: 218543432
Latest commit 58d8473 Oct 24, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore Spell .gitignore correctly. Feb 18, 2015
AUTHORS Being more explicit about copyright notices. Jul 2, 2011
BUILD Initial attempt to move to Bazel. Jun 4, 2018
LICENSE Project import generated by Copybara. May 22, 2018
README.md Update version numbers in README to 3.1.0 and set version in pom.xml … Nov 27, 2017
WORKSPACE Update Bazel Common Oct 23, 2018
builddefs.bzl Remove ASM's module-info.class. Sep 20, 2018
pom.xml Update maven build for 2018. Oct 24, 2018
rules.txt Initial attempt to move to Bazel. Jun 4, 2018

README.md

The Allocation Instrumenter is a Java agent written using the java.lang.instrument API and ASM. Each allocation in your Java program is instrumented; a user-defined callback is invoked on each allocation.

How to get it

The latest release is available from Maven Central as:

<dependency>
  <groupId>com.google.code.java-allocation-instrumenter</groupId>
  <artifactId>java-allocation-instrumenter</artifactId>
  <version>3.1.0</version>
</dependency>

Basic usage

In order to write your own allocation tracking code, you have to implement the Sampler interface and pass an instance of that to AllocationRecorder.addSampler():

AllocationRecorder.addSampler(new Sampler() {
  public void sampleAllocation(int count, String desc, Object newObj, long size) {
    System.out.println("I just allocated the object " + newObj +
      " of type " + desc + " whose size is " + size);
    if (count != -1) { System.out.println("It's an array of size " + count); }
  }
});

You can also use the allocation instrumenter to instrument constructors of particular classes. You do this by instantiating a ConstructorCallback and passing it to ConstructorInstrumenter.instrumentClass():

try {
  ConstructorInstrumenter.instrumentClass(
      Thread.class, new ConstructorCallback<Thread>() {
        @Override public void sample(Thread t) {
          System.out.println("Instantiating a thread");
        }
      });
} catch (UnmodifiableClassException e) {
  System.out.println("Class cannot be modified");
}

For more information on how to get or use the allocation instrumenter, see Getting Started.

You can’t perform that action at this time.