M
Matt Goodwin
I have a program that looks at a directory for files and if there are
some files it reads the file contents into a byte stream and calls a
web service method. The problem I am having is that system memory
never gets released. I have profiled the application with JProbe and
memory gets collected from the heap, but when I look at the Windows
Task Manager it keeps going up and up until I get an out of memory
exception. The weird thing is when I comment out the web service call
the memory never climbs. I think I am getting rid of all references
to objects. Below are some relevant pieces of the code.
// code snippet from main method
Thread t1 = new Thread() {
public void run() {
while(true) {
try {
List files = DirectoryScanner.getFiles(dirName,fileExt,startTime); //
static method
Iterator itr = files.iterator();
while(itr.hasNext()) {
final File file = (File)itr.next();
log.info("Filename: " + file.getName());
new Thread(new Runnable() {
public synchronized void run() {
FileTransfer.TransferFile(file, logDirName, failedDirName,
successDirName); //static method
}}).start();
}
files=null;
sleep(interval);
} catch(Exception e) {
log.info("Caught exception setting up receiver " + e.getMessage());
e.printStackTrace();
}
}
}
};
t1.setName("DirectoryScanner Thread");
t1.start();
// code from FileTransfer.TransferFile method
public static void TransferFile(File file, String logDirName, String
failedDirName, String successDirName ) {
String fileHeader=null;
String fileContents=null;
TransformFileSoap binding=null;
// read file
try {
fileHeader = getFileHeader(file.getName());
if(fileHeader==null){
errLog.error("Filename: " + file.getAbsolutePath() + " not using
correct naming convention");
log.error("Filename: " + file.getAbsolutePath() + " not using correct
naming convention");
return;
}
fileContents = getFileContents(file);
if(fileContents==null){
errLog.error("Could not retrieve contents of file: " +
file.getAbsolutePath());
log.error("Could not retrieve contents of file: " +
file.getAbsolutePath());
return;
}
// call web service
try {
binding = new TransformFileLocator().getTransformFileSoap();
((TransformFileSoapStub)binding).setMaintainSession(false);
} catch(javax.xml.rpc.ServiceException se) {
errLog.error("ServiceException: Could not locate service: " +
se.getMessage());
moveToDirectory(file,failedDirName);
return;
}
try {
binding.convert(fileContents,fileHeader);
} catch(java.rmi.RemoteException jre) {
log.error(jre.getMessage());
errLog.error("File: " + file.getName() + " " + jre.getMessage());
moveToDirectory(file,failedDirName);
return;
}
moveToDirectory(file,successDirName);
appLog.info("File: " + file.getName() + " successfully processed");
} catch(Exception e) {
log.error("Could not process file: " + file.getName() +
e.getMessage());
errLog.error("Could not process file: " + file.getName() +
e.getMessage());
} finally {
fileHeader=null;
fileContents=null;
binding=null;
}
}
Thanks in advance,
Matt
some files it reads the file contents into a byte stream and calls a
web service method. The problem I am having is that system memory
never gets released. I have profiled the application with JProbe and
memory gets collected from the heap, but when I look at the Windows
Task Manager it keeps going up and up until I get an out of memory
exception. The weird thing is when I comment out the web service call
the memory never climbs. I think I am getting rid of all references
to objects. Below are some relevant pieces of the code.
// code snippet from main method
Thread t1 = new Thread() {
public void run() {
while(true) {
try {
List files = DirectoryScanner.getFiles(dirName,fileExt,startTime); //
static method
Iterator itr = files.iterator();
while(itr.hasNext()) {
final File file = (File)itr.next();
log.info("Filename: " + file.getName());
new Thread(new Runnable() {
public synchronized void run() {
FileTransfer.TransferFile(file, logDirName, failedDirName,
successDirName); //static method
}}).start();
}
files=null;
sleep(interval);
} catch(Exception e) {
log.info("Caught exception setting up receiver " + e.getMessage());
e.printStackTrace();
}
}
}
};
t1.setName("DirectoryScanner Thread");
t1.start();
// code from FileTransfer.TransferFile method
public static void TransferFile(File file, String logDirName, String
failedDirName, String successDirName ) {
String fileHeader=null;
String fileContents=null;
TransformFileSoap binding=null;
// read file
try {
fileHeader = getFileHeader(file.getName());
if(fileHeader==null){
errLog.error("Filename: " + file.getAbsolutePath() + " not using
correct naming convention");
log.error("Filename: " + file.getAbsolutePath() + " not using correct
naming convention");
return;
}
fileContents = getFileContents(file);
if(fileContents==null){
errLog.error("Could not retrieve contents of file: " +
file.getAbsolutePath());
log.error("Could not retrieve contents of file: " +
file.getAbsolutePath());
return;
}
// call web service
try {
binding = new TransformFileLocator().getTransformFileSoap();
((TransformFileSoapStub)binding).setMaintainSession(false);
} catch(javax.xml.rpc.ServiceException se) {
errLog.error("ServiceException: Could not locate service: " +
se.getMessage());
moveToDirectory(file,failedDirName);
return;
}
try {
binding.convert(fileContents,fileHeader);
} catch(java.rmi.RemoteException jre) {
log.error(jre.getMessage());
errLog.error("File: " + file.getName() + " " + jre.getMessage());
moveToDirectory(file,failedDirName);
return;
}
moveToDirectory(file,successDirName);
appLog.info("File: " + file.getName() + " successfully processed");
} catch(Exception e) {
log.error("Could not process file: " + file.getName() +
e.getMessage());
errLog.error("Could not process file: " + file.getName() +
e.getMessage());
} finally {
fileHeader=null;
fileContents=null;
binding=null;
}
}
Thanks in advance,
Matt