手动调用垃圾回收方法

当WEB站点内存溢出时, 手动调用垃圾回收方法, 以缓解内存压力,增加分析问题的时间。  


<%@ page contentType="text/html; charset=GBK"%>
<%
		long totalMemory = Runtime.getRuntime().totalMemory();

		long freeMemory = Runtime.getRuntime().freeMemory();
		long usedMemory = totalMemory - freeMemory;
		java.text.NumberFormat format = new java.text.DecimalFormat("###,###");
		String memoryInfo="垃圾回收前,内存使用情况为:已用内存/总内存:" + format.format(usedMemory)
				+ "/" + format.format(totalMemory);
		System.out.println(memoryInfo);
		out.println("<br>");
		out.println(memoryInfo);
		out.println("<br>");

System.out.println("垃圾回收开始,时间为:"+System.currentTimeMillis());
out.println("垃圾回收开始,时间为:"+System.currentTimeMillis());
System.gc();
		 totalMemory = Runtime.getRuntime().totalMemory();

		 freeMemory = Runtime.getRuntime().freeMemory();
		 usedMemory = totalMemory - freeMemory;
         memoryInfo="垃圾回收前,内存使用情况为:已用内存/总内存:" + format.format(usedMemory)
				+ "/" + format.format(totalMemory);
		System.out.println(memoryInfo);
		out.println("<br>");
		out.println(memoryInfo);
System.out.println("垃圾回收结束,时间为:"+System.currentTimeMillis());
		out.println("<br>");
out.println("垃圾回收结束,时间为:"+System.currentTimeMillis());
%>

你可能感兴趣的:(html,jsp,Web)