I need to monitor the amount of memory consumed by threads spawned by my application. The idea is to take corrective actions, if a greedy thread consumes too much of memory. I have referred to How much memory does my java thread take?. One of the suggestions on that link is to use getThreadAllocatedBytes
in ThreadMXBean.
I experimented with getThreadAllocatedBytes
with the following job.
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 run this job on four threads for considerable time. Though the job does not accumulate memory continuously, the values returned by getThreadAllocatedBytes
keeps increasing and does not go down even once. This implies that getThreadAllocatedBytes
does not return the actual amount of memory on heap used by the thread. It returns the total amount of memory allocated on the heap for the thread since it was started. My platform details are as follows:
Linux PG85213.egi.ericsson.com 3.5.0-030500-generic #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 desired behavior of getThreadAllocatedBytes
?
If so, is there no way to find effective memory on heap used by a thread.
Am listing the complete program for reference:
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();
//introduce sleep to prevent process hogging
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(AnotherWorkBench.class.getName()).log(Level.SEVERE, null, ex);
}
System.runFinalization();
System.gc();
}
}
}
private void runDummyJobs() {
Runnable dummyJob = new Runnable() {
@Override
public void run() {
threadIds.add(Thread.currentThread().getId());
latch.countDown();
dummyJob();
}
};
Runnable memoryMonitorJob = new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " : Monitor thread started");
ThreadMXBean threadMxBean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
threadMxBean.setThreadAllocatedMemoryEnabled(true);
while (true) {
for (Long threadId : threadIds) {
System.out.println(Thread.currentThread().getName() + " : Thread ID : " + threadId + " : memory = " + threadMxBean.getThreadAllocatedBytes(threadId) + " bytes");
}
//wait between subsequent scans
try {
System.out.println(Thread.currentThread().getName() + " : secondary sleep");
Thread.currentThread().sleep(5000);
System.out.println(Thread.currentThread().getName() + " : out of secondary sleep");
} catch (InterruptedException ex) {
Logger.getLogger(WorkBench.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
};
Executors.newSingleThreadExecutor().submit(dummyJob);
Executors.newSingleThreadExecutor().submit(dummyJob);
Executors.newSingleThreadExecutor().submit(dummyJob);
Executors.newSingleThreadExecutor().submit(dummyJob);
try {
latch.await();
} catch (InterruptedException ex) {
Logger.getLogger(AnotherWorkBench.class.getName()).log(Level.SEVERE, null, ex);
}
Executors.newSingleThreadExecutor().submit(memoryMonitorJob);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new AnotherWorkBench().runDummyJobs();
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…