Skip to content

Commit abf0c58

Browse files
committed
Switching to memuse to get physical memory
1 parent d7ef2ac commit abf0c58

4 files changed

Lines changed: 8 additions & 53 deletions

File tree

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package: ParallelLogger
22
Type: Package
33
Title: Support for Parallel Computation, Logging, and Function Automation
44
Version: 3.4.2
5-
Date: 2025-04-10
5+
Date: 2025-05-20
66
Authors@R: c(
77
person("Martijn", "Schuemie", email = "schuemie@ohdsi.org", role = c("aut", "cre")),
88
person("Marc", "Suchard", role = c("aut")),
@@ -22,7 +22,8 @@ Imports:
2222
jsonlite,
2323
methods,
2424
utils,
25-
rstudioapi
25+
rstudioapi,
26+
memuse
2627
Suggests:
2728
sendmailR,
2829
testthat,

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ ParallelLogger 3.4.2
33

44
Bugfixes
55

6-
1. Fixes `getPhysicalMemory()` on Mac machines (broken in previous fix).
6+
1. The `getPhysicalMemory()` package now uses the `memuse` package, for simplicity and better reliability.
77

88

99
ParallelLogger 3.4.1

R/Cluster.R

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -313,55 +313,14 @@ formatError <- function(threadNumber, error, args) {
313313
#' Get the total amount of physical memory
314314
#'
315315
#' @returns
316-
#' The number of GB of RAM. Returns NA if the function failed. One GB is
317-
#' 1,000,000,000 bytes.
316+
#' The number of GB of RAM. One GB is 1,000,000,000 bytes.
318317
#'
319318
#' @examples
320319
#' getPhysicalMemory()
321320
#'
322321
#' @export
323322
getPhysicalMemory <- function() {
324-
os <- Sys.info()[['sysname']]
325-
if (os == "Windows") {
326-
output <- tryCatch(
327-
system("wmic ComputerSystem get TotalPhysicalMemory /value", intern = TRUE),
328-
error = function(e) {
329-
return("")
330-
}
331-
)
332-
idx <- grep("TotalPhysicalMemory=", output, value = TRUE)
333-
if (length(idx) > 0) {
334-
memoryString <- gsub("TotalPhysicalMemory=", "", idx[1])
335-
memory <- as.numeric(memoryString)
336-
return(memory / (1e9)) # Convert to GB
337-
} else {
338-
return(NA)
339-
}
340-
} else if (os == "Linux" || os == "Darwin") {
341-
memory <- tryCatch(
342-
as.numeric(system("/usr/sbin/sysctl -n hw.memsize", intern = TRUE))/1e+09, # Convert to GB
343-
error = function(e) {
344-
return(NA)
345-
},
346-
warning = function(e) {
347-
return(NA)
348-
}
349-
)
350-
if (!is.na(memory)) {
351-
return(memory)
352-
} else {
353-
memory <- tryCatch({
354-
output <- system("grep MemTotal /proc/meminfo", intern = TRUE)
355-
output <- gsub("kB", "", gsub("MemTotal:", "", output), ignore.case = TRUE)
356-
as.numeric(output) / 1e6 # Convert to GB
357-
},
358-
error = function(e) {
359-
return(NA)
360-
})
361-
return(memory)
362-
}
363-
} else {
364-
warning("Operating system not supported.")
365-
return(NA)
366-
}
323+
memory <- memuse::Sys.meminfo()$totalram
324+
memory <- memuse::swap.unit(x, "GB")
325+
return(memory@size)
367326
}

tests/testthat/test-cluster.R

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,5 @@ test_that("Test getThreadNumber", {
7878

7979

8080
test_that("Test getPhysicalMemory", {
81-
# Very dirty, but skip_on_cran() doesn't work on CRAN's M1mac machine
82-
# (see https://github.com/r-lib/testthat/issues/2074)
83-
if (Sys.getenv("CDM5_POSTGRESQL_CDM_SCHEMA", unset = "") == "") {
84-
skip("Not an OHDSI machine, so skipped test for getPhysicalMemory()")
85-
}
8681
expect_false(is.na(getPhysicalMemory()))
8782
})

0 commit comments

Comments
 (0)