I have a servlet that receives xml data that is posted to this endpoint.
I convert the xml to a java object etc., and I want to make sure I am minimizing memory usage during this process.
What is the best way for me to analyze my memory usage and measure how things fluctuate as I post xml data to the servlet?
For tracing this always went well for me (output in console):
2013-01-05T22:52:13.954+0100: 15918369.557: [GC 15918369.557: [DefNew: 65793K->227K(98304K), 0.0031220 secs] 235615K->170050K(491520K), 0.0033220 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
Reference: Virtual Machine Garbage Collection Tuning
VisualVM非常适合用来衡量您的应用程序,它提供了各种图表,用于显示运行时CPU信息,内存使用情况和线程等。
You can use JMX :
HeapMemory:
public String monitorMemory() {
StringBuilder sb = new StringBuilder("Memory:");
MemoryMXBean mmbean = ManagementFactory.getMemoryMXBean();
MemoryUsage hmu = mmbean.getHeapMemoryUsage();
sb.append("[HeapMemoryUsage:");
sb.append(" Used=" + formatBytes(hmu.getUsed()));
sb.append(" Committed=" + formatBytes(hmu.getCommitted()));
sb.append("]");
MemoryUsage nhmu = mmbean.getNonHeapMemoryUsage();
sb.append("[NonHeapMemoryUsage:");
sb.append(" Used=" + formatBytes(nhmu.getUsed()));
sb.append(" Committed=" + formatBytes(nhmu.getCommitted()));
sb.append("]");
return sb.toString();
}
MemoryPool:
public String monitorMemoryPool() {
StringBuilder sb = new StringBuilder("MemoryPool:");
List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
for (MemoryPoolMXBean p : pools) {
sb.append("[" + p.getName() + ":");
MemoryUsage u = p.getUsage();
sb.append(" Used=" + formatBytes(u.getUsed()));
sb.append(" Committed=" + formatBytes(u.getCommitted()));
sb.append("]");
}
return sb.toString();
}
Run the code above before and after you code or method.
This maybe not accurate enough while you web service process other requets.
As @Gavin Xiong said, you can use some tools with UI like VisualVM .
But, you'd better not use UI tools on you production environment.
Single Thread
test will be more accurate.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.