About final keyword and memory allocation

Hi all,

my question is about final keyword and memory allocation.

Supposing a code like the next one. I ask if the 'msg' object in the dispatch() method allocate memory that is never freed or if it is freed when a new call to dispatch() is done (anser keep allocated only the last allocated msg.

publicfinalclass SocketConn{

privatestaticvolatile DataFrame answer =new DataFrame();

privatevoid dispatch(byte[] data){

final DataFrame msg =new DataFrame(data);

SwingWorker worker =new SwingWorker(){

public Object construct(){

dispatchREQ(msg);

returnnull;

}

};

worker.start();

}

privatevoid dispatchREQ(DataFrame msg){

...

answer = msg;

...

}

}

Thank's in advance for any answer.

[1683 byte] By [amaglio.ca] at [2007-11-27 9:25:26]
# 1

What will happen is that when the SwingWorker object is created it will contain an anonymous copy of the DataFrame reference as an instance field. The original msg variable will be released as normal at the end of the dispatch method but the SwingWorker will still reference it (the DataFrame object that is, not the local variable), and because the SwingWorker is supported by a live thread, that reference will remain reachable until the thread finishes.

Message was edited by:

malcolmmc

malcolmmca at 2007-7-12 22:22:03 > top of Java-index,Java Essentials,Java Programming...