Memory Leak with BinaryReader/Byte Array
Author |
Message |
Brian Hear #1 / 7
|
 Memory Leak with BinaryReader/Byte Array
Can someone tell me how to release the memory used in the code below please? I'm using task manager's mem usage column to identify that there is a problem Dim lobjStream As New IO.FileStream("C:\myfile", IO.FileMode.Open) Dim lobjBinaryReader As New IO.BinaryReader(lobjStream) Dim lbytX() As Byte 'Put a breakpoint here, step through and watch the memory go up in Task Manager lbytX = lobjBinaryReader.ReadBytes(lobjStream.Length) 'Non of these appear to release the memory Array.Clear(lbytX, 0, UBound(lbytX) + 1) ReDim lbytX(0) Erase lbytX lbytX = Nothing lobjStream.Close() lobjStream = Nothing lobjBinaryReader.Close() lobjBinaryReader = Nothing I have written something similar that opens up a file when you click on it in a listview, reads the whole file, finds it needs to and closes it. Each time you do this the memory goes up and up! The user could be browsing through a number of files so the memory usage escalates very quickly even though they are only a couple of meg each. Thanks, Brian.
|
Fri, 23 Sep 2005 01:14:36 GMT |
|
 |
Armin Zingle #2 / 7
|
 Memory Leak with BinaryReader/Byte Array
Quote: > Can someone tell me how to release the memory used in the code > below please? I'm using task manager's mem usage column to > identify that there is a problem
When reading a 31 MB file, memory usage is increased by 31MB, so... Apart from that: ms-help://MS.VSCC/MS.MSDNVS/cpguide/html/cpconautomaticmemorymanagement.htm (watch for wrapping) -- Armin
|
Fri, 23 Sep 2005 03:25:58 GMT |
|
 |
Jerem #3 / 7
|
 Memory Leak with BinaryReader/Byte Array
Memory stays in uses during your method call. It should go down after the method exits unless your using a global, static, or public variable. Array.Clear( ) has nothing to do with memory; rather it resets values of the array to their defaults (wich may, in some cases, free up memory, but this would be a side-effect). Erase should que the array to the garbage collector, if you call GC.Collect( ), this should clear the memory. But, again, the memory should be getting cleared every time the function exits, you say it just keeps growing right? So what happens after repeating the process 100 times, do you have like 2 gb of memory used? Jeremy Quote:
> Can someone tell me how to release the memory used in the code below please? > I'm using task manager's mem usage column to identify that there is a > problem > Dim lobjStream As New IO.FileStream("C:\myfile", IO.FileMode.Open) > Dim lobjBinaryReader As New IO.BinaryReader(lobjStream) > Dim lbytX() As Byte > 'Put a breakpoint here, step through and watch the memory go up in > Task Manager > lbytX = lobjBinaryReader.ReadBytes(lobjStream.Length) > 'Non of these appear to release the memory > Array.Clear(lbytX, 0, UBound(lbytX) + 1) > ReDim lbytX(0) > Erase lbytX > lbytX = Nothing > lobjStream.Close() > lobjStream = Nothing > lobjBinaryReader.Close() > lobjBinaryReader = Nothing > I have written something similar that opens up a file when you click on it > in a listview, reads the whole file, finds it needs to and closes it. Each > time you do this the memory goes up and up! The user could be browsing > through a number of files so the memory usage escalates very quickly even > though they are only a couple of meg each. > Thanks, > Brian.
|
Fri, 23 Sep 2005 06:51:28 GMT |
|
 |
Jerem #4 / 7
|
 Memory Leak with BinaryReader/Byte Array
If you could post a report of mem usage before and after the function call, for 10 calls that would help.
Memory stays in uses during your method call. It should go down after the method exits unless your using a global, static, or public variable. Array.Clear( ) has nothing to do with memory; rather it resets values of the array to their defaults (wich may, in some cases, free up memory, but this would be a side-effect). Erase should que the array to the garbage collector, if you call GC.Collect( ), this should clear the memory. But, again, the memory should be getting cleared every time the function exits, you say it just keeps growing right? So what happens after repeating the process 100 times, do you have like 2 gb of memory used? Jeremy Quote:
> Can someone tell me how to release the memory used in the code below please? > I'm using task manager's mem usage column to identify that there is a > problem > Dim lobjStream As New IO.FileStream("C:\myfile", IO.FileMode.Open) > Dim lobjBinaryReader As New IO.BinaryReader(lobjStream) > Dim lbytX() As Byte > 'Put a breakpoint here, step through and watch the memory go up in > Task Manager > lbytX = lobjBinaryReader.ReadBytes(lobjStream.Length) > 'Non of these appear to release the memory > Array.Clear(lbytX, 0, UBound(lbytX) + 1) > ReDim lbytX(0) > Erase lbytX > lbytX = Nothing > lobjStream.Close() > lobjStream = Nothing > lobjBinaryReader.Close() > lobjBinaryReader = Nothing > I have written something similar that opens up a file when you click on it > in a listview, reads the whole file, finds it needs to and closes it. Each > time you do this the memory goes up and up! The user could be browsing > through a number of files so the memory usage escalates very quickly even > though they are only a couple of meg each. > Thanks, > Brian.
|
Fri, 23 Sep 2005 06:52:10 GMT |
|
 |
