Skip to content

ConvertFrom-JSON high memory consumption #7698

Closed

Description

@strawgate

Steps to reproduce

$JSON = @"
{
  "configGlossary:installationAt": "Philadelphia, PA",
  "configGlossary:adminEmail": "ksm@pobox.com",
  "configGlossary:poweredBy": "Cofax",
  "configGlossary:poweredByIcon": "\/images\/cofax.gif",
  "configGlossary:staticPath": "\/content\/static",
  "templateProcessorClass": "org.cofax.WysiwygTemplate",
  "templateLoaderClass": "org.cofax.FilesTemplateLoader",
  "templatePath": "templates",
  "templateOverridePath": "",
  "defaultListTemplate": "listTemplate.htm",
  "defaultFileTemplate": "articleTemplate.htm",
  "useJSP": false,
  "jspListTemplate": "listTemplate.jsp",
  "jspFileTemplate": "articleTemplate.jsp",
  "cachePackageTagsTrack": 200,
  "cachePackageTagsStore": 200,
  "cachePackageTagsRefresh": 60,
  "cacheTemplatesTrack": 100,
  "cacheTemplatesStore": 50,
  "cacheTemplatesRefresh": 15,
  "cachePagesTrack": 200,
  "cachePagesStore": 100,
  "cachePagesRefresh": 10,
  "cachePagesDirtyRead": 10,
  "searchEngineListTemplate": "forSearchEnginesList.htm",
  "searchEngineFileTemplate": "forSearchEngines.htm",
  "searchEngineRobotsDb": "WEB-INF\/robots.db",
  "useDataStore": true,
  "dataStoreClass": "org.cofax.SqlDataStore",
  "redirectionClass": "org.cofax.SqlRedirection",
  "dataStoreName": "cofax",
  "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
  "dataStoreUrl": "jdbc:microsoft:sqlserver:\/\/LOCALHOST:1433;DatabaseName=goon",
  "dataStoreUser": "sa",
  "dataStorePassword": "dataStoreTestQuery",
  "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
  "dataStoreLogFile": "\/usr\/local\/tomcat\/logs\/datastore.log",
  "dataStoreInitConns": 10,
  "dataStoreMaxConns": 100,
  "dataStoreConnUsageLimit": 100,
  "dataStoreLogLevel": "debug",
  "maxUrlLength": 500
}
"@

$Count = 39999
$jsonarr = "[" + (($json + ",") * $Count) + $json + "]"
[system.gc]::Collect()
write-host ""
write-host ""
write-host "JSON -- Objects: $($Count + 1)"
write-host "JSON -- Length:  $($jsonarr.length / 1024 / 1024 ) Megabytes"

$BeforeMemory = $((get-process -pid $PID).WorkingSet)
[void] (convertfrom-json $jsonarr)
$AfterMemory = $((get-process -pid $PID).WorkingSet)

$MemoryDiff = $AfterMemory - $BeforeMemory

write-host "Memory Usage -- Before:     $($BeforeMemory  / 1024 / 1024) Megabytes"
write-host "Memory Usage -- After:      $($AfterMemory  / 1024 / 1024) Megabytes"
write-host "Memory Usage -- Difference: $($MemoryDiff  / 1024 / 1024) Megabytes"
write-host "Memory Usage -- Multiple:   $($MemoryDiff / $jsonarr.length)"

output:

JSON -- Objects: 40000
JSON -- Length:  67.4819955825806 Megabytes
Memory Usage -- Before:     210.8359375 Megabytes
Memory Usage -- After:      1240.90625 Megabytes
Memory Usage -- Difference: 1030.0703125 Megabytes
Memory Usage -- Multiple:   15.2643724241892

Actual behavior

Powershell Core ends up with 1 GB of memory used and peaks to 1.6 GB of memory utilization during convertfrom-json

Expected behavior

Powershell Core shouldn't need 1.6 gigabytes of memory to deserialize a 65MB JSON file.

Environment data

Name                           Value
----                           -----
PSVersion                      6.1.0-preview.3
PSEdition                      Core
GitCommitId                    v6.1.0-preview.3
OS                             Microsoft Windows 10.0.17134
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. powercode commented on Sep 3, 2018

    @powercode
    Collaborator

    We could consider interning property names.

  2. powercode commented on Sep 3, 2018

    @powercode
    Collaborator

    Or, probably better, some name dictionary, were we resue the property names. That should help a lot with json memory consumption.

  3. added
    Issue-Enhancement
    WG-Engine-Performance
    on Sep 4, 2018
  4. markekraus commented on Oct 2, 2018

    @markekraus
    Contributor

    This is actually a duplicate of #6199 as both commands use the same underlying code path.

  5. removed
    Resolution-Duplicate
    on Nov 19, 2019
  6. iSazonov commented on Nov 19, 2019

    @iSazonov
    Collaborator

    The issue is still present.

    /cc david-tap

  7. iSazonov commented on Nov 19, 2019

    @iSazonov
    Collaborator

    After the code review I think we need to migrate to System.Text.Json to get performance benefits.

  8. jszabo98 commented on Dec 21, 2019

    @jszabo98

    Note that convertfrom-json has no streaming mode like jq. So it will load the whole file into memory, no matter how big it is.

  9. hanneshayashi commented on Dec 8, 2020

    @hanneshayashi

    Note that convertfrom-json has no streaming mode like jq. So it will load the whole file into memory, no matter how big it is.

    Would be great if it had a streaming mode, like jq, though...

  10. 7 remaining items

  11. iSazonov commented on Jun 27, 2021

    @iSazonov
    Collaborator

    See #11198 for information.

  12. strawgate commented on Jun 27, 2021

    @strawgate
    ContributorAuthor

    I know this is the wrong place to talk about the module but i'll just drop one more note:

    I ran the Json tests against ConvertFrom-JsonFast and found a couple of issues that I resolved (I implemented noenumerate, depth, and pipeline support). At this time i have 216/224 tests passing with the only tests failing being tests that check for the exact exception type being thrown. The good news is the performance characteristics are the same with these additional items as it was without them.

    I will look into adding pscustomobject support as well. I also need to run the tests for Invoke-RestMethod as well.

    While the ConvertFrom-Json test coverage isn't great, it's nice to see that it could be fairly compatible!

  13. iSazonov commented on Jun 28, 2021

    @iSazonov
    Collaborator

    Bill Easton (@strawgate) I guess we have very poor test coverage. It would be great if you added new ones. Also we have some issues for Json cmdlets, please look while you're at the beginning of the journey.

  14. strawgate commented on Jun 29, 2021

    @strawgate
    ContributorAuthor

    I have a PR here that improves performance but unfortunately does not impact memory utilization: #15684

  15. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  16. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  17. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  18. added
    Resolution-No Activity
    on Nov 16, 2023
  19. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-EnhancementResolution-No ActivityWG-CmdletsWG-Engine-Performance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions