最近项目当中出现一个问题,内存一直居高不下!初步怀疑是StringBuffer没有释放的问题。专门为StringBuffer做了个小测试。代码如下:public class TestBuffer extends JFrame {
static JLabel lblmb; public TestBuffer() {
getContentPane().setLayout(null); JButton button = new JButton("开 始");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
append();
}
});
button.setBounds(139, 35, 95, 25);
getContentPane().add(button); lblmb = new JLabel("内存:0MB");
lblmb.setBounds(58, 116, 236, 15);
getContentPane().add(lblmb); JButton btnGc = new JButton("GC");
btnGc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.err.println("手动GC.");
System.gc();
System.err.println("手动GC完毕.");
}
});
btnGc.setBounds(139, 81, 95, 25);
getContentPane().add(btnGc);
new Thread(new Runnable() { @Override
public void run() {
while (true) {
TestBuffer.this.lblmb.setText("内存:" + getUsedMemoryMB()
+ " MB");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public static void main(String[] args) {
TestBuffer test = new TestBuffer();
test.setVisible(true);
test.setBounds(0, 0, 300, 200);
} private static void append() {
StringBuffer sb = new StringBuffer();
System.err.println("拼接开始!");
for (int i = 0; i < 100000; i++) {
sb.append("枯开发拉动副经理卡上地方建立卡的时间分厘卡世界的风口浪尖圣诞快乐附近啊圣诞快乐附近考虑到");
}
System.err.println("拼接结束!");
} public static double getUsedMemoryMB() {
return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime()
.freeMemory()) / 1024D / 1024D;
} private static final long serialVersionUID = 1L;
}这个测试代码不知道对不对!使用这个代码进行测试后发现当不进行手动GC时,内存一直没有下来。当手动GC后,内存释放!
  请求高人!
      谢谢!
 分少再加!