Memory allocation for particular process in UNIX


 
Thread Tools Search this Thread
Top Forums Programming Memory allocation for particular process in UNIX
# 1  
Old 02-26-2015
Blade Memory allocation for particular process in UNIX

I want to run a C program on my linux machine. I want to allocate specific heap size for that process (C program) to run.
How can I do that?
I know in Java same can be done using -Xmx option.
There may be some option which I can specify in the C program like Java or may be in linux process.
# 2  
Old 02-26-2015
Your allocator must have the same interface as the standard one, malloc, realloc, calloc, free, ... in your own shared library.

You can then preload your shared library as in:
CMD="LD_PRELOAD=/home/kbw/bin/myalloc.so prog arg1 arg2"
'$CMD'
or somthing like that.
# 3  
Old 02-26-2015
Memory allocation for particular process in UNIX

Thanks for your Reply Kbw but I did not understand that how would I allocate memory using this.
I want to allocate some part of RAM to this process using C program
# 4  
Old 02-26-2015
I assumed you had an allocator that you wanted to use. In fact, I assumed you had written an allocator that you wanted to use.

At any rate you have to have one in a shared library somewhere and my instructions show how to load it.

"I want to allocate some part of RAM to this process using C program" If that's what you want to do, what's wrong with the standard malloc?

Last edited by kbw; 02-26-2015 at 02:38 PM.. Reason: Add futher information
# 5  
Old 02-26-2015
C does not have a "virtual machine" or garbage collection like Java does, operates with virtual memory, and has its own heap segment which never shrinks, which all make preallocation generally pointless except for some extremely specialized tasks. C programmers are usually more concerned with limiting memory than preallocating it.

For these specialized tasks you can use mmap(), mlock(), and madvise() to control how/when the kernel pages memory.

In short, what exactly are you trying to optimize here?

Last edited by Corona688; 02-26-2015 at 02:51 PM..
# 6  
Old 02-26-2015
"I want to allocate specific heap size for that process (C program) to run."

I see, I must have been answering the wrong question. Ok, lets start again.

C does have a "virtual machine". In fact, the C virtual machine pretty much maps onto a real machine, that's what makes C so fast, because there's no translation. The C virtual machine has a call stack and a heap formed at opposite ends of the same memory block. The stack may have a fixed size, but the heap is everything else. So if you put more RAM in the machine, it shows up as more heap.

If you need memory you use it and hand it back when done. There's no portable way to put an upper limit on the heap. You may be able to specify it some custom heap, but not in a general way,

If you need memory just allocate it. The Java VM should coexist with the underlying system allocator.

So, in short, you can't limit it in a general way, and it shouldn't bother you that you can't.
# 7  
Old 02-26-2015
Quote:
Originally Posted by kbw
C does have a "virtual machine".
It has a real machine. Smilie

Quote:
The C virtual machine has a call stack and a heap formed at opposite ends of the same memory block. The stack may have a fixed size, but the heap is everything else.
It's not really a block of memory. It's all translated from virtual addresses to real addresses. Both the stack and the heap are automatically "sized" through the magic of virtual memory. The kernel itself, behind the scenes, cares very little about which is which -- it just assigns memory in fixed-sized blocks from the pile. See The Paging Game for a general explanation on how paging works.

Quote:
If you need memory you use it and hand it back when done.
Agreed. There's no java VM to worry about, only the kernel itself. You use it, and the kernel either lets you or not.

It doesn't get "handed back", though. Your program keeps it for later, just in case you need to allocate again -- this makes it faster, and probably why he wanted to preallocate, but the kernel handles this for you.

Quote:
There's no portable way to put an upper limit on the heap.
Check out setrlimit.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Memory allocation problem

I am using ubuntu. I have written a program to calculate prime factors. it works perfectly fine till entered number is less than 9989 (or so ) but when one enters a number higher than that, for example 15000, it does not work. Can anyone guide me whats the problem ? although new codes are welcome,... (2 Replies)
Discussion started by: Abhishek_kumar
2 Replies

2. Programming

C++/ROOT Memory Allocation?

Hello, I am new to C++ programming, so I'm still getting a feel for things. I recently wrote a simple C++ program (to be used as a ROOT Macro) to conduct a statistical analysis of a varied version of the Monty Hall problem (code below). Basically, the programs runs a few simple calculations to... (7 Replies)
Discussion started by: Tyler_92
7 Replies

3. HP-UX

Single Process Memory Allocation HP-UX 11i v3

Hi, I have HP-UX 11i v3 running on ia64. One of my application is 32-bit and I want to increase the memory allocation of this file upto 2GB. I am contentiously receiving an error message of Out of Memory. Can you please explain the procedure what kernel configuration( like maxdsize or... (1 Reply)
Discussion started by: ahmadamin416
1 Replies

4. Shell Programming and Scripting

memory allocation to a variable

hello all.. i'm a beginner in shell scripting. I need to know what is really happening when we are creating a variable in shell scripting? how memory is allocated for that variable? (3 Replies)
Discussion started by: aarathy
3 Replies

5. Programming

Memory allocation in C

Hi Experts I need some help in static memory allocation in C. I have a program in which I declared 2 variables, one char array and one integer. I was little surprised to see the addresses of the variables. First: int x; char a; printf("%u %u\n', &x, a); I got the addresses displayed... (2 Replies)
Discussion started by: unx_freak
2 Replies

6. Programming

memory allocation in subroutine

Hi everyone, I'm not new to C programming, but I'm having question regarding the memory allocation of a pointer variable which, for instance, will be declared in main(), but its memory will be allocated in subroutine. To clearify my question, I provide a small working example: #include... (1 Reply)
Discussion started by: MIB_Maik
1 Replies

7. HP-UX

HP-UX memory usage allocation

Hi all, I have a HP-UX Server with 4 gigabytes of physical RAM. When I use the 'Glance' utility to see what my memory utilization is, my memory usage shows up maxed out at 99%. I shut off all the known processes that I'm running on that box and the memory utilization is still at 78% (with Swap... (3 Replies)
Discussion started by: dehuang83
3 Replies

8. UNIX for Dummies Questions & Answers

HP-UX memory usage allocation

Hi all, I have a HP-UX Server with 4 gigabytes of physical RAM. When I use the 'Glance' utility to see what my memory utilization is, my memory usage shows up maxed out at 99%. I shut off all the known processes that I'm running on that box and the memory utilization is still at 78% (with Swap... (1 Reply)
Discussion started by: dehuang83
1 Replies

9. UNIX for Advanced & Expert Users

threads and memory allocation

Hello! First of all, forgive me for bad English. When I starts new thread (pthread_create), system allocates some memory for it (for example, for thread's stack). I wonder when does it deallocate this memory? The problem is that I have a program which sometimes creates new threads and sometimes... (3 Replies)
Discussion started by: prankster
3 Replies

10. UNIX for Dummies Questions & Answers

memory allocation

I would like to know how I could allocate some more memory to a process. Please note that I am not the root user. (1 Reply)
Discussion started by: sagar
1 Replies
Login or Register to Ask a Question