Mic*_*ael 6 r memory-profiling data.table
在包含对data.table函数调用的R代码中分析内存的正确方法是什么?假设我要确定表达式期间的最大内存使用量。
该参考文献表明Rprofmem可能不是正确的选择:https :
 //cran.r-project.org/web/packages/profmem/vignettes/profmem.html
记录通过R的本机API的本机allocVector3()部分完成的所有内存分配,这意味着几乎所有的内存分配都被记录。R的垃圾收集器会在某些时候自动释放所有以这种方式分配的对象。profmem()不会记录垃圾收集事件。未记录的分配是由非R本机库或将内部代码Calloc()/ Free()用于内部对象的R包完成的分配。R垃圾收集器不处理此类对象。
data.table源代码包含对的大量调用Calloc(),malloc()因此这表明这Rprofmem将无法衡量data.table函数分配的所有内存。如果Rprofmem不是正确的工具,那么Matthew Dowle怎么在这里使用它:R:循环遍历data.table中的列?
我发现了一个参考文献,提出了类似的潜在问题gc()(可用于测量两次调用之间的最大内存使用量gc()):https :
 //r.789695.n4.nabble.com/Determining-the-maximum-memory-usage -of-a-function-td4669977.html
gc()是一个好的开始。在任务执行之前调用gc(reset = TRUE),在执行任务之后调用gc(),您会在此期间看到R占用的最大额外内存。(这不包括编译后的代码分配的内存,这在重新使用时很难测量。)
我没有发现任何暗示存在类似问题Rprof(memory.profiling=TRUE)。这是否意味着即使不总是使用R API分配内存,该Rprof方法data.table仍然适用?
如果Rprof(memory.profiling=TRUE)实际上不是该工作的正确工具,那是什么?
会ssh.utils::mem.usage工作吗?
如果您使用的是 Windows,则可以通过 R 将 RGUI 和内存压缩的 Powershell 内存和其他性能对象作为系统命令调用,并调用各种内存计数器。我还没有在 R 中存储 Powershell 对象的路径。Windows 用于存储常用对象的 RGui 和“内存压缩”的 Powershell 代码:
    $t1 = ps | where {$_.Name -EQ 'RGui' -or $_.Name -EQ 'Memory Compression'};
    $t2 = $t1 | Select { $_.Id;
    [math]::Round($_.WorkingSet64/1MB);
    [math]::Round($_.PrivateMemorySize64/1MB);
    [math]::Round($_.VirtualMemorySize64/1MB) };
    $t2 | ft * 
    $t1 | gm -View All
    $t1.Modules
    $t1.MaxWorkingSet
