Memory leak

E

Erik

I have a memory leak in one function, but I have no idea why it leaks.
This is the function:

protected void ProcessState_FILE_SEND(){
byte[] data = new byte[readSize];
int dataRead = 0;

//If it is the first file. Also a file discriptor has to be send.
if(currentFile == -1){
NextFile(); //Get the first file.
try{ //Send the file discriptor.
Send_File_Discriptor(sessionID,
filelistWorking[currentFile].getName(),
filelistWorking[currentFile].length(), GenerateFileID());
} catch (IOException e1) {
System.out.println("Error reading file:
"+filelistWorking[currentFile].getName());
Disconnect();

return;
}
}

try {
dataRead = ReadFile(data); //Read the file data.
if(dataRead != -1){
Send_File_Data(sessionID, data); //Send the data.
bytesSend += dataRead;
}
} catch (IOException e1) {
System.out.println("Error reading file:
"+filelistWorking[currentFile].getName());
Disconnect();

return;
}
//The file is done reading and the next file should be send and the
beginning of his data.
if(dataRead == -1){
UpdateStats(); //Update the stats so the correct bytesSend is
added.
NextFile(); //Get the next file.
if(currentFile < filelistWorking.length){ //If it is not the last
file..
try { //Send discriptor, read and send the data.
Send_File_Discriptor(sessionID,
filelistWorking[currentFile].getName(),
filelistWorking[currentFile].length(), GenerateFileID());
dataRead = ReadFile(data);
Send_File_Data(sessionID, data);
bytesSend += dataRead;
} catch (IOException e1) {
System.out.println("Error reading file:
"+filelistWorking[currentFile].getName());
Disconnect();

return;
}
}else{ //If there are no files left to send.
try{
Send_Got_Files_Correctly(sessionID, GetFilelist());
state = LAST_CHECK;
}catch(IOException e){
Disconnect();

return;
}
}
}

}

When analyzing the program I noticed that the byte[] data variable
(declared in the beginning of the method) eats all my memory and
doesn't get released. Does anyone knows why this happens and how to
fix this?

Erik
 
E

Erik

I have a memory leak in one function, but I have no idea why it leaks.
This is the function:

protected void ProcessState_FILE_SEND(){
byte[] data = new byte[readSize];
int dataRead = 0;

//If it is the first file. Also a file discriptor has to be send.
if(currentFile == -1){
NextFile(); //Get the first file.
try{ //Send the file discriptor.
Send_File_Discriptor(sessionID,
filelistWorking[currentFile].getName(),
filelistWorking[currentFile].length(), GenerateFileID());
} catch (IOException e1) {
System.out.println("Error reading file:
"+filelistWorking[currentFile].getName());
Disconnect();

return;
}
}

try {
dataRead = ReadFile(data); //Read the file data.
if(dataRead != -1){
Send_File_Data(sessionID, data); //Send the data.
bytesSend += dataRead;
}
} catch (IOException e1) {
System.out.println("Error reading file:
"+filelistWorking[currentFile].getName());
Disconnect();

return;
}
//The file is done reading and the next file should be send and the
beginning of his data.
if(dataRead == -1){
UpdateStats(); //Update the stats so the correct bytesSend is
added.
NextFile(); //Get the next file.
if(currentFile < filelistWorking.length){ //If it is not the last
file..
try { //Send discriptor, read and send the data.
Send_File_Discriptor(sessionID,
filelistWorking[currentFile].getName(),
filelistWorking[currentFile].length(), GenerateFileID());
dataRead = ReadFile(data);
Send_File_Data(sessionID, data);
bytesSend += dataRead;
} catch (IOException e1) {
System.out.println("Error reading file:
"+filelistWorking[currentFile].getName());
Disconnect();

return;
}
}else{ //If there are no files left to send.
try{
Send_Got_Files_Correctly(sessionID, GetFilelist());
state = LAST_CHECK;
}catch(IOException e){
Disconnect();

return;
}
}
}

}

When analyzing the program I noticed that the byte[] data variable
(declared in the beginning of the method) eats all my memory and
doesn't get released. Does anyone knows why this happens and how to
fix this?

Erik

And method looks Send_File_Data like this:

protected void Send_File_Data(int SessionID, byte[] data)throws
IOException{
FileData payload = new FileData(SessionID, data);

Message msg = new Message(FILE_DATA);
msg.SetPayload(payload);

AddToSendQueue(msg);
}
 
M

Martin Gregorie

byte[] data = new byte[readSize];

I notice that this buffer is only initialised once and has a greater scope
than data buffer needs in your code. I also notice that you've capitalised
your method names, which is confusing.

What does ReadFile() do? Are you quite sure it isn't extending data?
 
E

Erik

byte[] data = new byte[readSize];

I notice that this buffer is only initialised once and has a greater scope
than data buffer needs in your code. I also notice that you've capitalised
your method names, which is confusing.

What does ReadFile() do? Are you quite sure it isn't extending data?

The ReadFile method:

protected int ReadFile(byte[] data)throws IOException{
int dataRead = 0;

try {
dataRead = fileInput[currentFile].read(data);
} catch (IOException e) {
throw e;
}
if(dataRead == -1){
fileInput[currentFile].close();
return -1;
}

return dataRead;
}
 
M

Martin Gregorie

byte[] data = new byte[readSize];

I notice that this buffer is only initialised once and has a greater scope
than data buffer needs in your code. I also notice that you've capitalised
your method names, which is confusing.

What does ReadFile() do? Are you quite sure it isn't extending data?

The ReadFile method:

protected int ReadFile(byte[] data)throws IOException{
int dataRead = 0;

try {
dataRead = fileInput[currentFile].read(data);
} catch (IOException e) {
throw e;
}
if(dataRead == -1){
fileInput[currentFile].close();
return -1;
}

return dataRead;
}

There's still a lot of stuff you're not telling us, such as what type of
objects make up fileInput[].
 
E

Erik

On Sat, 19 Apr 2008 14:09:46 -0700, Erik wrote:
byte[] data = new byte[readSize];
I notice that this buffer is only initialised once and has a greater scope
than data buffer needs in your code. I also notice that you've capitalised
your method names, which is confusing.
What does ReadFile() do? Are you quite sure it isn't extending data?
The ReadFile method:
protected int ReadFile(byte[] data)throws IOException{
int dataRead = 0;
try {
dataRead = fileInput[currentFile].read(data);
} catch (IOException e) {
throw e;
}
if(dataRead == -1){
fileInput[currentFile].close();
return -1;
}
return dataRead;
}

There's still a lot of stuff you're not telling us, such as what type of
objects make up fileInput[].

Sorry :)

fileInput is a DataInputStream[]
FileData and Message are just classes to transport the data, they just
hold the data.
 
E

Erik

On Sat, 19 Apr 2008 14:09:46 -0700, Erik wrote:
byte[] data = new byte[readSize];
I notice that this buffer is only initialised once and has a greater scope
than data buffer needs in your code. I also notice that you've capitalised
your method names, which is confusing.
What does ReadFile() do? Are you quite sure it isn't extending data?
--
martin@ | Martin Gregorie
gregorie. |
org | Zappa fan & glider pilot
The ReadFile method:
protected int ReadFile(byte[] data)throws IOException{
int dataRead = 0;
try {
dataRead = fileInput[currentFile].read(data);
} catch (IOException e) {
throw e;
}
if(dataRead == -1){
fileInput[currentFile].close();
return -1;
}
return dataRead;
}
There's still a lot of stuff you're not telling us, such as what type of
objects make up fileInput[].

Sorry :)

fileInput is a DataInputStream[]
FileData and Message are just classes to transport the data, they just
hold the data.

I think I found the problem. The messages with the byte[] data are
send across a network
using a ObjectOutputStream. I think that the ObjectOutputStream caches
the data. I thought I
had fixed that problem by calling the flush method of the
OutputStream, but apparently this
is not working. So how do I clear the cached data of the
ObjectOutputStream?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top