0

I have written a script which assigns virtual or "simulated" memory to a process that I have created. What I want to know is, how can I then list how much memory the process has? I want it to return "process has x mbs" if I have assigned some memory or "process has currently no assigned memory" if I haven't assigned any memory.

How can I do this? I have included snippets of the code as it is too long to do so for pasting in here.

  1)
        read -p "Enter Memory Value [MB]" p 
        $script/simulate assignmem oxygen $p
        sleep 5
        ;;

    function assign_mem()
    { 
    stop_kill $1
        ulimit -m $2 
    "$script_folder"/"$1">/dev/null&
    echo "Process $1 Assigned $2MB Memory"
    }
paul
  • 197
  • 1
  • 2
  • 12

2 Answers2

0

From the man page for ubuntu proc - /proc file system

 /proc/[number]/maps
              A file containing the currently mapped memory regions and  their
              access permissions.
  The format is:

    address           perms offset  dev   inode      pathname
    08048000-08056000 r-xp 00000000 03:0c 64593      /usr/sbin/gpm
    08056000-08058000 rw-p 0000d000 03:0c 64593      /usr/sbin/gpm
    08058000-0805b000 rwxp 00000000 00:00 0
    40000000-40013000 r-xp 00000000 03:0c 4165       /lib/ld-2.2.4.so
    40013000-40015000 rw-p 00012000 03:0c 4165       /lib/ld-2.2.4.so
    4001f000-40135000 r-xp 00000000 03:0c 45494      /lib/libc-2.2.4.so
    40135000-4013e000 rw-p 00115000 03:0c 45494      /lib/libc-2.2.4.so
    4013e000-40142000 rw-p 00000000 00:00 0
    bffff000-c0000000 rwxp 00000000 00:00 0

However - ulimit does not automatically assign virtual memory to a process. It lets the process do things to acquire memory like: create shared memory segments, call one of several system calls to allocate memory. Your process has to do this.

jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
  • so I can't do something like this? 'code' function display_memory() { "$script_folder"/"$1">/dev/null& echo $2MB echo "Process $1 currently assigned $2MB Memory" } 1) read -p "display Memory Value [MB]" p $script/simulate displaymemory oxygen $p sleep 5 ;; 'code' – paul Mar 01 '15 at 15:55
0

You can find out how much virtual memory is assigned to a process using the ps command:

 ps -ovsize= 9999

where 9999 should be replaced with the process id.

The value produced will be in KiB; i.e., to get the actual number of bytes, you need to multiply by 1024.

The = suppresses printing a header line, which makes the result a simple number. You can specify more than one data element if that's useful; try, for example,

ps -e -opid,vsize,cmd

There are many other useful features of ps. man ps for details.


Independent of the above answer, it is worth noting that changing ulimit -m does not allocate memory (nor virtual memory space) to a process, and furthermore it is about resident memory, not virtual memory. You could use ulimit -v to limit the maximum amount of space that the process can request, but the process still needs to request the space before it will be assigned.

rici
  • 234,347
  • 28
  • 237
  • 341
  • ok well then will the above commands refer to the resident memory? essentially all i want is another option in my menu to be able to show that once ulimit has been used, the limit that is assigned to that process. e.g. ulimit -v 100mb. check that process -"it has 100mb" or "it has no mb" if i haven't yet used ulimit. do you know the syntax for this? – paul Mar 01 '15 at 16:59
  • @paul: You can use `ulimit -m` itself to produce that information, but not for a specified process. The default is "unlimited", not 0. – rici Mar 01 '15 at 17:06
  • Ok, how would I write it in a manner similar to the above question? I want it to return all process assigned memory sizes so something like if a process has memory assigned(by me) then "process has x mbs assigned" and if not, "process has no mbs assigned." i can edit above to post my full code if you like. – paul Mar 02 '15 at 22:21