Title: [System.Convert]::FromBase64String causes memory leak with large strings · Issue #21473 · PowerShell/PowerShell · GitHub
Open Graph Title: [System.Convert]::FromBase64String causes memory leak with large strings · Issue #21473 · PowerShell/PowerShell
X Title: [System.Convert]::FromBase64String causes memory leak with large strings · Issue #21473 · PowerShell/PowerShell
Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerShell 5.1 and PowerShell. Steps to reprodu...
Open Graph Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
X Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/21473
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[System.Convert]::FromBase64String causes memory leak with large strings","articleBody":"### Prerequisites\n\n- [X] Write a descriptive title.\n- [X] Make sure you are able to repro it on the [latest released version](https://github.com/PowerShell/PowerShell/releases)\n- [X] Search the existing issues.\n- [X] Refer to the [FAQ](https://github.com/PowerShell/PowerShell/blob/master/docs/FAQ.md).\n- [X] Refer to [Differences between Windows PowerShell 5.1 and PowerShell](https://learn.microsoft.com/powershell/scripting/whats-new/differences-from-windows-powershell).\n\n### Steps to reproduce\n\nThis was tested on PowerShell 7.4.2\r\n1. Download the .NET 8.0 installer for Windows x64 to use as the test file, the direct link to this is here: https://dotnet.microsoft.com/en-us/download/dotnet/thank-you/sdk-8.0.204-windows-x64-installer\r\n2. Save this file into a folder on your computer somewhere\r\n3. Create a PowerShell script with the following contents in the same folder as the .NET 8.0 installer:\r\n\r\n```powershell\r\nGet-Date\r\nWrite-Host \"Creating Path to dotnet.exe test file\"\r\n$file = Join-Path -Path $PSScriptRoot -ChildPath \"dotnet.exe\"\r\nWrite-Host \"Reading all file bytes into memory\"\r\n$bytes = [System.IO.File]::ReadAllBytes( $file )\r\nWrite-Host \"Converting file bytes to base64 string\"\r\n$base64 = [System.Convert]::ToBase64String( $bytes )\r\nWrite-Host \"Converting base64 string back to file bytes\"\r\n$bytes = [System.Convert]::FromBase64String( $base64 )\r\nWrite-Host \"Test complete\"\r\nGet-Date\r\n```\r\n\r\nNOTE: That if you test this with a newer version of the .NET 8.0 installer, you may have to modify the test script to pick the correct file for the test since the filename is hard coded on line 3.\r\n\r\n4. Open a PowerShell window in the folder with the script and test file\r\n5. Run the PowerShell script\r\n6. Open Task Manager and observe the memory usage of PowerShell\r\n7. Note the time required to complete the conversion from Base64 and the usage of upwards of 3.5 GB of RAM to do so\r\n\r\nThe .NET 8.0 installer for Windows x64 is approximately 222 MB in size. Reading into memory and converting to base64 then converting back should require about 790 MB of RAM with all variables remaining in scope during the process and no garbage collection happening or object disposal happening. The observed behavior appears to be memory-leak related as the amount of memory used once the conversion eventually completes is about 3.4 GB of RAM. These data points can be see in the attached screen shots.\n\n### Expected behavior\n\n```console\nWhen you run the same script in PowerShell 7 and Windows PowerShell 5.1, you see two very different behaviors:\r\n\r\nIn PowerShell 7.4.2, the time to complete is 82 seconds and memory used is 3.4 GB\r\nIn PowerShell 5.1, the time to complete is 7 seconds and memory used is 1.0 GB\r\n\r\nThis suggests there is an error in the PowerShell 7.4.2 / .NET 8.0 implementation.\n```\n\n\n### Actual behavior\n\n```console\nIn PowerShell 7.4.2, the time to complete is 82 seconds and memory used is 3.4 GB.\n```\n\n\n### Error details\n\n_No response_\n\n### Environment data\n\n```powershell\nName Value\r\n---- -----\r\nPSVersion 7.4.2\r\nPSEdition Core\r\nGitCommitId 7.4.2\r\nOS Microsoft Windows 10.0.22631\r\nPlatform Win32NT\r\nPSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}\r\nPSRemotingProtocolVersion 2.3\r\nSerializationVersion 1.1.0.1\r\nWSManStackVersion 3.0\n```\n\n\n### Visuals\n\n## Testing in PowerShell 7.4.2\r\n\r\n\u003cimg width=\"425\" alt=\"ps7-test\" src=\"https://github.com/PowerShell/PowerShell/assets/29439901/a8e1be27-4536-4d1b-a8c9-d95a4135d771\"\u003e\r\n\r\n## Testing in PowerShell 5.1\r\n\r\n\u003cimg width=\"396\" alt=\"ps5-test\" src=\"https://github.com/PowerShell/PowerShell/assets/29439901/0cf00507-a436-4de2-9e2b-54579a4f759a\"\u003e\r\n\r\n## PowerShell 7.4.2 Memory Usage\r\n\r\n\u003cimg width=\"518\" alt=\"ps7-mem\" src=\"https://github.com/PowerShell/PowerShell/assets/29439901/f3341828-a3c3-464b-b37e-b047c9dc3cb5\"\u003e\r\n\r\n## PowerShell 5.1 Memory Usage\r\n\r\n\u003cimg width=\"524\" alt=\"ps5-mem\" src=\"https://github.com/PowerShell/PowerShell/assets/29439901/4acacc9d-7b64-4a54-92da-5b88369f505c\"\u003e\r\n","author":{"url":"https://github.com/chopinrlz","@type":"Person","name":"chopinrlz"},"datePublished":"2024-04-15T15:26:00.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":74},"url":"https://github.com/21473/PowerShell/issues/21473"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0bb74310-5a33-a995-d3a5-c3138f765cfa |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D3DC:256E35:1047F6F:173A3C1:6A551E55 |
| html-safe-nonce | a29df557fc40db83d6b8fe28e9e8b988ebca22682131574e9da91d4f37110869 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEM0RDOjI1NkUzNToxMDQ3RjZGOjE3M0EzQzE6NkE1NTFFNTUiLCJ2aXNpdG9yX2lkIjoiMjAyNDAzNjYyMTcyNTUzOTkyNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 036983938c6fe3ec3604fda37a7d62e714d6390b45aba2c8e9113a018fa476f3 |
| hovercard-subject-tag | issue:2243969672 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/21473/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f6d316b7190ee3fc3f3e6435829663f85eccc99795acc8730ca75f0ebec5c4ce/PowerShell/PowerShell/issues/21473 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f6d316b7190ee3fc3f3e6435829663f85eccc99795acc8730ca75f0ebec5c4ce/PowerShell/PowerShell/issues/21473 |
| og:image:alt | Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | chopinrlz |
| hostname | github.com |
| expected-hostname | github.com |
| None | 682d273eacb2ac51680c6eb9c0b270f029f7ce74c32090f319083c34497e28a5 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 3b6d37c6470adadff4194742daaab9a817cc4980 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width