java 局部变量 内存,java局部变量的内存分配

I have a java application which uses a SerialPortEvent which will be called continously ,

public void serialEvent(SerialPortEvent evt) {

if (evt.getEventType() == SerialPortEvent.DATA_AVAILABLE) {

try {

StringBuilder sBuilder = new StringBuilder();

int length = input.available();

byte[] array = new byte[length];

int numBytes = input.read(array);

.......

......

}

i print the array variable contents in a text pane.

I have a scenario in which the event will be called continuosly , it makes the the windows Memory(private Working set) increase gradually and doesn't stop.

My question is, whether creating new variables every time the event is called makes use of memory??

i simply get contents and print it in JTextpane and nothing else.

解决方案

Creating variables as such doesn't create a memory leak. The leak happens when you keep a reference to a local variable somewhere.

My guess is that you eventually append the content of sBuilder to the JTextpane which of course keeps the content around permanently.

The solution is to check the length of the JTextpane (number of lines). If there are too many, then remove some. That way, you always keep, say, 1000 lines in memory and the consumption will be in check.

Related:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值