If you are using Windows, you can call Powershell memory and other performance objects for RGui and Memory Compression as system commands through R and call various memory counters. I don't have a path to store Powershell objects in R yet. Powershell Code for RGui and 'Memory Compression' which Windows uses to store frequently used objects:
$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
Powershell embedded in R:
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
Powershell Code:
$t1 | where {$_.ProcessName -eq "Rgui"} | Measure-Object -Maximum *memory* | ft Property,Maximum
Powershell embedded in R:
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
To see all the Rgui objects:
$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)
...
Rprof
2) Identify every non-low-level memory allocation withRprofmem
3) Identify each and every memory allocation using thevalgrind
tool (or similar). Really complicated. – CatchflyRprof
works see also: https://mcmap.net/q/393746/-interpretation-of-memory-profiling-output-of-rprof – Catchflydata.table
code option #2 (Rprofmem
) won't work correctly? – DeceitfulRprof
andgc
do work then but do offer only a guess which statement in the executed function to blame (due to the time interval probing and async garbage collection). If you want to find out more exactly how often and how much memory is allocated by a single statementRprofmem
is better because it tracks exactly this (except C-level/OS memory requests of course) but does not tell you the total amount of memory used. BTW: You can use both together at the same time with github.com/HenrikBengtsson/profmem – CatchflyRprof
norRprofmem
can track C/OS-level memory allocations and I IMHO wouldn't expectdata.table
to waste non-R-memory for data under R-sovereignity because this would be a waste of time (due to memory copies). Indexing may be an exception but takes much less space than the data (we are usingdata.table
for its ability to cope with big data sizes). But if you really want to know the total memory consumption (incl. C/OS-allocs) or look for memory leaks see the instructions here: cran.r-project.org/doc/manuals/r-release/… – Catchflyssh.utils::mem.usage
(which shows the process memory usage only) you could use the Windows task manager or Linuxtop
& friends. But what can do you do then with this value? You can run out of R-internal memory even when you still have enough computer memory since R uses its own memory allocation logic and this value does not give you any hint which R statement(s) to blame for that... – Catchflyssh.utils::mem.usage
reporting process-level memory isn't the same as the process-level memory shown intop
? I don't understand the distinction. – Deceitfuldata.table
may usecalloc/malloc
you think that the bulk of actual data allocation is being done through the R API. Does this suggest that if we're interested in how to correctly usedata.table
to be memory-efficient, thenRprof
/Rprofmem
is sufficient, but that if we think thatdata.table
doesn't allocate memory correctly, then we'd need to usevalgrind
? Am I interpreting what you're saying correctly? – Deceitful