简体   繁体   中英

OutOfMemoryException while reading a file

In my application I am reading a file using a windows service. This service reads the file every second. This is the code I am using to read the file:

public static byte[] GetBytesFromFile(string fullFilePath)
{
    FileStream fs = File.OpenRead(fullFilePath);
    try
    {
        byte[] bytes = new byte[fs.Length];
        fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
        fs.Close();
        return bytes;
    }
    finally
    {
        fs.Close();
    }
}

After some hours I am getting a System.OutOfMemoryException . Am I doing anything wrong here?

Update: I am using the returned bytes in this code:

object s = null;
System.Reflection.Assembly a = Assembly.Load(bytes);
object o = a.CreateInstance("ID_" + report.ID.ToString().Replace("-", "_"));

Type t = o.GetType();
MethodInfo mi = t.GetMethod(name);
object[] values = new object[1];
values[0] = nameValue;
s = mi.Invoke(o, values);
return s;

With the edit, the problem is obvious:

System.Reflection.Assembly a = Assembly.Load(bytes);

Here's the thing: assemblies do not unload from an AppDomain . There are some garbage-collectible things like DynamicMethod - but not a full assembly. If you are loading assemblies, the only safe way to do that is into a separate AppDomain in the process. Then you can jettison the AppDomain when you are done with it (which unloads everything that is in it).


Actually, there is no guarantee that your existing code even works - Read does not guarantee to read all the data. I have a pending blog post about that...

But: if the files are big, it is likely you are slowly saturating the LOH, which is a non-compacting heap. The best advice I can offer is: don't read huge files into arrays. Try to re-write the downstream code to work with either a Stream API, some kind of "reader" API, or some kind of parsing iterator block.

Without seeing how the rest of the code processes things, it is hard to say more. However, it would be a good idea to run a memory profiler on the code to confirm that it is indeed the byte[] that are causing the memory issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM