PowerShell Invoke-RestMethod问题和需要确定堆内存

时间:2017-07-26 18:21:27

标签: java powershell tomcat

使用以下代码:https://www.reddit.com/r/PowerShell/comments/3iced0/gettomcat_status/

如果我们调用以下网址:http://RemoteServer/manager/status?XML=true

我们在下面看到堆内存:

  • 姓名:PS Eden Space
  • 姓名:PS Old Gen
  • 姓名:PS Survivor Space

我遇到的问题是为Used和Maximum添加所有这三个似乎都是关闭的(方式太高)。

每个人都有堆内存。这是否意味着堆内存是附加的还是包含所有这三个?

Memory Pools
Name:PS Eden Space Type:Heap memory Initial:1210056704 Committed:1155530752 Maximum:1168113664 Used:984427608 
Name:PS Old Gen Type:Heap memory Initial:538968064 Committed:538968064 Maximum:538968064 Used:507145336 
Name:PS Survivor Space Type:Heap memory Initial:199229440 Committed:226492416 Maximum:226492416 Used:0 

让我们展示一些代码:

function Get-TomcatStatus{
     param(
     $fileName,
         $tomcatserver ,
         $tomcatport = 80,
         $tomcatuser = "login",
         $tomcatpassword = "pass"
     )
     # https://www.reddit.com/r/PowerShell/comments/3iced0/gettomcat_status/
     #Get Credentials into the right format
     $tomcatpassword = ConvertTo-SecureString -string $tomcatpassword -asplaintext -force
     $cred = New-object -TypeName System.Management.Automation.PSCredential -argumentlist ($tomcatuser,$tomcatpassword)

     #Invoke the restinterface 
     Write-Host http://$tomcatserver`:$tomcatport/manager/status?XML=true
     $TomcatStats = Invoke-RestMethod -Uri http://$tomcatserver`:$tomcatport/manager/status?XML=true -Credential $cred

     # <threadInfo  maxThreads="150" currentThreadCount="25" currentThreadsBusy="1" />
     $threads = $TomcatStats.GetElementsByTagName("threadInfo") | ? {$_.currentThreadCount -ne 0}

     $Memory = $TomcatStats.GetElementsByTagName("memory") 

     # <memorypool name='PS Eden Space' type='Heap memory' usageInit='1210056704' usageCommitted='1155530752' usageMax='1168113664' usageUsed='1027265568'/>
     $MemoryEden = $TomcatStats.GetElementsByTagName("memorypool") | ? {$_.name -eq "PS Eden Space"}
     $MemoryOldGen = $TomcatStats.GetElementsByTagName("memorypool") | ? {$_.name -eq "PS Old Gen"}
     $MemorySurvivor = $TomcatStats.GetElementsByTagName("memorypool") | ? {$_.name -eq "PS Survivor Space"}

     # <requestInfo  maxTime="6302" processingTime="32729" requestCount="260" errorCount="215" bytesReceived="61985" bytesSent="162507" />
     $requestInfo = $TomcatStats.GetElementsByTagName("requestInfo") | ? {$_.bytessent -ne 0}

     #Populate output object
     #$Output = New-object PsObject
     #$output | add-member -type noteproperty -name "Server" -Value $tomcatserver
Add-Content $fileName "<tr>"
     Add-Content $fileName "<td align='center'> $tomcatserver ($ip) </td>"
     #Memory
     #$output | add-member -type noteproperty -name "Memory_Used" -Value $memory.total
    $memorytotal = $memory.total
    $memorytotal = $memorytotal/1MB
    $memorytotal = $memorytotal.ToString("00.00")
    Add-Content $fileName "<td align='center'> $memorytotal MB</td>"
     #$output | add-member -type noteproperty -name "Memory_free" -Value $memory.free
    $memoryfree = $memory.free
    $memoryfree = $memoryfree/1MB
    $memoryfree = $memoryfree.ToString("00.00")
    Add-Content $fileName "<td align='center'> $memoryfree MB</td>"
     #$output | add-member -type noteproperty -name "Memory_Max" -Value $memory.max
    $memorymax = $memory.max
    $memorymax = $memorymax/1MB
    $memorymax = $memorymax.ToString("00.00")
    Add-Content $fileName "<td align='center'> $memorymax MB</td>"
    #Name:PS Eden Space Type:Heap memory Initial:1210056704 Committed:1155530752 Maximum:1168113664 Used:1027265568 

    $memoryEdenUsed = $MemoryEden.usageUsed
    $memoryOldGenUsed = $MemoryOldGen.usageUsed
    $memorySurvivorUsed = $MemorySurvivor.usageUsed
    $memoryHeapUsed = $memoryEdenUsed + $memoryOldGenUsed + $memorySurvivorUsed
    $memoryHeapUsed = $memoryHeapUsed/1MB
    $memoryHeapUsed = $memoryHeapUsed.ToString("00.00")
    Add-Content $fileName "<td align='center'> $memoryHeapUsed MB</td>"

    $memoryEdenMax = $MemoryEden.usageMax
    $memoryOldGenMax = $MemoryOldGen.usageMax
    $memorySurvivorMax = $MemorySurvivor.usageMax
    $memoryHeapMax = $memoryEdenMax + $memoryOldGenMax + $memorySurvivorMax
    $memoryHeapMax = $memoryHeapMax/1MB
    $memoryHeapMax = $memoryHeapMax.ToString("00.00")
    Add-Content $fileName "<td align='center'> $memoryHeapMax MB</td>"
    $PercentUsed = ($memoryHeapUsed / $memoryHeapMax)*100
    $PercentUsed = [Math]::Round($PercentUsed, 0)
    Add-Content $fileName "<td align='center'> $PercentUsed %</td>"
     #threads
     #$output | add-member -type noteproperty -name "Current_Threads_busy" -Value $threads.currentThreadsBusy
    $threadscurrentThreadsBusy = $threads.currentThreadsBusy
    Add-Content $fileName "<td align='center'> $threadscurrentThreadsBusy </td>"
     #$output | add-member -type noteproperty -name "Current_Thread_Count" -Value $threads.currentThreadCount
    $threadscurrentThreadCount = $threads.currentThreadCount
    Add-Content $fileName "<td align='center'> $threadscurrentThreadCount </td>"
     #$output | add-member -type noteproperty -name "Max_Threads" -Value $threads.MaxThreads
    $threadsMaxThreads = $threads.MaxThreads
    Add-Content $fileName "<td align='center'> $threadsMaxThreads </td>"
     #requestInfo
     #$output | add-member -type noteproperty -name "Request_Max_ProcessingTime_ms" -Value $requestInfo.maxTime
    $requestInfomaxTime = $requestInfo.maxTime
    Add-Content $fileName "<td align='center'> $requestInfomaxTime </td>"
     #$output | add-member -type noteproperty -name "Request_Count" -Value $requestInfo.requestcount
    $requestInforequestcount = $requestInfo.requestcount
    Add-Content $fileName "<td align='center'> $requestInforequestcount </td>"
     #$output | add-member -type noteproperty -name "ProcessingTime_total_s" -Value $requestInfo.processingTime
    $requestInfoprocessingTime = $requestInfo.processingTime
    Add-Content $fileName "<td align='center'> $requestInfoprocessingTime </td>"
Add-Content $fileName "</tr>"

     #Return $output
}

给我带来困难的部分是:

#Name:PS Eden Space Type:Heap memory Initial:1210056704 Committed:1155530752 Maximum:1168113664 Used:1027265568 

    $memoryEdenUsed = $MemoryEden.usageUsed
    $memoryOldGenUsed = $MemoryOldGen.usageUsed
    $memorySurvivorUsed = $MemorySurvivor.usageUsed
    $memoryHeapUsed = $memoryEdenUsed + $memoryOldGenUsed + $memorySurvivorUsed
    $memoryHeapUsed = $memoryHeapUsed/1MB
    $memoryHeapUsed = $memoryHeapUsed.ToString("00.00")
    Add-Content $fileName "<td align='center'> $memoryHeapUsed MB</td>"

    $memoryEdenMax = $MemoryEden.usageMax
    $memoryOldGenMax = $MemoryOldGen.usageMax
    $memorySurvivorMax = $MemorySurvivor.usageMax
    $memoryHeapMax = $memoryEdenMax + $memoryOldGenMax + $memorySurvivorMax
    $memoryHeapMax = $memoryHeapMax/1MB
    $memoryHeapMax = $memoryHeapMax.ToString("00.00")
    Add-Content $fileName "<td align='center'> $memoryHeapMax MB</td>"
    $PercentUsed = ($memoryHeapUsed / $memoryHeapMax)*100
    $PercentUsed = [Math]::Round($PercentUsed, 0)
    Add-Content $fileName "<td align='center'> $PercentUsed %</td>"

1 个答案:

答案 0 :(得分:0)

我想我想出来了..

读。

https://smarttechie.org/2016/08/15/understanding-the-java-memory-model-and-the-garbage-collection/

堆内存:JVM使用此内存来存储对象。这种记忆又分为两个不同的区域,称为“年轻代空间”和“终身空间”。

年轻一代:年轻一代或新太空被分为两个部分,称为“伊甸园空间”和“幸存者空间”。

Eden Space:当我们创建一个对象时,内存将从Eden Space中分配。

阅读本文,我们需要删除: 幸存者和老人 我们看到了有利的结果。

这是最终的代码:

