Linux 虚拟内存限制

Linux virtual memory limit

我正在尝试为进程 (Tsw) 分配内存。不幸的是,我不能分配超过 ~ 1 Gb 的虚拟内存(在 for 循环中分配 2MB 块)。执行期间无限制更改命令。

这是 cat /proc/$(pidof Tsw)/limits 命令的输出。

Limit                     Soft Limit           Hard Limit           Units     
Max cpu time              unlimited            unlimited            seconds   
Max file size             unlimited            unlimited            bytes     
Max data size             unlimited            unlimited            bytes     
Max stack size            8388608              unlimited            bytes     
Max core file size        0                    unlimited            bytes     
Max resident set          unlimited            unlimited            bytes     
Max processes             13633                13633                processes 
Max open files            1024                 4096                 files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       13633                13633                signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us        

这里是top命令的部分输出(注意/usr/bin/Tsw,从顶部数第三个过程):

Mem: 1082976K used, 990736K free, 0K shrd, 128K buff, 28136K cached
CPU:   0% usr   4% sys   0% nic  95% idle   0% io   0% irq   0% sirq
Load average: 1.07 0.36 0.13 1/242 2007
  PID  PPID USER     STAT   VSZ %VSZ %CPU COMMAND
 1730  1690 root     S     179m   9%   2% /usr/bin/btsvc_main_9300
  585     1 root     S    45784   2%   2% /usr/bin/brsCore --test-software
 1691     1 root     S     997m  49%   0% /usr/bin/Tsw
 1941     1 root     S     121m   6%   0% /usr/sbin/unifi_helper_tsw --wifi-ptest --wifi-char-device /dev/unifiudi0 --wifi-ptest-firmware ptest/firmware/ptest_unifi_93xx_a07_ptdl.xbv --wifi-ptest-profile ptest/profile/unitest_profile_CSRC9xxx_Cust_BGA.txt --log-level-text CsrWifiPtestApp..A
 1687  1683 root     S     119m   6%   0% /usr/sbin/unifi_helper_tsw --wifi-char-device /dev/unifi0 --wifi-on --wifi-restart-on-error --wifi-exit-on-error --wifi-exit-on-unplug --wifi-address 00:1C:C1:A2:5D:FF --wifi-mib --wifi-add-interface ff:ff:ff:ff:ff:ff
 1693     1 root     S    70452   3%   0% /usr/bin/avTSWCdspAudio.exe /usr/lib/audiomanager/routing/libavPluginRoutingDirana.so
 1219     1 root     S    66740   3%   0% /usr/bin/TSWNav_GNSS
 1215     1 root     S    63460   3%   0% /usr/bin/dlt-system -c /etc/dlt-system.conf
 1039     1 root     S    50464   2%   0% /usr/bin/avDiranaBoot.exe -t DIRANA3 -p /run/audio/dirana_boot.pipe -r 0 -d 0
 1216     1 root     S    48400   2%   0% /usr/bin/RadioAppTSW
 1493     1 root     S    47184   2%   0% /usr/bin/ssw_pers_pccl_hwi_svc
 1494     1 root     S    47180   2%   0% /usr/bin/ssw_pers_pccl_early_svc
 1690     1 root     S    35672   2%   0% /usr/bin/btsvc_main_9300
 1692     1 root     S    30516   1%   0% /usr/bin/tsw-apple-authic-info
    1     0 root     S    30060   1%   0% {systemd} /sbin/init
 1225     1 root     S    28224   1%   0% /usr/bin/TSWNav_Gyro
 1224     1 root     S    28220   1%   0% /usr/bin/TSWNav_Accel
 1212     1 root     S    26968   1%   0% /usr/bin/tsw-ipc-sample
  793     1 root     S    19532   1%   0% /usr/bin/ssw_pers_nba_svc
  622     1 root     S     6656   0%   0% /usr/lib/systemd/systemd-journald
 1232     1 root     S     4440   0%   0% /usr/sbin/sshd -D
  746     1 root     S     3448   0%   0% /usr/lib/systemd/systemd-udevd
 2003  1691 root     S     2960   0%   0% sh -c mount -o remount, rw / && cd usr/bin && top -b -n 1 > statistics_after.txt
 2007  2003 root     R     2960   0%   0% top -b -n 1
 1682    30 root     S     2960   0%   0% {unififw} /bin/sh /usr/sbin/unififw 0 2
 1683  1682 root     S     2960   0%   0% {unififw} /bin/sh /usr/sbin/unififw 0 2
 1209     1 root     S     2708   0%   0% /usr/bin/dlt-daemon
 1694     1 root     S     2660   0%   0% /usr/sbin/conn_wifi_tsw
 1297     1 messageb S     2552   0%   0% /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
 1685  1682 root     S     1820   0%   0% logger -t unififw -p daemon.notice
 1625     1 root     S     1816   0%   0% ldattach -8e1s 2000000 19 /dev/ttymxc1
 1161     2 root     SW       0   0%   0% [irq/65-2010000.]
    8     2 root     SW       0   0%   0% [rcu_preempt]
 1629     2 root     SW       0   0%   0% [hydra-sdio-io]
  482     2 root     SW       0   0%   0% [kworker/u8:2]
    6     2 root     SW       0   0%   0% [kworker/u8:0]
  560     2 root     SW       0   0%   0% [ubi_bgt0d]
 1577     2 root     SW       0   0%   0% [file_server_thr]
  699     2 root     SW       0   0%   0% [rtp_parser]
  754     2 root     SW       0   0%   0% [rtp_isr_data_th]
 1464     2 root     SW       0   0%   0% [mmcqd/1]
    3     2 root     SW       0   0%   0% [ksoftirqd/0]
  701     2 root     SW       0   0%   0% [rtp_write_threa]
  755     2 root     SW       0   0%   0% [spi_send_thread]
  756     2 root     SW       0   0%   0% [rtp_parser_thre]
 1163     2 root     SW       0   0%   0% [spi2]
 1387     2 root     SW       0   0%   0% [irq/54-mmc0]
 1630     2 root     SW       0   0%   0% [hydra/complete]
   30     2 root     SW       0   0%   0% [kworker/u8:1]
 1210     2 root     SW       0   0%   0% [kworker/0:2]
    2     0 root     SW       0   0%   0% [kthreadd]
    4     2 root     SW       0   0%   0% [kworker/0:0]
    5     2 root     SW<      0   0%   0% [kworker/0:0H]
    7     2 root     SW       0   0%   0% [migration/0]
    9     2 root     SW       0   0%   0% [rcu_bh]
   10     2 root     SW       0   0%   0% [rcu_sched]
   11     2 root     SW       0   0%   0% [watchdog/0]
   12     2 root     SW       0   0%   0% [watchdog/1]
   13     2 root     SW       0   0%   0% [migration/1]
   14     2 root     SW       0   0%   0% [ksoftirqd/1]
   15     2 root     SW       0   0%   0% [kworker/1:0]
   16     2 root     SW<      0   0%   0% [kworker/1:0H]
   17     2 root     SW       0   0%   0% [watchdog/2]
   18     2 root     SW       0   0%   0% [migration/2]
   19     2 root     SW       0   0%   0% [ksoftirqd/2]
   20     2 root     SW       0   0%   0% [kworker/2:0]
   21     2 root     SW<      0   0%   0% [kworker/2:0H]
   22     2 root     SW       0   0%   0% [watchdog/3]
   23     2 root     SW       0   0%   0% [migration/3]
   24     2 root     SW       0   0%   0% [ksoftirqd/3]
   25     2 root     SW       0   0%   0% [kworker/3:0]
   26     2 root     SW<      0   0%   0% [kworker/3:0H]
   27     2 root     SW<      0   0%   0% [khelper]
   28     2 root     SW       0   0%   0% [kdevtmpfs]
   29     2 root     SW<      0   0%   0% [netns]
   37     2 root     SW       0   0%   0% [kworker/0:1]
   38     2 root     SW       0   0%   0% [kworker/1:1]
   39     2 root     SW       0   0%   0% [kworker/2:1]
   40     2 root     SW       0   0%   0% [kworker/3:1]
  241     2 root     SW<      0   0%   0% [writeback]
  243     2 root     SW<      0   0%   0% [bioset]
  245     2 root     SW<      0   0%   0% [kblockd]
  279     2 root     SW       0   0%   0% [khubd]
  307     2 root     SW       0   0%   0% [ipu1_task]
  308     2 root     SW       0   0%   0% [ipu1_task]
  309     2 root     SW       0   0%   0% [ipu2_task]
  310     2 root     SW       0   0%   0% [ipu2_task]
  343     2 root     SW       0   0%   0% [kswapd0]
  402     2 root     SW       0   0%   0% [fsnotify_mark]
  406     2 root     SW<      0   0%   0% [crypto]
  416     2 root     SW<      0   0%   0% [shm_workqueue]
  429     2 root     SW       0   0%   0% [kworker/2:2]
  474     2 root     SW<      0   0%   0% [usbdiag_queue]
  481     2 root     SW<      0   0%   0% [ci_otg]
  511     2 root     SW<      0   0%   0% [deferwq]
  544     2 root     SW       0   0%   0% [gpmi_statistics]
  700     2 root     SW<      0   0%   0% [rtp_admin]
  753     2 root     SW       0   0%   0% [spi0]
  795     2 root     SW       0   0%   0% [kworker/1:2]
 1040     2 root     SW       0   0%   0% [kworker/3:2]
 1152     2 root     SW       0   0%   0% [spi1]
 1177     2 root     SW       0   0%   0% [spi4]
 1269     2 root     SW<      0   0%   0% [vpu_wq]
 1406     2 root     SW       0   0%   0% [ubifs_bgt0_60]
 1409     2 root     SW       0   0%   0% [irq/56-mmc1]
 1413     2 root     SW       0   0%   0% [kworker/u8:3]
 1465     2 root     SW       0   0%   0% [mmcqd/1boot0]
 1466     2 root     SW       0   0%   0% [mmcqd/1boot1]
 1467     2 root     SW       0   0%   0% [mmcqd/1rpmb]
 1480     2 root     SW<      0   0%   0% [kworker/0:1H]
 1482     2 root     SW<      0   0%   0% [kworker/2:1H]
 1486     2 root     SW<      0   0%   0% [kworker/1:1H]
 1533     2 root     SW       0   0%   0% [jbd2/mmcblk1p3-]
 1534     2 root     SW<      0   0%   0% [ext4-dio-unwrit]
 1536     2 root     SW       0   0%   0% [jbd2/mmcblk1p2-]
 1537     2 root     SW<      0   0%   0% [ext4-dio-unwrit]
 1578     2 root     SW<      0   0%   0% [coredump_wq]
 1631     2 root     SW       0   0%   0% [service_mgr_thr]
 1679     2 root     SW<      0   0%   0% [unifi_workq]
 1717     2 root     SW       0   0%   0% [ubifs_bgt0_50]
 1758     2 root     SW       0   0%   0% [ubifs_bgt0_17]
 1759     2 root     SW       0   0%   0% [UNIFI_BH]
 1766     2 root     SW       0   0%   0% [hydra/complete]
 1936     2 root     SW       0   0%   0% [reader]
 1937     2 root     SW       0   0%   0% [writer]

