JDK-6422028 : Per-thread allocation tally
  • Type: Enhancement
  • Component: core-svc
  • Sub-Component: java.lang.management
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2006-05-04
  • Updated: 2010-04-02
  • Resolved: 2006-05-10
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
It would be very helpful to be able to ask how much memory has been allocated by a certain thread. For example, Thread could offer a (native) method:

  public long allocationTotal();

When a thread is new and has not run, this would be zero. As it runs, all allocations which occur within it would increment this number.

Rollover would be acceptable; only the difference between two readings, within a reasonably small amount of time, would be meaningful. (In some ways, it would be like the millisecond clock.)

JUSTIFICATION :
Often runaway or unintentional memory usage is hard to track down, especially if the objects created are short-lived. Allocation profiling helps but involves a lot of overhead, which often cannot be justified in the live systems where occasional, hard-to-reproduce memory overuse occurs.

I suspect this counter could be maintained very easily, with negligible overhead, and would allow meaningful paths of execution -- such as the servicing of a single web hit -- to be compared in their memory consumption. Those paths  which 'stick out' (far above the norms for expected-similar operations) could be examined in more depth.

This feature could also supercede the RFE here -- http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6173675 ("M&M: approximate memory allocation rate per thread") -- as it would provide the raw numbers necessary to calculate various rates as desired. (Rate per time period, rate per request serviced, rate per database operation, etc.)

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The following code would give meaningful info in the 'useDuring' variable:

long useBefore = Thread.currentThread().allocationTotal();
doSomething();
long useDuring = Thread.currentThread().allocationTotal() - useBefore;

ACTUAL -
No such method exists.

Comments
EVALUATION As for 6173675, we consider to provide the raw amount of approximate per-thread memory usage and the rate can be calculated by sampling the raw data. Exact per-thread allocation would have very high performance impact to the running application. We believe approximate per-thread memory stat will satisfy this need. I'm going to close this as a dup of 6173675 and update 6173675 to clarify the raw amount vs rate.
10-05-2006

EVALUATION Adding even a single machine instruction to the main object allocation path of Java SE would create significant overhead. Having to (atomically) add to another object's field for each allocation would greatly increase cost unless that object can be located trivially, to say nothing of the effect of the required memory synchronizations. The net effect could be to intrude on and distort the behavior of multithreaded applications and rob performance for the sake of a relatively rare use case. There are probably other ramifications. But perhaps allocation tallying could be offered as a monitoring feature that is enabled and disabled. Redispatching this to classes_management.
08-05-2006