I need to track the amount of memory consumed by threads created by my application. The idea is to take corrective action if the greedy thread consumes too much memory. I mentioned How much memory does my java thread take? . One suggestion for this link is to use getThreadAllocatedBytes in ThreadMXBean. , I experimented with getThreadAllocatedBytes with the following task.
List<Long> primes = new ArrayList<Long>(); long i = 0; while (true) { primes.add(++i); if ((i % 10) == 0) { primes.clear(); System.runFinalization(); System.gc(); } }
I have been doing this task on four threads for a considerable amount of time. Although the operation does not accumulate memory continuously, the values ββreturned by getThreadAllocatedBytes continue to increase and do not drop even once. This means that getThreadAllocatedBytes does not return the actual amount of memory in the heap used by the thread. It returns the total amount of memory allocated on the heap for the stream since it started. My platform information is as follows:
Linux PG85213.egi.ericsson.com 3.5.0-030500-generi # 201207211835 SMP Sat Jul 21 22:35:55 ββUTC 2012 x86_64 x86_64 x86_64 GNU / Linux java version "1.7.0_45"
Java (TM) SE Runtime Environment (build 1.7.0_45-b18) Java HotSpot (TM) 64-bit server VM (build 24.45-b08, mixed mode)
Is the above behavior the desired behavior of getThreadAllocatedBytes ? If so, there is no way to find the effective memory in the heap used by the thread.
Listing the complete help system:
package workbench; import java.lang.management.ManagementFactory; import com.sun.management.ThreadMXBean; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; import java.util.logging.Level; import java.util.logging.Logger; public class AnotherWorkBench { private static final CountDownLatch latch = new CountDownLatch(4); static final List<Long> threadIds = Collections.synchronizedList(new ArrayList<Long>()); private void dummyJob() { List<Long> primes = new ArrayList<Long>(); long i = 0; while (true) { primes.add(++i); if ((i % 10) == 0) { primes.clear();
java memory-management multithreading memory
Sarveswaran meeenakshi sundaram
source share