Jeet
Jeet

Reputation: 1046

How to get the statistics of the garbage collected objects?

Is it possible to see the java objects (and their class type) that were made null and which are

  1. Not yet garbage collected/cleaned
  2. garbage collected/cleaned.

This statistic will help to know that how many objects repeatedly created (by a wrong logic) instead of creating one time.

Upvotes: 0

Views: 703

Answers (2)

Stephen C
Stephen C

Reputation: 718816

I think that it is theoretically possible, though frankly you would be crazy to try it.

The route to finding unreachable objects is to use the Java VM Tool Interface (JVMTI) to iterate over all objects in the heap (reachable or unreachable) in order to find the one you are looking for. Then you extract its state via JVMTI and (somehow) reify it so that you can display it.

Normally you would do this in a separate JVM; e.g. the one running your debugger or profiling tool. But it is possible for an application to attach an agent to itself, and use it to dig around in the JVM. However, this is not the intended usage for JVMTI, and I would anticipate that there could be "hazards" in doing this.

You can read more here:

But please don't blame me if you go crazy trying to get this working.


UPDATE I concur with Marko's note that you are unlikely to learn anything significant by looking at unreachable objects.

Upvotes: 2

Marko Topolnik
Marko Topolnik

Reputation: 200158

to display the unwanted or null java objects that are not cleaned by the java garbage process

This is not a well-defined concept; at least there are no useful definitions which would give you anything of relevance.

A piece of memory where an object was allocated can be considered free for all practical purposes as soon as that object has become unreachable. The amount of memory that the block represents is available to the JVM allocator in the sense that no out-of-memory event will happen due to that block being "overlooked" in some sense.

Further note that many "garbage collection" algorithms usually do the exact opposite: they find live objects and relocate them so they occupy a contiguous block of memory. The algorithms are simply oblivious to "garbage" objects and treat them as just empty space.

So, even if you manage to write up some low-level Java Agent-based module which will enumerate all the objects on the heap, you will not gain any interesting insight: the unreachable objects which you encounter will just happen to linger on because the JVM has not yet felt the need to reuse their memory.

Upvotes: 1

Related Questions