Groups > Microsoft > Windows PowerShell > System.OutOfMemoryException




RE: System.OutOfMemoryException

RE: System.OutOfMemoryException
Wed, 16 Apr 2008 11:03:01 -070
Have you tried using the -totalcount parameter on get-content ?
-- 
Richard Siddaway
All scripts are supplied "as is" and with no warranty 
PowerShell MVP
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Robertico" wrote:

> After excellent help from Kiron (see my previous posting: Read and search 
> through a binary file) i used the script to read and search throug a binary

> file.
> Using this script with a binary file of approximately 650 Mb, I received a

> 'System.OutOfMemoryException' (using approximately 2 Gb of memory).
> I know that Powershell v 1.0 isn't very friendly using memory.
> 
> So i think that an option can be to read chunks of bytes. Is it possible to

> modify the script to read chunks of bytes, or is there an alternative.
> 
> Thanks in advance,
> 
> Robertico 
> 
> 
Post Reply
Re: System.OutOfMemoryException
Wed, 16 Apr 2008 17:34:53 -060
Hi Robertico,
 By default Get-Content reads 1 line --or in this case, 1 byte-- at a time, you
can modify the Cmdlet's behavior through its -ReadCount parameter, but when more
than 1 item --line or byte-- is read, Get-Content outputs an array of items
--lines or bytes. In your case you would then have to unravel this array of
bytes, format each byte as a hexadecimal number and join the hexadecimal bytes
into a single string which is piped and processed further. In this example I'm
passing a -ReadCount of 500KB but you should modify this to fit your memory and
performance requirements, also be aware that when breaking the content into
chunks you may affect the marker you're searching for.

# for more on Get-Content's -ReadCount parameter
man gc -p r*

Try this:
# v1
$file = <file's path>
$pattern = '131B1B087C156108AE151B'
$prevBytes = 8
$chunkSize = 500kb
gc $file -en byte -r $chunkSize |
 % {
  $bytes = [string]::join('', ($_ | % {'{0:X2}' -f $_}))
  [regex]::matches($bytes, $pattern, 'ignoreCase') |
   % {
    $i = $_.index - $prevBytes * 2
    [string]::join('', $bytes[$i..($i + $prevBytes * 2 - 1)]) |
     % {
      $hexBytes = $_
      $byteArray = 0..($_.length - 1) | ? {!($_ -band 1)} |
       % {"0x$($hexBytes.subString($_,2))"}
      [array]::reverse($byteArray)
      [bitConverter]::toString($byteArray) -replace '-'
     }
   }
 }

-- 
Kiron
Post Reply
System.OutOfMemoryException
Wed, 16 Apr 2008 19:14:48 +020
After excellent help from Kiron (see my previous posting: Read and search 
through a binary file) i used the script to read and search throug a binary 
file.
Using this script with a binary file of approximately 650 Mb, I received a 
'System.OutOfMemoryException' (using approximately 2 Gb of memory).
I know that Powershell v 1.0 isn't very friendly using memory.

So i think that an option can be to read chunks of bytes. Is it possible to 
modify the script to read chunks of bytes, or is there an alternative.

Thanks in advance,

Robertico 

Post Reply
about | contact