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: