Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 504101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:27:43+00:00 2026-05-13T06:27:43+00:00

Given a class (ex: foo.bar.MyClass), how can I get a count of the number

  • 0

Given a class (ex: foo.bar.MyClass), how can I get a count of the number of instances of that class existing within the JVM?

Thanks

EDIT: I’m looking for the code that will retrieve this count.

EDIT: More specifically, an implementation of the method Integer getInstanceCount(Class c)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:27:43+00:00Added an answer on May 13, 2026 at 6:27 am

    There are three or even four possible answers depending on the exact context. Here they are:

    1. If you can change the source code of the class, then you can add a static counter (see @The Feast’s answer)

    2. If you’re having memory problems then you should use a memory profiler (see @winSharp93) such as: http://java.sun.com/javase/6/docs/technotes/tools/share/jhat.html,

    3. If you don’t have access to the source code you can register an instrumentation agent with the JVM which will inject that static field into the classes that interest you (not trivial, but possible). A tutorial on instrumentation is available here: http://today.java.net/pub/a/today/2008/04/24/add-logging-at-class-load-time-with-instrumentation.html

    4. If you were looking for some Java API that exposes this information, then – sadly – the answer is that no such facility is supported.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an assembly that contains namespace Foo{public class Bar;} How could I create an
This is just an example, but given the following model: class Foo(models.model): bar =
Given the following class public class Foo { public int FooId { get; set;
Given a variable foo of type FooClass* and a member variable in that class
given this code: class Foo def bar return Bar.new end end class Bar ...
In F#, given the following class: type Foo() = member this.Bar<'t> (arg0:string) = ignore()
Given: namespace A { class Foo; class Bar; } namespace B { class Foo;
Example: // can't forward declare with class Foo::Bar // actual class class Foo {
Given: case class Foo(a: Int, b: String, c: Double) you can say: val params
I have a simple class that defines some constants, e.g.: module Foo class Bar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.