[Closed] another MXS memory leaking….
This seems to prevent the memory leaking.
(
st = timestamp(); sh = heapfree
for k = 1 to 1000000 do
(
tm1 = matrixfromnormal (random -[1,1,1] [1,1,1])
tm2 = matrixfromnormal (random -[1,1,1] [1,1,1])
tm3 = tm1 * tm2
[B]free tm3[/B]
)
format "time:% heap:%
" (timestamp()-st) (sh-heapfree)
)
Even though it frees the matrix, the value seems to keep there, and so you can grab it later on. For example:
(
st = timestamp(); sh = heapfree
fn GetTM =
(
for k = 1 to 1000000 do
(
tm1 = matrixfromnormal (random -[1,1,1] [1,1,1])
tm2 = matrixfromnormal (random -[1,1,1] [1,1,1])
tm3 = tm1 * tm2
free tm3
if k == 1000000 do return tm3
)
)
tm = GetTM()
format "time:% heap:%
" (timestamp()-st) (sh-heapfree)
tm
)
Not sure how hard could it get to be implement it in a more complex environment.
It’s enough to put it behind then matrix operation… Although it makes the code running slower…
for i = 19685 to 19700 do (
t = timestamp()
m = heapfree
for k=1 to i do (matrix3 1 * matrix3 1)
format "iter:% time:% memory:%
" (i) (timestamp() - t) (m - heapfree)
)
for i = 19685 to 19700 do (
t = timestamp()
m = heapfree
for k=1 to i do free (matrix3 1 * matrix3 1)
format "iter:% time:% memory:%
" (i) (timestamp() - t) (m - heapfree)
)
iter:19685 time:8 memory:64L
iter:19686 time:7 memory:64L
iter:19687 time:8 memory:64L
iter:19688 time:8 memory:168L
iter:19689 time:8 memory:272L
iter:19690 time:8 memory:376L
iter:19691 time:7 memory:480L
iter:19692 time:9 memory:584L
iter:19693 time:8 memory:688L
iter:19694 time:8 memory:792L
iter:19695 time:7 memory:896L
iter:19696 time:8 memory:1000L
iter:19697 time:8 memory:1104L
iter:19698 time:8 memory:1208L
iter:19699 time:8 memory:1312L
iter:19700 time:8 memory:1312L
OK
iter with free:19685 time:10 memory:64L
iter with free:19686 time:9 memory:64L
iter with free:19687 time:9 memory:64L
iter with free:19688 time:9 memory:64L
iter with free:19689 time:9 memory:64L
iter with free:19690 time:9 memory:64L
iter with free:19691 time:9 memory:64L
iter with free:19692 time:9 memory:64L
iter with free:19693 time:9 memory:64L
iter with free:19694 time:10 memory:64L
iter with free:19695 time:9 memory:64L
iter with free:19696 time:9 memory:64L
iter with free:19697 time:10 memory:64L
iter with free:19698 time:9 memory:64L
iter with free:19699 time:9 memory:64L
iter with free:19700 time:10 memory:64L
OK
But anyway if you want to make it more memory efficient, and the speed doesn’t matter that much after a certain situation, then you have to make two different functions for the same purpose. One for less and another one for more operations… In this case the code runs faster on a smaller amount of operations, and memory efficient on higher iterations count.
Yes, it should be enough.
For 1 million iterations I get an average difference of around 2 % (40 milliseconds) slower when using free.
As the thread was about memory leaking, I think 40 milliseconds in 1 million iterations is not that bad if it prevents the leaking.
Up to 100K iterations, I don’t think it should be a concern to lose 5-10 milliseconds (generally speaking).
free, copy, delete, etc. this is not exact action in case of mxs using. it’s only RESERVED function names.
every developer can use it any way he wants. usually these function realized per class.
so ‘free matrix’ doesn’t really mean that it frees any memory or deletes instance of a matrix value. more likely it does do nothing
gc()
t = timestamp()
m = heapfree
for k=1 to 1000000 do (matrix3 1 * matrix3 1)
format "time:% memory:%
" (timestamp() - t) (m - heapfree)
gc()
t = timestamp()
m = heapfree
for k=1 to 1000000 do free (matrix3 1 * matrix3 1)
format "time:% memory:%
"(timestamp() - t) (m - heapfree)
Surprise…
If using this way, then it’s faster with free… And of course on higher iteration count, the difference is more significant…
507868808L
78593894
507868272L
OK
time:537 memory:203910568L
OK
507868824L
78594701
507868296L
OK
time:457 memory:2192L
OK
OK