我是这样分配的:

// RAM_SIZE = 1.5 Gb
// RAM_BLOCK_TEST = 2 Mb
#define RAM_SIZE (1024U * 1024U * 1536U) 
#define RAM_BLOCK_TEST (1024U * 1024U * 2U)

uint32_t uiMemBlockCount = 0;
// uiLoopNr = 768
uint32_t uiLoopNr = (uint32_t)RAM_SIZE/(uint32_t)RAM_BLOCK_TEST;
uint8_t* pcMemArray[uiLoopNr];

for(uiMemBlockCount = 0; uiMemBlockCount < uiLoopNr; uiMemBlockCount++)
{
    pcMemArray[uiMemBlockCount] = (uint8_t*)calloc(RAM_BLOCK_TEST, sizeof(uint8_t));
    if (NULL == pcMemArray[uiMemBlockCount])
    {
        printf("Allocated %u block(s) of memory with %d size", uiMemBlockCount, RAM_BLOCK_TEST);
        break;
    }
}

也许 Linux OS 中还有其他限制不允许我这样做? 谢谢!

我找到问题了。

Virtual kernel memory layout:
vector : 0xffff0000 - 0xffff1000 ( 4 kB)
fixmap : 0xfff00000 - 0xfffe0000 ( 896 kB)
vmalloc : 0xc0800000 - 0xff000000 (1000 MB)
lowmem : 0x40000000 - 0xc0000000 (2048 MB)
modules : 0x3f000000 - 0x40000000 ( 16 MB)
.text : 0x40008000 - 0x40411ef4 (4136 kB)
.init : 0x40412000 - 0x4043ce80 ( 172 kB)
.data : 0x4043e000 - 0x404607c0 ( 138 kB)
.bss : 0x404607c0 - 0x40498208 ( 223 kB)

lowmem 从 0x40000000 开始。
所以我每个进程的虚拟内存不能超过 1 Gb。

这可以在内核配置中使用 CONFIG_PAGE_OFFSET 进行更改。