In my program, I use tcmalloc for memory allocation and release. In order to keep the memory back to the memory in time, after the completion of the call, using** MallocExtension::instance()->ReleaseFreeMemory()**. However, this only ensures that real memory is freed and virtual memory is still occupied.
My program needs to mount a file to a specified memory address through shmat after a series of operations. If tcmalloc uses too much virtual memory, the mount address may become unavailable.
Is there a way to free virtual memory in tcmalloc, or is there a function available in Linux?
int main() {
int i = 0;
while (i < 100) {
char* a = (char*)malloc(1024 * 1024 * 2 * i);
malloc_stats();
free(a);
MallocExtension::instance()->ReleaseFreeMemory();
malloc_stats();
i++;
usleep(1000 * 300);
}
// in last loop , actual memory used is small , but Virtual address space used is big...
// if now I use shmat to mount something, it maybe fail.
return 0;
}
MALLOC: 207634928 ( 198.0 MiB) Bytes in use by application
MALLOC: + 0 ( 0.0 MiB) Bytes in page heap freelist
MALLOC: + 56408 ( 0.1 MiB) Bytes in central cache freelist
MALLOC: + 0 ( 0.0 MiB) Bytes in transfer cache freelist
MALLOC: + 440 ( 0.0 MiB) Bytes in thread cache freelists
MALLOC: + 11251864 ( 10.7 MiB) Bytes in malloc metadata
MALLOC: ------------
MALLOC: = 218943640 ( 208.8 MiB) Actual memory used (physical + swap)
MALLOC: + 10130219008 ( 9660.9 MiB) Bytes released to OS (aka unmapped)
MALLOC: ------------
MALLOC: = 10349162648 ( 9869.7 MiB) **Virtual address space used**
I tried to use MallocExtension::instance()->ReleaseFreeMemory() , but it only frees actual memory, but for virtual address. I hope the virtual addresses are also released.
I have the following gcroot output:
(lldb) gcroot 0x00007FC1EB641670
HandleTable:
00007FC3900B1388 (pinned handle)
-> 00007FC278027028 System.Object[]
-> 00007FC1E82CF848 System.Collections.Concurrent.ConcurrentDictionary`2[[Dapper.SqlMapper+Identity, Dapper.StrongName],[Dapper.SqlMapper+CacheInfo, Dapper.StrongName]]
-> 00007FBEEAA5E998 System.Collections.Concurrent.ConcurrentDictionary`2+Tables[[Dapper.SqlMapper+Identity, Dapper.StrongName],[Dapper.SqlMapper+CacheInfo, Dapper.StrongName]]
-> 00007FBEEAA51000 System.Collections.Concurrent.ConcurrentDictionary`2+Node[[Dapper.SqlMapper+Identity, Dapper.StrongName],[Dapper.SqlMapper+CacheInfo, Dapper.StrongName]][]
-> 00007FBE6BCB00C0 System.Collections.Concurrent.ConcurrentDictionary`2+Node[[Dapper.SqlMapper+Identity, Dapper.StrongName],[Dapper.SqlMapper+CacheInfo, Dapper.StrongName]]
-> 00007FBE6BCAFFA0 Dapper.SqlMapper+Identity
-> 00007FC1EB641670 System.String
Found 1 unique roots (run 'gcroot -all' to see all roots).
What is the meaning of the address 00007FC3900B1388? How can I locate the relevant logic in the code?
EDIT 1
The System.Object[] object at 00007FC278027028 takes 130584 bytes:
(lldb) dumpobj 00007FC278027028
Name: System.Object[]
MethodTable: 00007fc315075510
EEClass: 00007fc315075488
Size: 130584(0x1fe18) bytes
Array: Rank 1, Number of elements 16320, Type CLASS
I.e. it must be on LOH.
Please, find below the output of eeheap -gc
Number of GC Heaps: 8
------------------------------
Heap 0 (000000000125D930)
generation 0 starts at 0x00007FBE89EA6AA0
generation 1 starts at 0x00007FBE8895C1B0
generation 2 starts at 0x00007FBE67FFF000
ephemeral segment allocation context: none
segment begin allocated committed allocated size committed size
00007FBE67FFE000 00007FBE67FFF000 00007FBE8A4DD0A8 00007FBE94E3F000 0x224de0a8(575529128) 0x2ce40000(753139712)
Large object heap starts at 0x00007FC267FFF000
segment begin allocated committed allocated size committed size
00007FC267FFE000 00007FC267FFF000 00007FC277C6C938 00007FC277C8D000 0xfc6d938(264689976) 0xfc8e000(264822784)
00007FBCB442A000 00007FBCB442B000 00007FBCB51FCA48 00007FBCB51FD000 0xdd1a48(14490184) 0xdd2000(14491648)
Allocated Heap Size: Size: 0x32f1d428 (854709288) bytes.
Committed Heap Size: Size: 0x3d8a0000 (1032454144) bytes.
------------------------------
Heap 1 (0000000001265A20)
generation 0 starts at 0x00007FBF06D4BB48
generation 1 starts at 0x00007FBF0594AB08
generation 2 starts at 0x00007FBEE7FFF000
ephemeral segment allocation context: none
segment begin allocated committed allocated size committed size
00007FBEE7FFE000 00007FBEE7FFF000 00007FBF0766C740 00007FBF11B4B000 0x1f66d740(526833472) 0x29b4c000(699711488)
Large object heap starts at 0x00007FC277FFF000
segment begin allocated committed allocated size committed size
00007FC277FFE000 00007FC277FFF000 00007FC287F9DFF0 00007FC287FF5000 0xff9eff0(268038128) 0xfff6000(268394496)
00007FBC84424000 00007FBC84425000 00007FBC84EA2150 00007FBC84EA3000 0xa7d150(10998096) 0xa7e000(11001856)
Allocated Heap Size: Size: 0x30089880 (805869696) bytes.
Committed Heap Size: Size: 0x3a5c0000 (979107840) bytes.
------------------------------
Heap 2 (000000000126D8A0)
generation 0 starts at 0x00007FBF89893538
generation 1 starts at 0x00007FBF88849088
generation 2 starts at 0x00007FBF67FFF000
ephemeral segment allocation context: none
segment begin allocated committed allocated size committed size
00007FBF67FFE000 00007FBF67FFF000 00007FBF89EC4EB8 00007FBF94ADD000 0x21ec5eb8(569138872) 0x2cade000(749592576)
Large object heap starts at 0x00007FC287FFF000
segment begin allocated committed allocated size committed size
00007FC287FFE000 00007FC287FFF000 00007FC297FF8718 00007FC297FF9000 0xfff9718(268408600) 0xfffa000(268410880)
00007FBCE4430000 00007FBCE4431000 00007FBCE53E9098 00007FBCE53EA000 0xfb8098(16482456) 0xfb9000(16486400)
Allocated Heap Size: Size: 0x32e77668 (854029928) bytes.
Committed Heap Size: Size: 0x3da91000 (1034489856) bytes.
------------------------------
Heap 3 (0000000001275720)
generation 0 starts at 0x00007FC0079EF878
generation 1 starts at 0x00007FC006993E30
generation 2 starts at 0x00007FBFE7FFF000
ephemeral segment allocation context: none
segment begin allocated committed allocated size committed size
00007FBFE7FFE000 00007FBFE7FFF000 00007FC00C09B238 00007FC013259000 0x2409c238(604619320) 0x2b25a000(723886080)
Large object heap starts at 0x00007FC297FFF000
segment begin allocated committed allocated size committed size
00007FC297FFE000 00007FC297FFF000 00007FC2A7E5CBC0 00007FC2A7E7D000 0xfe5dbc0(266722240) 0xfe7e000(266854400)
00007FBCF4432000 00007FBCF4433000 00007FBCF55AB6D8 00007FBCF55CC000 0x11786d8(18319064) 0x1199000(18452480)
Allocated Heap Size: Size: 0x350724d0 (889660624) bytes.
Committed Heap Size: Size: 0x3c271000 (1009192960) bytes.
------------------------------
Heap 4 (000000000127D5A0)
generation 0 starts at 0x00007FC088CAE0B0
generation 1 starts at 0x00007FC087A4C4E8
generation 2 starts at 0x00007FC067FFF000
ephemeral segment allocation context: none
segment begin allocated committed allocated size committed size
00007FC067FFE000 00007FC067FFF000 00007FC08AC581F0 00007FC093EA6000 0x22c591f0(583373296) 0x2bea7000(736784384)
Large object heap starts at 0x00007FC2A7FFF000
segment begin allocated committed allocated size committed size
00007FC2A7FFE000 00007FC2A7FFF000 00007FC2B7F24C40 00007FC2B7F45000 0xff25c40(267541568) 0xff46000(267673600)
00007FBD237BE000 00007FBD237BF000 00007FBD24FBC270 00007FBD24FBD000 0x17fd270(25154160) 0x17fe000(25157632)
Allocated Heap Size: Size: 0x3437c0a0 (876069024) bytes.
Committed Heap Size: Size: 0x3d5eb000 (1029615616) bytes.
------------------------------
Heap 5 (0000000001285420)
generation 0 starts at 0x00007FC1097B5D90
generation 1 starts at 0x00007FC10872DB10
generation 2 starts at 0x00007FC0E7FFF000
ephemeral segment allocation context: none
segment begin allocated committed allocated size committed size
00007FC0E7FFE000 00007FC0E7FFF000 00007FC10DAD4388 00007FC114D2C000 0x25ad5388(632116104) 0x2cd2d000(752013312)
Large object heap starts at 0x00007FC2B7FFF000
segment begin allocated committed allocated size committed size
00007FC2B7FFE000 00007FC2B7FFF000 00007FC2C7DD3C68 00007FC2C7DF4000 0xfdd4c68(266161256) 0xfdf5000(266293248)
00007FBC74422000 00007FBC74423000 00007FBC74AD9BA0 00007FBC74ADA000 0x6b6ba0(7039904) 0x6b7000(7041024)
Allocated Heap Size: Size: 0x35f60b90 (905317264) bytes.
Committed Heap Size: Size: 0x3d1d9000 (1025347584) bytes.
------------------------------
Heap 6 (000000000128D2A0)
generation 0 starts at 0x00007FC1887CB3E0
generation 1 starts at 0x00007FC1877EFF98
generation 2 starts at 0x00007FC167FFF000
ephemeral segment allocation context: none
segment begin allocated committed allocated size committed size
00007FC167FFE000 00007FC167FFF000 00007FC18A95FE60 00007FC193A66000 0x22960e60(580259424) 0x2ba67000(732327936)
Large object heap starts at 0x00007FC2C7FFF000
segment begin allocated committed allocated size committed size
00007FC2C7FFE000 00007FC2C7FFF000 00007FC2D7F7E3F8 00007FC2D7FD5000 0xff7f3f8(267908088) 0xffd6000(268263424)
00007FBCD442E000 00007FBCD442F000 00007FBCD52B9180 00007FBCD52BA000 0xe8a180(15245696) 0xe8b000(15249408)
Allocated Heap Size: Size: 0x3376a3d8 (863413208) bytes.
Committed Heap Size: Size: 0x3c8c8000 (1015840768) bytes.
------------------------------
Heap 7 (0000000001295120)
generation 0 starts at 0x00007FC2086004E0
generation 1 starts at 0x00007FC2075711A8
generation 2 starts at 0x00007FC1E7FFF000
ephemeral segment allocation context: none
segment begin allocated committed allocated size committed size
00007FC1E7FFE000 00007FC1E7FFF000 00007FC20879C120 00007FC213FA3000 0x2079d120(544854304) 0x2bfa4000(737820672)
Large object heap starts at 0x00007FC2D7FFF000
segment begin allocated committed allocated size committed size
00007FC2D7FFE000 00007FC2D7FFF000 00007FC2E7F465B8 00007FC2E7F67000 0xff475b8(267679160) 0xff68000(267812864)
00007FBCC442C000 00007FBCC442D000 00007FBCC514E798 00007FBCC51A5000 0xd21798(13768600) 0xd78000(14123008)
Allocated Heap Size: Size: 0x31405e70 (826302064) bytes.
Committed Heap Size: Size: 0x3cc84000 (1019756544) bytes.
------------------------------
GC Allocated Heap Size: Size: 0x199cdd658 (6875371096) bytes.
GC Committed Heap Size: Size: 0x1e5872000 (8145805312) bytes.
The pinned handle value is 0x00007FC3900B1388. I am not sure how to compute the range that owns it. I do not see any numbers that produce a range that covers 0x00007FC3900B1388.
BTW, I have found the relevant static inside the Dapper.SqlMapper class:
public static class SqlMapper
{
...
private static readonly ConcurrentDictionary<Identity, CacheInfo> _queryCache;
}
https://github.com/DapperLib/Dapper/blob/ca00feeb5fafe5262166689c0bec2b80b53add4e/Dapper/SqlMapper.cs#L59
does any one encounter this kind of issue in java G1 gc
the first highlight user time is about 4 ms
but the second one user time is 0 ms and system time is about 4ms.
in G1 gc system time shouldn't be high, is it a bug in G1 gc?
below is my gc argunments
Xms200g -Xmx200g -Xmn30g -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSCompactAtFullCollection -XX:CMSMaxAbortablePrecleanTime=5000 -XX:+CMSClassUnloadingEnabled -XX:+CMSScavengeBeforeRemark -verbose:gc -XX:+PrintPromotionFailure -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -XX:+PrintHeapAtGC
2018-01-07T04:54:39.995+0800: 906650.864: [GC (Allocation Failure) 2018-01-07T04:54:39.996+0800: 906650.865: [ParNew
Desired survivor size 1610612736 bytes, new threshold 6 (max 6)
- age 1: 69747632 bytes, 69747632 total
- age 2: 9641544 bytes, 79389176 total
- age 3: 10522192 bytes, 89911368 total
- age 4: 11732392 bytes, 101643760 total
- age 5: 9158960 bytes, 110802720 total
- age 6: 10917528 bytes, 121720248 total
: 25341731K->170431K(28311552K), 0.2088528 secs] 153045380K->127882325K(206569472K), 0.2094236 secs] [Times: **user=4.53 sys=0.00, real=0.21 secs]**
Heap after GC invocations=32432 (full 10):
par new generation total 28311552K, used 170431K [0x00007f6058000000, 0x00007f67d8000000, 0x00007f67d8000000)
eden space 25165824K, 0% used [0x00007f6058000000, 0x00007f6058000000, 0x00007f6658000000)
from space 3145728K, 5% used [0x00007f6658000000, 0x00007f666266ffe0, 0x00007f6718000000)
to space 3145728K, 0% used [0x00007f6718000000, 0x00007f6718000000, 0x00007f67d8000000)
concurrent mark-sweep generation total 178257920K, used 127711893K [0x00007f67d8000000, 0x00007f9258000000, 0x00007f9258000000)
Metaspace used 54995K, capacity 55688K, committed 56028K, reserved 57344K
}
2018-01-07T04:54:40.205+0800: 906651.074: Total time for which application threads were stopped: 0.2269738 seconds, Stopping threads took: 0.0001692 seconds
{Heap before GC invocations=32432 (full 10):
par new generation total 28311552K, used 25336255K [0x00007f6058000000, 0x00007f67d8000000, 0x00007f67d8000000)
eden space 25165824K, 100% used [0x00007f6058000000, 0x00007f6658000000, 0x00007f6658000000)
from space 3145728K, 5% used [0x00007f6658000000, 0x00007f666266ffe0, 0x00007f6718000000)
to space 3145728K, 0% used [0x00007f6718000000, 0x00007f6718000000, 0x00007f67d8000000)
concurrent mark-sweep generation total 178257920K, used 127711893K [0x00007f67d8000000, 0x00007f9258000000, 0x00007f9258000000)
Metaspace used 54995K, capacity 55688K, committed 56028K, reserved 57344K
2018-01-07T04:55:02.541+0800: 906673.411: [GC (Allocation Failure) 2018-01-07T04:55:02.542+0800: 906673.411: [ParNew
Desired survivor size 1610612736 bytes, new threshold 6 (max 6)
- age 1: 93841912 bytes, 93841912 total
- age 2: 11310104 bytes, 105152016 total
- age 3: 8967160 bytes, 114119176 total
- age 4: 10278920 bytes, 124398096 total
- age 5: 11626160 bytes, 136024256 total
- age 6: 9077432 bytes, 145101688 total
: 25336255K->195827K(28311552K), 0.1926783 secs] 153048149K->127918291K(206569472K), 0.1932366 secs] [Times: **user=0.00 sys=4.07, real=0.20 secs]**
Heap after GC invocations=32433 (full 10):
par new generation total 28311552K, used 195827K [0x00007f6058000000, 0x00007f67d8000000, 0x00007f67d8000000)
eden space 25165824K, 0% used [0x00007f6058000000, 0x00007f6058000000, 0x00007f6658000000)
from space 3145728K, 6% used [0x00007f6718000000, 0x00007f6723f3cf38, 0x00007f67d8000000)
to space 3145728K, 0% used [0x00007f6658000000, 0x00007f6658000000, 0x00007f6718000000)
concurrent mark-sweep generation total 178257920K, used 127722463K [0x00007f67d8000000, 0x00007f9258000000, 0x00007f9258000000)
Metaspace used 54995K, capacity 55688K, committed 56028K, reserved 57344K
}
2018-01-07T04:55:02.735+0800: 906673.604: Total time for which application threads were stopped: 0.2149603 seconds, Stopping threads took: 0.0002262 seconds
2018-01-07T04:55:14.673+0800: 906685.542: Total time for which application threads were stopped: 0.0183883 seconds, Stopping threads took: 0.0002046 seconds
2018-01-07T04:55:14.797+0800: 906685.666: Total time for which application threads were stopped: 0.0135349 seconds, Stopping threads took: 0.0002472 seconds
2018-01-07T04:55:14.810+0800: 906685.679: Total time for which application threads were stopped: 0.0129019 seconds, Stopping threads took: 0.0001014 seconds
2018-01-07T04:55:14.823+0800: 906685.692: Total time for which application threads were stopped: 0.0125939 seconds, Stopping threads took: 0.0002915 seconds
2018-01-07T04:55:21.597+0800: 906692.466: Total time for which application threads were stopped: 0.0137018 seconds, Stopping threads took: 0.0001683 seconds
{Heap before GC invocations=32433 (full 10):
your command-line specifies -XX:+UseConcMarkSweepGC - this isn't a G1 issue.
Situation: estimate if you can compute big matrix with your Ram and Swap in Linux Matlab
I need the sum of Mem and Swap, corresponding values by free -m under Heading total in Linux
total used free shared buff/cache available
Mem: 7925 3114 3646 308 1164 4220
Swap: 28610 32 28578
Free Ram memory in Matlab by
% http://stackoverflow.com/a/12350678/54964
[r,w] = unix('free | grep Mem');
stats = str2double(regexp(w, '[0-9]*', 'match'));
memsize = stats(1)/1e6;
freeRamMem = (stats(3)+stats(end))/1e6;
Free Swap memory in Matlab: ...
Relation between Memory requirement and Matrix size of Matlab: ...
Testing Suever's 2nd iteration
Suever's command gives me 29.2 GB that is corresponding to free's output so correct
$ free
total used free shared buff/cache available
Mem: 8115460 4445520 1956672 350692 1713268 3024604
Swap: 29297656 33028 29264628
System: Linux Ubuntu 16.04 64 bit
Linux kernel: 4.6
Linux kernel options: wl, zswap
Matlab: 2016a
Hardware: Macbook Air 2013-mid
Ram: 8 GB
Swap: 28 Gb on SSD (set up like in the thread How to Allocate More Space to Swap and Increase its Size Greater than Ram?)
SSD: 128 GB
You can just make a slight modification to the code that you've posted to get the swap amount.
function freeMem = freeMemory(type)
[r, w] = unix(['free | grep ', type]);
stats = str2double(regexp(w, '[0-9]*', 'match'));
memsize = stats(1)/1e6;
if numel(stats) > 3
freeMem = (stats(3)+stats(end))/1e6;
else
freeMem = stats(3)/1e6;
end
end
totalFree = freeMemory('Mem') + freeMemory('Swap')
To figure out how much memory a matrix takes up, use the size of the datatype and multiply by the number of elements as a first approximation.
I'm a new user of IOzone, when I run the IOzone with the command: ./iozone -i 0 -i 1 -t 2 -T, it generates the following result(partially):
Command line used: ./iozone -i 0 -i 1 -t 2 -T
Output is in Kbytes/sec
Time Resolution = 0.000001 seconds.
Processor cache size set to 1024 Kbytes.
Processor cache line size set to 32 bytes.
File stride size set to 17 * record size.
Throughput test with 2 threads
Each thread writes a 512 Kbyte file in 4 Kbyte records
Children see throughput for 2 initial writers = 650943.69 KB/sec
Parent sees throughput for 2 initial writers = 13090.24 KB/sec
Min throughput per thread = 275299.72 KB/sec
Max throughput per thread = 375643.97 KB/sec
Avg throughput per thread = 325471.84 KB/sec
Min xfer = 356.00 KB
Children see throughput for 2 rewriters = 1375881.50 KB/sec
Parent sees throughput for 2 rewriters = 10523.74 KB/sec
Min throughput per thread = 1375881.50 KB/sec
Max throughput per thread = 1375881.50 KB/sec
Avg throughput per thread = 687940.75 KB/sec
Min xfer = 512.00 KB
Children see throughput for 2 readers = 2169601.25 KB/sec
Parent sees throughput for 2 readers = 27753.94 KB/sec
Min throughput per thread = 2169601.25 KB/sec
Max throughput per thread = 2169601.25 KB/sec
Avg throughput per thread = 1084800.62 KB/sec
Min xfer = 512.00 KB
Children see throughput for 2 re-readers = 2572435.25 KB/sec
Parent sees throughput for 2 re-readers = 26311.78 KB/sec
Min throughput per thread = 2572435.25 KB/sec
Max throughput per thread = 2572435.25 KB/sec
Avg throughput per thread = 1286217.62 KB/sec
Min xfer = 512.00 KB
iozone test complete.
I get confused about meaning of "throughput" and "Min xfer", is there someone can help me?
By the way, why the throughput seen from children and parent is different? Thanks!
Min xfer refers to the smallest amount of data written at one time. "Each thread writes a 512 Kbyte file in 4 Kbyte records"
So if the Min xfer was 512.00 KB it wrote the entire actual file to disk at once (grouped all the 4 Kbyte records together).
Children and parent throughput are different due to OS I/O buffering. iozone doesn't force direct (non-buffered) read or writes with the throughput test. What you're really testing is your system's buffer cache + disk cache + disk speed combo.