locked
Is this a bug or WTF ?!¿ RRS feed

  • Question

  • I wanted to split my camera photos onto events by photo number, so i started with powershell.

    $range = @(2553,200000000)
    ls -Path "U:\DCIM\100NCD40" |? { [int]::Parse($_.Name.Split("_")[1].Split(".")[0]) -in $range[0]..$range[1] }

    This 2 lines generates 6Gb of RAM usage, but if i use a smaller number it simply works...

    Using Windows 10 updated.



    nova alerta, a veure si te'n surts

    Monday, June 4, 2018 11:15 AM

All replies

  • How about something like this way?

     | where { $int -ge $range[0] -and $int -le $range[1] }
    • Edited by JS2010 Monday, June 4, 2018 3:25 PM
    • Proposed as answer by j0rt3g4 Monday, June 4, 2018 7:14 PM
    Monday, June 4, 2018 3:22 PM
  • The range operator ('..") creates a list. In your case that list contains close to two hundred thousand integers.

    The simple change suggested by JS2010 would fix your problem.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Monday, June 4, 2018 4:02 PM
  • That's obviously a WORKARROUND, but the post was about the bug within the code.

    We all know these lines shoud not use 6gb of ram.

    And that's the matter.


    nova alerta, a veure si te'n surts

    Tuesday, June 5, 2018 10:10 AM
  • So, it's a poorly implemented method, with non documented limitations.

    Please fix it.

    Meanwhile i will use JS2010 workarround.

    Thanks.


    nova alerta, a veure si te'n surts

    Tuesday, June 5, 2018 10:11 AM
  • Your code does not use 6Gb of memory.  The memory listed is just a full virtual allocation.  It is not really in use.  All PS processes on a machine with a large amount of free memory will show that much allocation.  If other processes require it the memory reservation will be released as the system sees fit.


    \_(ツ)_/

    Tuesday, June 5, 2018 2:27 PM
  • Anyways my hdd is swapping and i had to close everything.

    U sure?


    nova alerta, a veure si te'n surts

    Tuesday, June 5, 2018 2:39 PM
  • You're not just creating a list of 200,000,000 integers, you're creating that list for every file you have.

    Tuesday, June 5, 2018 2:46 PM
  • Creating 200,000k integers is the worst way to perform such action 10..200.000.000 the function needs improvement.

    There where less than 1k files, so the files are not the problem.

    Improve the code.

    Thanks

    Try yourself, it REALLY uses 6gb Ram

    Improve the code.


    nova alerta, a veure si te'n surts

    Tuesday, June 5, 2018 2:51 PM
  • It is silly to create a list to check a range.  Also, when in a tight loop, the memory will not be freed immediately.  This is because PS does not know that you do not need the variables and the loop delays the garbage collector.

    There is no bug only bad code.  Bad coding cannot be compensated for and, most often, will undermine the systems and PowerShell's ability to optimize the task.

    If you are on a 32 bit system then this code will be a system killer.


    \_(ツ)_/


    • Edited by jrv Tuesday, June 5, 2018 2:56 PM
    Tuesday, June 5, 2018 2:55 PM
  • Creating 200,000k integers is the worst way to perform such action 10..200.000.000 the function needs improvement.

    There where less than 1k files, so the files are not the problem.

    Improve the code.

    Thanks

    Try yourself, it REALLY uses 6gb Ram

    Improve the code.


    nova alerta, a veure si te'n surts

    I am running this now on 1000 files and it causes no issues with my system and all PS processes always show 6Gb of memory reserve after running any commands.  There is no "swapping" and all processes are running normally.

    You need to understand how Windows uses and allocates memory to understand what is happening.  WHat is happening is what is expected when you run code like this.

    If I watch memory the actual system usage goes up for a while then the system claims back about 20% of the memory allocated.  PS always shows the full allocation that it has received even though the stale memory has been flushed to disk.  "Swapping" will only occur when the system needs to reload flushed memory to run a process.


    \_(ツ)_/

    Tuesday, June 5, 2018 3:06 PM