Brian Hear #5 / 7
|
 Memory Leak with BinaryReader/Byte Array
I'm not convinced it's going up every time now. I spent some time looking at the code I based my example on. It looks like the memory goes up by the difference between the last ubound and the new ubound if the new ubound is bigger. To test this I've added a button and text box to a form and put this code in it : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim lbytX(Convert.ToInt32(TextBox1.Text)) As Byte End Sub When I start the app, it shows approx 13.5MB mem usage. Entering 3,000,000 in to the text box and click the button it goes up to approx 16.5MB. Repeatedly pressing the button makes no difference. Changing the text box to 6,000,000 and pressing the button makes it go up to approx 19.5MB. Adding Erase and GC.Collect make no difference. It won't release this memory until the app is closed. I hope that helps illustrate the problem a little better. Brian.
If you could post a report of mem usage before and after the function call, for 10 calls that would help.
Memory stays in uses during your method call. It should go down after the method exits unless your using a global, static, or public variable. Array.Clear( ) has nothing to do with memory; rather it resets values of the array to their defaults (wich may, in some cases, free up memory, but this would be a side-effect). Erase should que the array to the garbage collector, if you call GC.Collect( ), this should clear the memory. But, again, the memory should be getting cleared every time the function exits, you say it just keeps growing right? So what happens after repeating the process 100 times, do you have like 2 gb of memory used? Jeremy
Quote: > Can someone tell me how to release the memory used in the code below please? > I'm using task manager's mem usage column to identify that there is a > problem > Dim lobjStream As New IO.FileStream("C:\myfile", IO.FileMode.Open) > Dim lobjBinaryReader As New IO.BinaryReader(lobjStream) > Dim lbytX() As Byte > 'Put a breakpoint here, step through and watch the memory go up in > Task Manager > lbytX = lobjBinaryReader.ReadBytes(lobjStream.Length) > 'Non of these appear to release the memory > Array.Clear(lbytX, 0, UBound(lbytX) + 1) > ReDim lbytX(0) > Erase lbytX > lbytX = Nothing > lobjStream.Close() > lobjStream = Nothing > lobjBinaryReader.Close() > lobjBinaryReader = Nothing > I have written something similar that opens up a file when you click on it > in a listview, reads the whole file, finds it needs to and closes it. Each > time you do this the memory goes up and up! The user could be browsing > through a number of files so the memory usage escalates very quickly even > though they are only a couple of meg each. > Thanks, > Brian.
|
Sat, 24 Sep 2005 04:56:23 GMT |
|
 |
Jerem #6 / 7
|
 Memory Leak with BinaryReader/Byte Array
