Why Writing a File Cost Such a Memory?

As a test case I tried the next code on my Mac OS X 10.2.3:
import java.io.*;
public class WriteZero {          
public static void main(String[] args) throws Exception{
java.io.BufferedOutputStream bs = new java.io.BufferedOutputStream(new FileOutputStream("zeros"));
int mb=1024*1024;
int mb500 = mb*500;
try{
  int k=0;
    while((1==1)){
         bs.write(0x0);
         k++;
       if(k%mb==0){
            Thread.sleep(200);//inessential but enables to work in the back ground
            System.gc();// tries to free memory
        }else if(k>mb500) break;//50mb
}catch(InterruptedException ie){;
}catch(java.lang.Throwable th){
    System.err.println(th.toString());
}finally{
     if(bs!=null) bs.close();
}This simply writes the size of 500MB of the characters '\u0000.' In the course of writing the free RAM decreases monotonously until it reaches around 6 MB of free memory, recording 919 pageouts. The number 919 of pageouts is very rare on my OS, which has 512MB of RAM. What causes this consumption of memory in simple writing a file?

Something up with either your VM or VM configuration maybe? I just copied and pasted your code, and it's cruising at just under 6MB - which means maybe 2 MB allocated in heap space.
Verbose:gc indicates that full garbage collections are being done about every second, but that the heap is a stable size.
Check your max and min heap-sizes, and also try it on a different VM if you can - there's no reason for this program to blow up memory that way. Also should be no need to call GC at all.

Similar Messages

  • Out of memory error when writing large file

    I have the piece of code below which works fine for writing small files, but when it encounters much larger files (>80M), the jvm throws an out of memory error.
    I believe it has something to do with the Stream classes. If I were to replace my PrintStream reference with the System.out object (which is commented out below), then it runs fine.
    Anyone else encountered this before?
         print = new PrintStream(new FileOutputStream(new File(a_persistDir, getCacheFilename()),
                                                                false));
    //      print = System.out;
              for(Iterator strings = m_lookupTable.keySet().iterator(); strings.hasNext(); ) {
                   StringBuffer sb = new StringBuffer();
                   String string = (String) strings.next();
                   String id = string;
                   sb.append(string).append(KEY_VALUE_SEPARATOR);
                   Collection ids = (Collection) m_lookupTable.get(id);
                   for(Iterator idsIter = ids.iterator(); idsIter.hasNext();) {
                        IBlockingResult blockingResult = (IBlockingResult) idsIter.next();
                        sb.append(blockingResult.getId()).append(VALUE_SEPARATOR);
                   print.println(sb.toString());
                   print.flush();
    } catch (IOException e) {
    } finally {
         if( print != null )
              print.close();
    }

    Yes, my first code would just print the strings as I got them. But it was running out of memory then as well. I thought of constructing a StringBuffer first because I was afraid that the PrintStream wasn't allocating the memory correctly.
    I've also tried flushing the PrintStream after every line is written but I still run into trouble.

  • Why is AE getting error writing a file after finishing a render?

    I have been trying to create an MPEG4 file from a comp.  It renders successfully - at least, it does not display any render errors - but then gets an error when writing the file.  It creates a file, but the file cannot be opened by Quicktime, nor Premiere Pro.

    Use Adobe Media Encoder to create MPEG-4 files.

  • Why has adobe reader stopped reading the pdf files on external SD memory?

    I have Samsung not 2 with  external sd memory that I stored all my pdf files into it , I haven't had any problem with using Adobe reader for reading all files from  my sd memory but for unknown reason I can't see all pdf files from sd anymore. In order to reach the files ,I have to use the folders view tab in the adobe reader instead of all pdf files view  .
    Thanks a lot for any feedback.

    On Android OS 4.1 - 4.3 (Jelly Bean) or older, Adobe Reader for Android version 11.2.x has a known defect in the "All PDFs" view. The All PDFs view does not show the files and folders on an external SD card. A workaround is to use the Folder View instead of the All PDFs view. We have fixed the defect and plan to include the fix in the next release of Reader for Android.
    On Android OS 4.4 (KitKat) only, neither All PDFs view nor Folder View shows any files or folders on an external SD card.  Reader cannot save any changes to the files on an external SD card, either.  We are still working on fixing the problems.  In the meantime, please copy the files from your external SD card to your device's internal storage (e.g. /storage/emulated/0/Documents/...) using a File Manager/Browser app or use a cloud storage service (Acrobat.com, Dropbox, Google Drive, etc.) to store files.  Sorry for the inconvenience.

  • Memory issues writing binary files, using a subVI to do all File Handling, topVI, when run normally files are way too large.

    Problem writing binary files with LV 8.5. I have one VI that does all the interaction/file I/O and another VI for testing. I find when I run the subVI where all the data reading/writing is happening with break points and single stepping through the code all is OK, but when I run normally the files are much larger than they should be and as well when I close the files I get a error. Any insights?  
    Walter

    Waltz wrote:
    I found the problem, I was actually trying to close the files twice.
    I don't see how this could cause the symptoms you described in the first post.
    (It only explains the error on close).
    LabVIEW Champion . Do more with less code and in less time .

  • "Error in writing to file" during AS setup -- ?

    Hello!
    I am having an odd problem":
    Trying to install AS 9.0.2 or AS Infrastructure 9.0.2, I receive an error box saying "Error in writing to file <path>". Sometimes I see four buttons (help/retry/ignore/cancel) and I can ignore and proceed, sometimes there are only three, without ignore, so I have to cancel the installation.
    The thing is, this error box always shows up at different file! So apparently the problem is not with some particular file, but somewhere else. If I go and check the "problematic" file -- hey, it's already there, copied successfully!
    One time I was lucky and I only got one such error and ignore button was there, so I ignored the error and then setup went till the end. But now I need to reinstall AS infrastructure and three times in a row I am getting these errors again and again :(
    Had anyone seen things like that? Is there any resolution to this problem?
    Thank you,
    Sasha.

    For those who're interested, I managed to work-around this problem: instead of installing off the network share, I actually burned 3 CDs and installed AS Infrastructure from them. Things went smoothly. Not sure why it could not install from the network share, but that's how it is.
    Sasha.

  • Could not open the file. Not enough memory

    I have always used fireworks cs4 normally, but today, i experienced a bit of trouble. there is one png file that when i click to open it the fireworks loads but then a message pops up (could not open the file. Not enough memory) after a bit of researching, i found out that this error happens when i bring to stage one of the common libraries menu bars. (menu bar 4). Does anyone know how to fix this?
    ps: I am able to open this file via file-open-*png. Please i really need help. Is the library corrupted maybe?

    Hi,
    It opened on mine (1.2Gb). I don't think it's a memory issue. I've used FWCS4 with 512mb RAM, never had such a problem. (of course it was very slow at the start, and i used to leave for a cup of coffee first and then come back when it was ready for work. That's why i upgraded to 1.2G .)
    Import this menu bar from the common library, save it again as a png and then open it with fireworks the way you did, to see if the same error occurs.
    Has this happened with other files as well?

  • I see message "Cannot open the file. Not enough memory to open the file"

    When I'm trying to open file from adobe.com on my tablet, I see massage " Cannot open the file. Not enough memory to open the file" . I've got 10 gb free memory , so I dont know, why...

    Which system are you running (is this information correct in your profile)?
    Are there complicating issues to this question such as you transferring libraries between computers?
    Have you upgraded iTunes recently?
    Troubleshooting iTunes installation on Mac OS X - http://support.apple.com/kb/HT2311  - read section on Opening iTunes for information about running multiple copies of iTunes.

  • Access denied error while writing a file to the file system - myfileupload.saveas() throws system.unauthorizedexception

    hi,
    as part of my requirement , i have to perform read and  write  operations of  few files [ using the file upload control in my custom visual web part] and on submit button click.
    but while writing these files - with the help of  fileupload control - and when i use  myfileupload.saveas(mylocation);
    - i am saving these files into my D:\ drive of my server , where i am executing my code -, am getting access denied error.
    it throws system.unauthorizedexception.
    i have given full control on that folder where i was trying to store my attached files. and also  after following asp.net forums,
    i have added  iusr group added and performed all those steps such that, the file is saved in my D:\ drive.
    but unfortunately  that didnt happen.
    also
    a) i am trying the code with runwithelevatedprivileges(delegate() )  code
    b) shared the drive within the  d :drive where i want o save the files.
    c) given the full privieleges for the app pool identity- in my case , its
    network service.
    the  other strange thing is that, the same code works perfectly in  other machine, where the same sp, vs 2012  etc were installed .
    would like to know, any other changes/ steps i need to make it on this  server, where i am getting the  error.
    help is  appreciated!