Run Code Online (Sandbox Code Playgroud)
R 中嵌入的 Powershell:
    ps_f <- function() { system("powershell -ExecutionPolicy Bypass -command $t1 = ps | where {$_.Name -EQ 'RGui' -or $_.Name -EQ 'Memory Compression'};
    $t2 = $t1 | Select { 
     $_.Id;
     [math]::Round($_.WorkingSet64/1MB);
     [math]::Round($_.PrivateMemorySize64/1MB);
     [math]::Round($_.VirtualMemorySize64/1MB) };
    $t2 | ft * "); }
    ps_f()
     $_.Id;                                                                                                                
     [math]::Round($_.WorkingSet64/1MB);                                                                                   
     [math]::Round($_.PrivateMemorySize64/1MB);                                                                            
     [math]::Round($_.VirtualMemorySize64/1MB)                                                                             
    -----------------------------------------------------------------------------------------------------------------------
    {2264, 1076, 3, 1401}                                                                                                  
    {15832, 3544, 6691, 11965}   
    ps_mem <- function() { system("powershell -ExecutionPolicy Bypass -command $t1 = ps | where {$_.Name -EQ 'RGui' -or $_.Name -EQ 'Memory Compression'};
    $t1 | Select ProcessName,MaxWorkingSet,MinWorkingSet,PagedMemorySize64,NonpagedSystemMemorySize64;")} 
    > ps_mem()
    ProcessName                : Memory Compression
    MaxWorkingSet              : 
    MinWorkingSet              : 
    PagedMemorySize64          : 3411968
    NonpagedSystemMemorySize64 : 0
    ProcessName                : Rgui
    MaxWorkingSet              : 1413120
    MinWorkingSet              : 204800
    PagedMemorySize64          : 7014719488
    NonpagedSystemMemorySize64 : 6662736
    # run some data.table operation
    > ps_mem()
    ProcessName                : Memory Compression
    MaxWorkingSet              : 
    MinWorkingSet              : 
    PagedMemorySize64          : 3411968
    NonpagedSystemMemorySize64 : 0
    ProcessName                : Rgui
    MaxWorkingSet              : 1413120
    MinWorkingSet              : 204800
    PagedMemorySize64          : 7015915520
    NonpagedSystemMemorySize64 : 6662736
Run Code Online (Sandbox Code Playgroud)
Powershell代码:
    $t1 | where {$_.ProcessName -eq "Rgui"} | Measure-Object -Maximum *memory* | ft  Property,Maximum
Run Code Online (Sandbox Code Playgroud)
R 中嵌入的 Powershell:
    ps_mem_ <- function() { system("powershell -ExecutionPolicy Bypass -command $t1 = ps | where {$_.Name -EQ 'RGui' -or $_.Name -EQ 'Memory Compression'};
    $t2 = $t1 | where {$_.ProcessName -eq 'Rgui'}; 
    $t2 | Measure-Object -Maximum *memory* | ft  Property,Maximum ")} 
    # having some problems with rollover...
    > ps_mem_()
    Property                       Maximum
    --------                       -------
    NonpagedSystemMemorySize       6662736
    NonpagedSystemMemorySize64     6662736
    PagedMemorySize            -1570734080
    PagedMemorySize64           7019200512
    PagedSystemMemorySize           680240
    PagedSystemMemorySize64         680240
    PeakPagedMemorySize        -1260961792
    PeakPagedMemorySize64      11623940096
    PeakVirtualMemorySize       -161009664
    PeakVirtualMemorySize64    17018859520
    PrivateMemorySize          -1570734080
    PrivateMemorySize64         7019200512
    VirtualMemorySize           -339103744
    VirtualMemorySize64        12545798144
    some data.table run
    > ps_mem_()
    Property                       Maximum
    --------                       -------
    NonpagedSystemMemorySize       6662736
    NonpagedSystemMemorySize64     6662736
    PagedMemorySize            -1570734080
    PagedMemorySize64           7019200512
    PagedSystemMemorySize           680240
    PagedSystemMemorySize64         680240
    PeakPagedMemorySize        -1260961792
    PeakPagedMemorySize64      11623940096
    PeakVirtualMemorySize       -161009664
    PeakVirtualMemorySize64    17018859520
    PrivateMemorySize          -1570734080
    PrivateMemorySize64         7019200512
    VirtualMemorySize           -339103744
    VirtualMemorySize64        12545798144
Run Code Online (Sandbox Code Playgroud)
要查看所有 Rgui 对象:
    $t1 | gm -View All
       TypeName: System.Diagnostics.Process
    Name                       MemberType     Definition
    ----                       ----------     ----------
    Handles                    AliasProperty  Handles = Handlecount
    Name                       AliasProperty  Name = ProcessName
    NPM                        AliasProperty  NPM = NonpagedSystemMemorySize64
    PM                         AliasProperty  PM = PagedMemorySize64
    SI                         AliasProperty  SI = SessionId
    VM                         AliasProperty  VM = VirtualMemorySize64
    WS                         AliasProperty  WS = WorkingSet64
    Disposed                   Event          System.EventHandler Disposed(System.Object, System.EventArgs)
    ErrorDataReceived          Event          System.Diagnostics.DataReceivedEventHandler ErrorDataReceived(System.Object, System.Diagnostics.DataReceivedEventArgs)
    ...
Run Code Online (Sandbox Code Playgroud)