0

I have a newb question : I remove an element in a global list but I a function by using a return(). I saw that my other element still at his address (using inspect), but the object after the operation doesn't have the same tracemem. Maybe I don't understand tracemem but after my remove, did I move or copy something ?! I would like to parallelize some operations with a big data in a list object, remove the variable when I finish to free the memory so I want to be sure I work with the same object.

Thank you.

reference information : How can I remove an element from a list?

# WHEN YOU HAVE BIG DATA, after a process you want to free some memory of your
# object by remove a variable data
# E.G open a *.mat file with structured style -> list
#
# Try with big data and wait a while (your computer) to know
# object_size and mem_used()
#

#------------------------------------------------------------
#With Global Variable
x <- list('a'=123456789,'b'='highlander')
names(x)
paste(length(x),object_size(x),mem_used(),tracemem(x))
.Internal(inspect(x))

# Remove data
x['a'] <- NULL
names(x)
paste(length(x),object_size(x),mem_used(),tracemem(x))
.Internal(inspect(x))
#------------------------------------------------------------
# with function 1 - NOT WORKED
# FREE MEMORY
freeMemoryFromStructuredList1 <- function(x,select){
  paste(length(x),object_size(x),mem_used(),tracemem(x))
  x[select]<- NULL
}

#With Function 1
x <- list('a'=123456789,'b'='highlander')
names(x)
paste(length(x),object_size(x),mem_used(),tracemem(x))
.Internal(inspect(x))

# TRy to Remove data
freeMemoryFromStructuredList1(x,'a')
names(x)
paste(length(x),object_size(x),mem_used(),tracemem(x))
.Internal(inspect(x))

#------------------------------------------------------------
#With Function 2 (return) - WORK, Highlander at the same Address,
# but tracemem NOT

# FREE MEMORY
freeMemoryFromStructuredList2 <- function(x,select){
  paste(length(x),object_size(x),mem_used(),tracemem(x)) 
  x[select]<- NULL
  return( x )
}

x <- list('a'=123456789,'b'='highlander')
names(x)
paste(length(x),object_size(x),mem_used(),tracemem(x))
.Internal(inspect(x))

# Remove data
x <-freeMemoryFromStructuredList2(x,'a')
names(x)
paste(length(x),object_size(x),mem_used(),tracemem(x))
.Internal(inspect(x))
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
swatz
  • 1

1 Answers1

1

This has to do with scoping and R functions.

R functions pass by value, not by reference, so when you call function 1 you are making a copy of the list and removing the selected element from that copy. The scope of the copy is the function, so it is deleted when the function finishes since you don't return it. Your original list remains unchanged.

Function 2 works because you return the copy, and reset your original list with the copy, which now has an element removed. The tracemem likely changes with function 2 because now your data structure is not using the same amount of memory (as it is one element less) so it occupies a slightly different location than the original. That's just an educated guess though.

Hopefully that clears things up, I don't know about how you want to parallelize things so I can't speak to that.

DMT
  • 1,577
  • 10
  • 16
  • That's what I assumed with the results, but I hope there is a way to pass by reference like in C. I no longer seek further. Thanks DMT – swatz Sep 18 '14 at 17:48