It is always a good idea to read the FAQ list, also installed on your computer, before wasting your time.įor example, How can I make my Perl program take less memory? is probably relevant to your issue. Some operating systems (notably, systems that use mmap(2) for allocating large chunks of memory) can reclaim memory that is no longer used, but on such systems, perl must be configured and compiled to use the OS's malloc, not perl's. That's why long-running programs sometimes re- exec themselves. On most operating systems, memory allocated to a program can never be returned to the system. Memory allocated to global variables can be reused (within your program) by using undef() and/or delete(). It is reserved in case the variables come back into scope. my() variables) cannot be reclaimed or reused even if they go out of scope. See the FAQ: How can I free an array or hash so my program shrinks?. In general, you cannot expect perl to release memory to the OS. (Basically, whatever BDB's pager decides to keep in memory this is controllable.) You can store gigabytes of data in a hash this way, and you will only use a tiny bit of memory. Or die "Cannot open file $filename: $! $BerkeleyDB::Error\n" If you want to work with large datasets, don't load the whole thing into memory, use something like BerkeleyDB:Įxample code, stolen verbatim: use strict If you are using Linux with a recent glibc, and are using that malloc, you can return free'd memory to the OS. Generally, yeah, that's how memory management on UNIX works. Any ideas how to free the memory of the hash?Īlso Devel::Size shows the hash is only taking up 92 bytes even though the program still is using 139K. So as you can see the memory goes down, but it only goes down the size of the scalar. Length of Scalar: 0 Scalar Mem Total Size: 24 Length of Scalar: 0 Scalar Mem Total Size: 111100028Ĭurrent Memory Usage: Virt: 139K RES: 135K Length of Scalar: 0 Scalar Mem Total Size: 12Īdding data to shared varable 100000 timesĬurrent Memory Usage: Virt: 228K RES: 224K Find out what level of support we can provide for your Perl versions and platforms. Length of Hash: 0 Hash Mem Total Size: 92 ActiveState offers commercial support for current and older versions of Perl, ranging from security fixes and module updates for Perl 5.16 - 5.28, to access to the ActivePerl distribution for Perl 5.8 - 5.14. " Scalar Mem Total Size: $total_size\n" Ĭurrent Memory Usage: Virt: 6.9K RES: 2.7K My $total_size = total_size(\%share_hash) Print "Current Memory Usage: Virt: $virt RES: $res\n" My($results) = grep !/grep/, Parse Data from Shell # Method to fix hash (Trying Everything i can think of! Print "Adding data to shared varable $total_loops times\n" Print "Hit Enter (Set Varable to nothing): " Use Number::Bytes::Human qw(format_bytes) When I do the same with a scalar and use undef, it will give the memory back to the OS. ![]() When I fill up a big hash, I can not get the memory to be released back to the OS. Thanks to everyone leading me to that solution.I am having some problems with memory in Perl. This way It doesn't full the virtual memory and other daemon are not killed. ![]() In fact the main program (the long running one) have been separated and I use small program who wait before all threads finish to exit. I resolved my problem, I changed my structure. So unless I kill the program the memory is still in use. So all thread exited but the memory usage is still 2.5% of my server total memory. I used 800 threads and all verified that they exit by printing the $ee var.īut once entering the while(1) loop here's the ps aux | grep perl output: ps aux | grep perl Once it exit the for loop and enter the infinite loop all the memory that have not been freed into the for loop is never released back to the operating system. The second code is releasing memory too but only when he's running inside the "for" loop. The first code run only one infinite loop and it release memory back to operating system about each 2 seconds (according to ps) I am now searching for 2 weeks about this problem and seems that I can't find any answer just searching on the web.
0 Comments
Leave a Reply. |