I loaded 300,000,000 bytes into 6 arrays. after 1st: 41mb after 2nd: 72mb after 3rd: 101mb after 4th: 131 after 5th: 123 after 6th: 124 waited 30 seconds: 154mb after erase (1): 154, 500 K after erase (2): 154,536 K after erase (3): 154,540 K after erase (4): 154,544 K after erase (5): 154,544 K after erase (6): 154,548 K After GC Collect: 154,880K I ran the same code another time, and put a break point in after all memory was allocated. The total memory used was 16mb (no, that isn't a typo). It went as low as 11 mb, although the app started running incredibly slow. After GC.Collect( ), memory went up to 16 mb. You do the math... The only thing I can say is that the garbage collector must have some logic that holds onto memory in an attempt to guess how much memory the app is going to use in the future. But that is just a guess. Quote:
> I'm not convinced it's going up every time now. I spent some time looking > at the code I based my example on. It looks like the memory goes up by the > difference between the last ubound and the new ubound if the new ubound is > bigger. > To test this I've added a button and text box to a form and put this code in > it : > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles Button1.Click > Dim lbytX(Convert.ToInt32(TextBox1.Text)) As Byte > End Sub > When I start the app, it shows approx 13.5MB mem usage. > Entering 3,000,000 in to the text box and click the button it goes up to > approx 16.5MB. Repeatedly pressing the button makes no difference. > Changing the text box to 6,000,000 and pressing the button makes it go up to > approx 19.5MB. > Adding Erase and GC.Collect make no difference. It won't release this > memory until the app is closed. > I hope that helps illustrate the problem a little better. > Brian.
> If you could post a report of mem usage before and after the function call, > for 10 calls that would help.
> Memory stays in uses during your method call. It should go down after the > method exits unless your using a global, static, or public variable. > Array.Clear( ) has nothing to do with memory; rather it resets values of the > array to their defaults (wich may, in some cases, free up memory, but this > would be a side-effect). > Erase should que the array to the garbage collector, if you call > GC.Collect( ), this should clear the memory. > But, again, the memory should be getting cleared every time the function > exits, you say it just keeps growing right? So what happens after repeating > the process 100 times, do you have like 2 gb of memory used? > Jeremy
> > Can someone tell me how to release the memory used in the code below > please? > > I'm using task manager's mem usage column to identify that there is a > > problem > > Dim lobjStream As New IO.FileStream("C:\myfile", IO.FileMode.Open) > > Dim lobjBinaryReader As New IO.BinaryReader(lobjStream) > > Dim lbytX() As Byte > > 'Put a breakpoint here, step through and watch the memory go up in > > Task Manager > > lbytX = lobjBinaryReader.ReadBytes(lobjStream.Length) > > 'Non of these appear to release the memory > > Array.Clear(lbytX, 0, UBound(lbytX) + 1) > > ReDim lbytX(0) > > Erase lbytX > > lbytX = Nothing > > lobjStream.Close() > > lobjStream = Nothing > > lobjBinaryReader.Close() > > lobjBinaryReader = Nothing > > I have written something similar that opens up a file when you click on it > > in a listview, reads the whole file, finds it needs to and closes it. > Each > > time you do this the memory goes up and up! The user could be browsing > > through a number of files so the memory usage escalates very quickly even > > though they are only a couple of meg each. > > Thanks, > > Brian.
|
Sat, 24 Sep 2005 07:49:33 GMT |
|
 |
Tom Shelto #7 / 7
|
 Memory Leak with BinaryReader/Byte Array
I loaded 300,000,000 bytes into 6 arrays. after 1st: 41mb after 2nd: 72mb after 3rd: 101mb after 4th: 131 after 5th: 123 after 6th: 124 waited 30 seconds: 154mb after erase (1): 154, 500 K after erase (2): 154,536 K after erase (3): 154,540 K after erase (4): 154,544 K after erase (5): 154,544 K after erase (6): 154,548 K After GC Collect: 154,880K I ran the same code another time, and put a break point in after all memory was allocated. The total memory used was 16mb (no, that isn't a typo). It went as low as 11 mb, although the app started running incredibly slow. After GC.Collect( ), memory went up to 16 mb. You do the math... The only thing I can say is that the garbage collector must have some logic that holds onto memory in an attempt to guess how much memory the app is going to use in the future. But that is just a guess. ***************************** I think that is a characteristic of Windows memory managment - at least on NT systems. IIRC, when you free memory, it is not necessarily returned to the system immediately. Windows lets your process keep it in case it needs it again, but will reclaim it if the system becomes low on memory... Tom Shelton
Quote: > I'm not convinced it's going up every time now. I spent some time looking > at the code I based my example on. It looks like the memory goes up by the > difference between the last ubound and the new ubound if the new ubound is > bigger. > To test this I've added a button and text box to a form and put this code in > it : > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles Button1.Click > Dim lbytX(Convert.ToInt32(TextBox1.Text)) As Byte > End Sub > When I start the app, it shows approx 13.5MB mem usage. > Entering 3,000,000 in to the text box and click the button it goes up to > approx 16.5MB. Repeatedly pressing the button makes no difference. > Changing the text box to 6,000,000 and pressing the button makes it go up to > approx 19.5MB. > Adding Erase and GC.Collect make no difference. It won't release this > memory until the app is closed. > I hope that helps illustrate the problem a little better. > Brian.
> If you could post a report of mem usage before and after the function call, > for 10 calls that would help.
> Memory stays in uses during your method call. It should go down after the > method exits unless your using a global, static, or public variable. > Array.Clear( ) has nothing to do with memory; rather it resets values of the > array to their defaults (wich may, in some cases, free up memory, but this > would be a side-effect). > Erase should que the array to the garbage collector, if you call > GC.Collect( ), this should clear the memory. > But, again, the memory should be getting cleared every time the function > exits, you say it just keeps growing right? So what happens after repeating > the process 100 times, do you have like 2 gb of memory used? > Jeremy
> > Can someone tell me how to release the memory used in the code below > please? > > I'm using task manager's mem usage column to identify that there is a > > problem > > Dim lobjStream As New IO.FileStream("C:\myfile", IO.FileMode.Open) > > Dim lobjBinaryReader As New IO.BinaryReader(lobjStream) > > Dim lbytX() As Byte > > 'Put a breakpoint here, step through and watch the memory go up in > > Task Manager > > lbytX = lobjBinaryReader.ReadBytes(lobjStream.Length) > > 'Non of these appear to release the memory > > Array.Clear(lbytX, 0, UBound(lbytX) + 1) > > ReDim lbytX(0) > > Erase lbytX > > lbytX = Nothing > > lobjStream.Close() > > lobjStream = Nothing > > lobjBinaryReader.Close() > > lobjBinaryReader = Nothing > > I have written something similar that opens up a file when you click on it > > in a listview, reads the whole file, finds it needs to and closes it. > Each > > time you do this the memory goes up and up! The user could be browsing > > through a number of files so the memory usage escalates very quickly even > > though they are only a couple of meg each. > > Thanks, > > Brian.
|
Sat, 24 Sep 2005 10:12:13 GMT |
|
|
|