    vishnuS1984 wrote:
    Hi Friends,
    I have gone through scores of examples and i am failing to understand the right thing to be done to copy a file from one directory to another. Here is my class...So let's see... C:\GetMe1 is a directory on your machine, right? And this is what you are doing with that directory:
    public static void copyFiles(File src, File dest) throws IOException
    // dest is a 'File' object but represents the C:\GetMe1 directory, right?
    fout = new FileOutputStream (dest);If it's a directory, where in your code are you appending the source file name to the path, before trying to open an output stream on it? You're not.
    BTW, this is awful:
    catch (IOException e)
    IOException wrapper = new IOException("copyFiles: Unable to copy file: " +
    src.getAbsolutePath() + "to" + dest.getAbsolutePath()+".");
    wrapper.initCause(e);
    wrapper.setStackTrace(e.getStackTrace());
    throw wrapper;
    }1) You're hiding the original IOException and replacing it with your own? For what good purpose?
    2) Even if you had a good reason to do that, this would be simpler and better:
    throw new IOException("your custom message goes here", e);
    rather than explicitly invokign initCause and setStackTrace. Yuck!

  • Not writing to file

    Hi,
    I have made a program that reads a string, converts it to bytes, then writes to a file.
    Well i got the first two working, but its not writing to file. I know it should work cos i have written to file on my previous tests, but when i used a FOR loop it stoped working. Do ya know why. Below is the code...
    for (int a = 0;a = bytes.length - 1;a++){
    of.write(bytes[a]);
    System.out.print(" " + bytes[a] + " : " + bytes.length);
    My friend told me to use bytes.length - 1 as it is meant to stop the OUTOFBOUNDS error.
    OK,
    my regards
    Below is the whole program, its a bit messy cos i got lots of old code that i have blocked.
    import java.io.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.lang.String;
    public class sender implements Runnable {
    int i= 0;
    String mes;
    int count =0;
    int b = 0;
    boolean yes = false;
    public static void main (String[] arguments) {
    Thread th = new Thread (new sender());
    th.start ();
    //sender();
    //while (true){}
    public void run() {
    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {}
    String mesdialog = JOptionPane.showInputDialog(null,
    "Enter your message");
    mes = mesdialog.toString();
    try {
    //FileOutputStream of = new
    //FileOutputStream ("send.txt");
    FileWriter of = new
    FileWriter("send.txt");
    //BufferedReader in = new
    // BufferedReader(fr);
    //while (true){}
    int i = 66;
    boolean eof = false;
    int inChar = 0;
    //String inChar;
    byte[] bytes = mes.getBytes();
    /* < disable / enable
    do {
    int n=Integer.MIN_VALUE;
    try{
    n = Integer.parseInt(mes);
    } catch (NumberFormatException nfe){
    //inChar = n;
    if (inChar != -1) {
    byte outChar = (byte)inChar* ;
    //char outChar = (char)inChar ;
    of.write(outChar);
    System.out.println("" + outChar);
    //of.write(mes,0,20);
    } else
    eof = true;
    } while (!eof);//*/
    for (int a = 0;a = bytes.length - 1;a++){
    of.write(bytes[a]);
    System.out.print(" " + bytes[a] + " : " + bytes.length);
    while (!yes) {
    if (bytes[b] == 32 )
    yes = true;
    else {
    of.write(bytes);
    System.out.print(" " + bytes[b]);
    b++;
    //System.exit(0);
    //outfile.write(input);
    } catch (IOException e) {
    System.out.print("AAA ERROR");}

    your friend was obviously wrong.
    for (int a = 0;a = bytes.length - 1;a++){ // that does nothing
    of.write(bytes[a]);
    System.out.print(" " + bytes[a] + " : " + bytes.length);
    }the above "for" loop says :
    1:initialize a with 0;
    2:do loop while a=bytes.length(<-that's completely wrong. it should be
    a boolean expression);
    3:for each loop increment "a" by one.
    try this:for (int a = 0;a < bytes.length;a++){
    of.write(bytes[a]);
    System.out.print(" " + bytes[a] + " : " + bytes.length);

  • Writing to file with ObjectOutputStream

    ApartmentWriterProgram asks information about apartments
    and saves it to a file.
    For some reason it doesnt save information, it writes:
    Writing to file "+file+ " failed.
    So there happens IOException in the method write(Apartment apartment).
    Or in the run().
    I dont know what's wrong. Do you?
    public class ApartmentFileHandler extends Object implements Serializable {
        private OutputStream outStream;
        private ObjectOutputStream obStream;
        private String filePath;
            public ApartmentFileHandler(String filePath) {
               this.filePath = filePath;
    /** Writes the given apartment data in the file. If the file already
    exists, the old contents are overwritten.
    Parameters:
    apartment - the apartment to be written in the file
    Throws:
    IOException - if other problems arose when handling the file (e.g. the
    file could not be written)
            public void write(Apartment apartment)
               throws IOException {
               outStream = new FileOutputStream(filePath);
               obStream = new ObjectOutputStream(outStream);
               obStream.writeObject(apartment);
    import java.io.*;
    import java.util.*;
    public class ApartmentWriterProgram
    extends Object
        private KeyboardReader keyboardReader;
        private List roomsInList;
        private Room[] rooms;
        private int numberOfRooms;
        private String file;
              public ApartmentWriterProgram()
                this.keyboardReader = new KeyboardReader();
                this.roomsInList = new ArrayList();
                this.rooms = new Room [numberOfRooms];
    /** Runs the writer program. The program asks the user for some
    apartment data and a file name, and saves the data in the file. */
              public void run() {
                try {
                  numberOfRooms = keyboardReader.getInt("Give number of rooms: ");
                  for (int i =1; i<numberOfRooms+1; i++) {
                     String type =keyboardReader.getString("Give " +i+ ". " + "type of the room: ");
                     double area =keyboardReader.getDouble("Give " +i+ ". " + "area of room: ");
                     Room room= new Room(type, area);
                     this.roomsInList.add(huone);
                  roomsInList.toArray(rooms);
                  Apartment apartment = new Apartment(rooms);
                  System.out.println();
                  file = keyboardReader.getString("Give name of the file: ");
                  ApartmentFileHandler handler = new ApartmentFileHandler(file);
                  handler.write(apartment);
               } catch (IOException ioe) {
                 System.out.println("Writing to file "+file+ " failed.");
             public static void main(String[]  args) {
                  ApartmentWriterProgram program = new ApartmentWriterProgram();
                  program.run();
    }

    There is nothing to gain in not putting the throws IOException in the write method, since the exception object will have the information about the actual problem.
    Is this a typo for the this post?                 this.roomsInList.add(huone); should be room nes pas.
    What exception is actually thrown? To find out useSystem.out.println("Exception: " + ioe);Then wonder why you have the AppartmentFileHandler implement Serializable when you are not writing that object to the file.

  • Error in Writing to File \Winnt\System32\mfcans32.dll

    Error window with msg "Error in Writing to File \Winnt\System32\mfcans32.dll" is being popuped while trying to install 9.0.3 on windows 2000 Advanced Server.
    My System Configuration Details are as follows.
    Compaq Proliant
    Ram : 2GB
    HARD DISK : 80GB
    PROCESSOR : 2.6
    Please help me to install.
    Thanks,
    Srinivas Chowdhary Annamareddy.

    You might want to investigate why it became Read-Only in the first place and are there any other files that should not be Read-Only read only.

  • Need help writing to file

    I have an ArrayList of XML strings. I am trying to write the XML strings in the arraylist to a text file. However, the only XML string that I see in the file is the first string repeated over and over. It is the only one getting to the file for some reason. I have tried the code below:
        public void writeToLogFile(String logFile, String msg) {
                try{   
                    FileWriter WriteFile = new FileWriter(logFile, true);  
                    BufferedWriter WriteBuff = new BufferedWriter(WriteFile); 
                    WriteBuff.write(msg+"\r\n");
                    WriteBuff.close(); 
                    WriteFile.close(); 
                }catch(IOException e) {
                    System.out.println("Error writing to file.");
    //        try {
    //            FileWriter fw = new FileWriter(logFile,true);
    //            PrintWriter pw = new PrintWriter(fw);
    //            pw.println(msg);
    //            pw.close();
    //        }catch(IOException ioExc) {
    //            System.out.println("error writing file");
        }  I tried the commented out code also. I've also tried using PrintStream. They all seem to give me the same result. My first thought was maybe I'm sending the same string to the method over and over but I'm not. If I loop through my arraylist of XML strings and do a System.out.println() instead of writing to file, I see that I am sending in different XML strings.
    Here's how I'm looping through the arraylist of XML strings call "al":
                for(int i=0; i<FileValues.length; i++) {
                    al = adapter.executeQuery(FileValues[0]);
    System.out.println("Writing to output file...");
    for(int x=0; x<=al.size()-1; x++) {
    //System.out.println(al.get(x).toString());
    test.writeToLogFile("c:/temp/a/limAdapterOutput", al.get(i).toString());
    System.out.println("Finished writing to output file.");
    What I'm doing is reading an arraylist (FileValues) of query parameters. For each parameter, I am querying a database based on the parameter and returning an arraylist of XML strings containing the query result. For each paramter, I'm returning about 1200 XML strings in the arraylist. Then I try to loop through the arraylist and write the XML strings to a file. However, this is when I run into my problem.
    Any ideas what's going on and how I can correct the problem? Thanks.

    for(int i=0; i<FileValues.length; i++) {
    al =
    al =
    al = adapter.executeQuery(FileValues[0]);
    System.out.println("Writing to output
    iting to output file...");
    for(int x=0; x<=al.size()-1; x++) {
    //System.out.println(al.get(x).toString());
    test.writeToLogFile("c:/temp/a/limAdapterOutput",
    ut", al.get(i).toString());
    System.out.println("Finished writing
    inished writing to output file.");
    When you write to the log file in the inner loop, it sounds like you want to use the x loop counter, not the i counter from the outer loop.  You comment out the print to System.out, which uses the x loop counter.  Why don't you do the same thing in the uncommented code?  Hard to follow exactly what you're doing, but maybe that will fix it.

  • Error in writing to file encountered while applying patch 9.2.0.8

    Hi,
    I encountered the error message below while applying Oracle 9.2.0.8 patch set using OUI on server running Oracle 9.2.0.7 and Windows 2003 Enterprise Edition SP2:
    Error in writing to file 'C:\oracle\ora92\BIN\orapls9.dll'
    [C:\oracle\ora92\BIN\orapls9.dll (he process cannot access
    the file because it is being used by another process)]
    I've already stopped the Oracle instance and services before invoking the setup.exe.
    My question is: Why this is happening and what is the resolution to prevent this error from happening?

    I used the script in the Metalink note 454040.1 and chose 5. I typed in the orapls9.dll to see which module is locking this dll and I found out it is avrrepsvc.exe which is one of the services our application is running. I just stopped it in Services Console and run the setup.exe of the patch. The installaton of patch run successfully. I checked the version using 'select banner from v$version' and I now got this results:
    SQL> select banner from v$version;
    BANNER
    Oracle9i Enterprise Edition Release 9.2.0.8.0 - Production
    PL/SQL Release 9.2.0.8.0 - Production
    CORE     9.2.0.8.0     Production
    TNS for 32-bit Windows: Version 9.2.0.8.0 - Production
    NLSRTL Version 9.2.0.8.0 - Production
    I have a related question. When checking from dba_registry, isn't it that versions of components should have been upgraded too? Please see below I what got:
    SQL>SELECT SUBSTR(comp_id,1,15) comp_id, status, SUBSTR(version,1,10)
    version, SUBSTR(comp_name,1,30) comp_name
    FROM dba_registry ORDER BY 1;
    COMP_ID STATUS VERSION COMP_NAME
    CATALOG VALID 9.2.0.7.0 Oracle9i Catalog Views
    CATJAVA VALID 9.2.0.7.0 Oracle9i Java Packages
    CATPROC VALID 9.2.0.7.0 Oracle9i Packages and Types
    CONTEXT VALID 9.2.0.7.0 Oracle Text
    JAVAVM VALID 9.2.0.7.0 JServer JAVA Virtual Machine
    ORDIM VALID 9.2.0.7.0 Oracle interMedia
    OWM VALID 9.2.0.1.0 Oracle Workspace Manager
    SDO VALID 9.2.0.7.0 Spatial
    XDB VALID 9.2.0.7.0 Oracle XML Database
    XML VALID 9.2.0.9.0 Oracle XDK for Java
    10 rows selected.
    What tasks did I missed int the steps?
    ADDITION:
    It looks like I forgot to run the catpatch.sql and utlrp.sql.
    Edited by: user5470917 on Jun 19, 2009 2:52 PM

  • Error in writing to file 'C:\WINDOWS\Systems32\msjet35.dll'

    Got the follow error message when installing oracle 10.2 on win 2003 server.
    Error in writing to file 'C:\WINDOWS\Systems32\msjet35.dll' Access is denied.
    Any ideas?

    You might want to investigate why it became Read-Only in the first place and are there any other files that should not be Read-Only read only.

Maybe you are looking for