others linux服务器运维 django3 监控 k8s golang 数据库 大数据 前端 devops 理论基础 java oracle 运维日志

centos7 限制普通用户内存使用大小

访问量:1203 创建时间:2021-02-17

查看内存大小:

[datam@hd ~]$ free 
              total        used        free      shared  buff/cache   available
Mem:       49292728     8180244    31254348     2418992     9858136    38226156
Swap:             0           0           0

限制datam用户只能使用30G内存

[datam@hd ~]$ cat /etc/security/limits.conf 
datam       soft  as    31457280
datam       hard  as    31457280

申请内存测试:

[datam@hd ~]$ cat a.c 
#include<stdio.h>                                                                                                                                                                                                                           
#include<stdlib.h>
size_t maximum=0;
int main(int argc,char *argv[])
{
    void * block;
    void * tmpblock;
    size_t blocksize[]={1024*1024, 1024, 1}; 
    int i,count;
    for(i=0;i<3;i++){
        for(count=1;;count++){
            block = malloc(maximum+blocksize[i]*count);
            if(block){
                tmpblock = block;
                maximum += blocksize[i]*count;
                free(block);
            }else{
                break;
            }   
        }   
    }   
    printf("maximum malloc size = %lf GB\n",maximum*1.0 / 1024.0 / 1024.0 / 1024.0);
    printf("the address is %x\n",tmpblock); 
    printf("the address end is %x\n", tmpblock + maximum);
    //while(1);
}
[datamp@hdpprde01 ~]$ gcc -o malloc  a.c
[datamp@hdpprde01 ~]$ ./malloc 
maximum malloc size = 29.933483 GB
the address is e4000010
the address end is 5fbe2e6d

引用文档:https://www.cnblogs.com/mikeguan/p/7109348.html

登陆评论: 使用GITHUB登陆