Topic
1 reply Latest Post - ‏2008-05-29T12:20:25Z by SystemAdmin
SystemAdmin
SystemAdmin
235 Posts
ACCEPTED ANSWER

Pinned topic Java read/write big file issue in Linux

‏2008-02-13T09:35:29Z |
/*
problem: when write big file in Linux Sles platform, memory is occupied by large
ammount and even when the operation complete, the memory resource still
can not be released.
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Test
{
public static void main(String[] args)
{
try
{
////big file path
String oldPath = "/home/db2inst1/frdmon_bk/FRDMON.0.db2inst1.NODE0000.CATN0000.20071029120106.001_sdasdasd";
String newPath = "/tmp/_COPIED_";
int byteread = 0;
BufferedInputStream bufIn = new BufferedInputStream(new FileInputStream(oldPath));
BufferedOutputStream bufOut = new BufferedOutputStream(new FileOutputStream(newPath));
byte[] buffer = new byte1024 * 64;
System.out.println("Start...");
while ((byteread = bufIn.read(buffer)) != -1)
{
////[?]each loop will eat memory by size of the buffer
bufOut.write(buffer, 0, byteread);
Thread.sleep(10);
}
bufIn.close();
bufOut.close();

////[?]memory occupied by size of the big file, and can not be released.

////[?]if delete the new file, resource will be released.
//File newfile = new File(newPath);
//newfile.delete();
//System.gc();
//System.runFinalization();
System.out.println("End");

/** run in Linux, cmd:
* /opt/IBM/WebSphere/AppServer/java/bin/java /tmp/Test.class
*
* After set JVM heap size16M~32M of websphere application and run in websphere,
* it seems that no such problem. But we have to set the heap size with value0~512M for other programe.
*
* And run in windows also has no such problem, the size of memory used is about the same as the
* buffer size during running and release when finish.
*/
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Updated on 2008-05-29T12:20:25Z at 2008-05-29T12:20:25Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    235 Posts
    ACCEPTED ANSWER

    Re: Java read/write big file issue in Linux

    ‏2008-05-29T12:20:25Z  in response to SystemAdmin
    BufferedInput and BufferedOutput reduces physical read and writes by storing data in memory. If this is causing you a problem, try w/o Buffered objects.

    Also try setting the objects to null when you are done with them.