function Get-TomcatStatus{
     param(
     $fileName,
         $tomcatserver ,
         $tomcatport = 80,
         $tomcatuser = "login",
         $tomcatpassword = "password"
     )
     # https://www.reddit.com/r/PowerShell/comments/3iced0/gettomcat_status/
     #Get Credentials into the right format
     $tomcatpassword = ConvertTo-SecureString -string $tomcatpassword -asplaintext -force
     $cred = New-object -TypeName System.Management.Automation.PSCredential -argumentlist ($tomcatuser,$tomcatpassword)

     #Invoke the restinterface 
     Write-Host http://$tomcatserver`:$tomcatport/manager/status?XML=true
     $TomcatStats = Invoke-RestMethod -Uri http://$tomcatserver`:$tomcatport/manager/status?XML=true -Credential $cred

     # <threadInfo  maxThreads="150" currentThreadCount="25" currentThreadsBusy="1" />
     $threads = $TomcatStats.GetElementsByTagName("threadInfo") | ? {$_.currentThreadCount -ne 0}

     $Memory = $TomcatStats.GetElementsByTagName("memory") 

     # <memorypool name='PS Eden Space' type='Heap memory' usageInit='1210056704' usageCommitted='1155530752' usageMax='1168113664' usageUsed='1027265568'/>
     $MemoryEden = $TomcatStats.GetElementsByTagName("memorypool") | ? {$_.name -eq "PS Eden Space"}
     #$MemoryOldGen = $TomcatStats.GetElementsByTagName("memorypool") | ? {$_.name -eq "PS Old Gen"}
     #$MemorySurvivor = $TomcatStats.GetElementsByTagName("memorypool") | ? {$_.name -eq "PS Survivor Space"}

     # <requestInfo  maxTime="6302" processingTime="32729" requestCount="260" errorCount="215" bytesReceived="61985" bytesSent="162507" />
     $requestInfo = $TomcatStats.GetElementsByTagName("requestInfo") | ? {$_.bytessent -ne 0}

     #Populate output object
     #$Output = New-object PsObject
     #$output | add-member -type noteproperty -name "Server" -Value $tomcatserver
    Add-Content $fileName "<tr>"
     Add-Content $fileName "<td align='center'> $tomcatserver ($ip) </td>"
     #Memory
     #$output | add-member -type noteproperty -name "Memory_Used" -Value $memory.total
    $memorytotal = $memory.total
    $memorytotal = $memorytotal/1MB
    $memorytotal = $memorytotal.ToString("00.00")
    Add-Content $fileName "<td align='center'> $memorytotal MB</td>"
     #$output | add-member -type noteproperty -name "Memory_free" -Value $memory.free
    $memoryfree = $memory.free
    $memoryfree = $memoryfree/1MB
    $memoryfree = $memoryfree.ToString("00.00")
    Add-Content $fileName "<td align='center'> $memoryfree MB</td>"
     #$output | add-member -type noteproperty -name "Memory_Max" -Value $memory.max
    $memorymax = $memory.max
    $memorymax = $memorymax/1MB
    $memorymax = $memorymax.ToString("00.00")
    Add-Content $fileName "<td align='center'> $memorymax MB</td>"
    #Name:PS Eden Space Type:Heap memory Initial:1210056704 Committed:1155530752 Maximum:1168113664 Used:1027265568 

    $memoryEdenUsed = $MemoryEden.usageUsed
    $memoryHeapUsed = $memoryEdenUsed
    $memoryHeapUsed = $memoryHeapUsed/1MB
    $memoryHeapUsed = $memoryHeapUsed.ToString("00.00")
    Add-Content $fileName "<td align='center'> $memoryHeapUsed MB</td>"

    $memoryEdenMax = $MemoryEden.usageMax
    $memoryHeapMax = $memoryEdenMax
    $memoryHeapMax = $memoryHeapMax/1MB
    $memoryHeapMax = $memoryHeapMax.ToString("00.00")
    Add-Content $fileName "<td align='center'> $memoryHeapMax MB</td>"
    $PercentUsed = ($memoryHeapUsed / $memoryHeapMax)*100
    $PercentUsed = [Math]::Round($PercentUsed, 0)
    Add-Content $fileName "<td align='center'> $PercentUsed %</td>"
     #threads
     #$output | add-member -type noteproperty -name "Current_Threads_busy" -Value $threads.currentThreadsBusy
    $threadscurrentThreadsBusy = $threads.currentThreadsBusy
    Add-Content $fileName "<td align='center'> $threadscurrentThreadsBusy </td>"
     #$output | add-member -type noteproperty -name "Current_Thread_Count" -Value $threads.currentThreadCount
    $threadscurrentThreadCount = $threads.currentThreadCount
    Add-Content $fileName "<td align='center'> $threadscurrentThreadCount </td>"
     #$output | add-member -type noteproperty -name "Max_Threads" -Value $threads.MaxThreads
    $threadsMaxThreads = $threads.MaxThreads
    Add-Content $fileName "<td align='center'> $threadsMaxThreads </td>"
     #requestInfo
     #$output | add-member -type noteproperty -name "Request_Max_ProcessingTime_ms" -Value $requestInfo.maxTime
    $requestInfomaxTime = $requestInfo.maxTime
    Add-Content $fileName "<td align='center'> $requestInfomaxTime </td>"
     #$output | add-member -type noteproperty -name "Request_Count" -Value $requestInfo.requestcount
    $requestInforequestcount = $requestInfo.requestcount
    Add-Content $fileName "<td align='center'> $requestInforequestcount </td>"
     #$output | add-member -type noteproperty -name "ProcessingTime_total_s" -Value $requestInfo.processingTime
    $requestInfoprocessingTime = $requestInfo.processingTime
    Add-Content $fileName "<td align='center'> $requestInfoprocessingTime </td>"
Add-Content $fileName "</tr>"

     #Return $output
}