Question

Memory allocation for a large list - WITHOUT swap - how to figure out what I can allocate?

Asked by: bstrauss3

I'm looking for a way to determine how much memory is available to a process before it begins to use swap.  I want to allocate only as many table/linked list entries as I can without utilizing swap space.

I realize this is an odd request.

The reason behind it is that the program has two modes of behavior.

   In normal mode, obviously the VM subsystem does it's thing and the 10% of entries which are being referenced frequently are the ones that remain in the working set.

  But then, say, every 5 minutes, the process needs to walk the entire structure (it's a top-n and purge idle process in fact).  So if entries are swapped out, this causes a page-swap storm as each page is brought in for the walk, only to cause another page to be swapped out.  By the end of the walk, the 10% I really need are probably gone and so they have to be paged back in.

Then in 10-20 seconds things settle down ... until the next walk.

The 10% isn't always the same 10% - so I can't set up a multi-level structure.  It's a locality of reference thing - if I reference entry 1002 now, I'll probably reference it again soon.  If I haven't referenced entry 1001 for a while, I probably won't.  That's why the VM does a pretty ok job until the walk starts... But there is enough variablity that you can't figure out if something belongs in L1 or L2 or L3.  And there is NOTHING identifiable about an individual entry - the cause of active/inactive is external to my process.

Other complicating facts:

(0) Each 'entry' is actually composed of multiple malloc() items, so there isn't a fixed size.  All are relatively small and so come out of sbrk.  I can adjust thresholds to make some come out of mmap but why bother?  Once I allocate enough, some malloc() implementations will mmap another large region for smaller suballocations anyway.  I can not control the malloc() on the system - it's probably the glibc default, but it doesn't have to be.


(1) The process is soft real time.

This means the walk needs to finish in a few seconds so that other processes can run on time.


(2) There may be other processes running and the system is probably configured with swap space.  

What this means is that the basic memory size information the system provides is NOT equivalent to how much my process can allocate.


(3) Other processes could suddenly change their memory usage, forcing my table/linked list entries to be paged out.

Yes, I know that this whole question assumes that the rest of the system is pretty stable.  That's a valid assumption - we recommend that only our process runs on the host, for precisely this reason.  But even if other processes are running, they tend to settle down into their own vm size and resident/working set.  So after the system is up, if I could allocate 100MB of memory w/o starting to page, then I probably can always allocate 100MB of memory.

I'm willing to live with this - I can retune this size every hour or so.  A little swapping won't kill me, the problem is that swapping EVERYTHING in makes the walk take so long that other near-real-time processes don't run.


(4) It would be nice if this worked across Unixes, but I can live with a Linux only solution.


(5) I'm well aware of vmstat, free, etc. and their associated /proc files.  And mallinfo() and getrlimit() etc.

But these don't tell you how much I can increase my RSS (working set) before starting to swap, because of Linux's grab memory buffering strategy.  And re-read #2, above...

Here for example is the output from free (/proc/meminfo), with the process having a small number of entries and pretty much idle:

$ free
                     total        used         free     shared    buffers     cached
Mem:        839756     216580     623176          0      32020      79520
-/+ buffers/cache:     105040     734716
Swap:      1012084          0    1012084

$ cat /proc/meminfo
MemTotal:       839756 kB
MemFree:        619400 kB
Buffers:         32432 kB
Cached:          81708 kB
SwapCached:          0 kB
Active:         116016 kB
Inactive:        67136 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:       839756 kB
LowFree:        619400 kB
SwapTotal:     1012084 kB
SwapFree:      1012084 kB
Dirty:               8 kB
Writeback:           0 kB
Mapped:          76600 kB
Slab:            30968 kB
CommitLimit:   1431960 kB
Committed_AS:   304720 kB
PageTables:       1152 kB
VmallocTotal:   180216 kB
VmallocUsed:      3600 kB
VmallocChunk:   174580 kB
HugePages_Total:     0
HugePages_Free:      0
Hugepagesize:     2048 kB

$ vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
 1  0      0 619272  32616  82304    0    0    13    15  505    16 27  0 72  1


So it looks like I could allocate 600 something MB before swapping.  Can I really? No!

(6) Don't bother suggesting a caching strategy.  Because of the realities of the data it won't work (I can not tell which is an active entry - all I could do is migration - and the virtual memory manager does a far, far better job and does it automatically).

(7) Right now, what I have is to check every hour or so and figure out if the system-wide amount of swap space has grown.  If so, I set the limit to about 95% of what I am presently using.  That actually works OK, but I would prefer a less empirical solution...

-----Burton

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2005-05-16 at 07:52:19ID21425579
Tags

mallinfo

,

swap

Topic

Unix Systems Programming

Participating Experts
3
Points
500
Comments
14

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Currupt swap file !
    I am experiencing the problem with a VM .. when i tried to power off the VM its giving the error.. the attempted operation cannot be performed in the current state (powered On) i checked the link below : http://www.hoodoo.co.nz/?m=200704 its saying problem is with swap f...
  2. Linux server swap memory problem
    I have a problem with my webserver. Basically as you will see my drive statistics displayed below, as well as the swap. Now I'm having a major problem with web traffic spikes a certain time of the day and the server goes into oblivion and Tomcat and Appache must be rebooted. ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: NovaDenizenPosted on 2005-05-16 at 08:57:41ID: 14011393

Have you looked into mlock()?  That will enable you to lock a range of your memory into ram, preventing it from swapping.  You need to be root to call it, though.


 

by: TalmashPosted on 2005-05-16 at 08:57:51ID: 14011395

 

by: bstrauss3Posted on 2005-05-16 at 11:16:22ID: 14012708

NovaDenizen:

mlock() in an interesting idea.  In order for the call to succeed, I have to have allocated the memory via mmap.   And it has to happen before I shed root privs.  Both of those I can finesse around.  So I would have to start by mmap() and mlock() until I no longer get MNOMEM.  Of course, this would pretty much kill any subsequent process that tried to run on the box.  And I wouldn't be able to alter my usage.  I'll have to think about that for a while...

Interesting idea, but not really an answer to the ? "how much can I allocate".   Let's see what other answers pop up.

-----Burton

 

by: bstrauss3Posted on 2005-05-16 at 11:20:15ID: 14012747

Talmash:

No.  All top does is to echo the values from /proc.   Please read my (5) in the question.

-----Burton

 

by: NovaDenizenPosted on 2005-05-16 at 12:50:03ID: 14013504

I'm not 100% familiar with mlock(), but I don't think mmap is a prerequisite for calling it.  At any rate, you would have to implement your own buffer pool and cache within the mlock()ed area.  

You could also do something like this.  It mlock()s memory gradually up to a limit.  Call it every time you malloc a buffer.  You have to hold on to root privileges, though:
#define MLOCK_LIMIT_KB 512
#define PAGE_ADDR(ptr) ((ptr) & 0xfffff000)
void *extend_mlock(void *ptr) {
    static unsigned long long last_mlock = 0;  // is this right?  not sure what the minimum should be
    unsigned long long ptr_l = (unsigned long long) ptr;
    if (reslong > last_mlock && reslong < (MLOCK_LIMIT_KB << 10)) {
         mlock(last_mlock, PAGE_ADDR(ptr_l + 4095) - last_mlock);
         last_mlock = PAGE_ADDR(ptr_l + 4095);
    }
}

Another alternative is mlockall().  Call mlockall(MCL_FUTURE) and your entire process will stay in ram.  You can drop root privileges right after the call.  You have to be responsible and make sure your process doesn't use up all the ram, though.

 

by: ahoffmannPosted on 2005-05-17 at 03:02:36ID: 14017112

silly question: how can you enshure that your process (or parts of it) are not swapped out?
If you cannot enshure that ('cause you're not root) why do you care about your data/tables then?

For user space processes you may use mmap, but keep in mind that this may cause disk IO.
Probably shared memory can solve your problem for the data part of your process (sorry no experiance with this)

 

by: bstrauss3Posted on 2005-05-17 at 05:19:24ID: 14017773

Silly answer: Read my original question ... it's a performance issue.

Better answer:  If I walk the structure and it's in memory it completes a whale of a lot faster than if it's so big much of it gets swapped out.

So, I'm trying to offer a trade-off - store less data but have better performance.  Then the user makes the decision - if you need all the data, well, you don't enable the ceiling option.  If performance is more important - set the ceiling.


Remember how an LRU VM works - infrequently used pages get aged out to slower storage.  With my reference pattern it's almost guaranteed that as I bring in rarely used entries for the walk, the frequently used ones I really need will be swapped out, because at that instant in time they are the least recently referenced ones.  Say my total size is 1G and my working set 100M - I'll end up causing 1.1GB of swapping...

After the walk ends, the frequently used entries are brought back in for the normal process.  I can't cache because *I* can't identify the frequently used ones - it's external.  The only characteristic of frequently used entries are that they are frequently used.   So while you would normally setup a manual caching process, why bother - the Virtual Memory Manager already does this.

It works great for the 59s of each minute which are the "normal" process, but that 1s of walking all entries is a killer...

-----Burton

 

by: ahoffmannPosted on 2005-05-17 at 07:58:37ID: 14019218

>  Silly answer: Read my original question ... it's a performance issue.
> .. allocate ..  as I can without utilizing swap space.

hmm, it might be a perfomance issue for you as programmer/user
But the OS (in particular the implementation of the kernel's task switcher and memory manager) desides which process gets swapped out, the programmer cannot prevend this (exceptions see comments above).

Said this, my question again: how can you enshure that your process (or parts of it) are not swapped out?
If you solved this, you can simply use any algorithm storing your most important data on the stack.
Or do I miss something?

 

by: bstrauss3Posted on 2005-05-17 at 08:41:18ID: 14019651

"how can you enshure [sic] that your process (or parts of it) are not swapped out?"  That's basically what I'm asking, but as "how much malloc() can I do", vs. tje historical question, "What is my resident set size"?

More broadly, yes you are missing something - which I said in my original question - there is no way to tell what's important.  

You can tell what WAS important, but that's only past information.  If I use that information, I'm as likely to make a bad choice as a good one.  And what was good at t-30 may not be good now at t  (it was probably a good choice at t-31 and t-29, but ... )

Think of it as Brownian motion (drunk's walk) - eventually you get from point a to point b, but a priori I can't say where you are going to be.  At any point in time you are likely to be near where you were in the recent past, but not always (one good lurch moves you a long way as it were).

That's exactly the locality of reference that a Least Recently Used (LRU) algorithm depends upon, so in general the OS' VMM does a good job of keeping important data in memory and swapping out the less important stuff.

Then, every minute or five, I have a process which checks EVERY entry.  This causes them all to be swapped in  AND distorts the LRU data.  After the process completes, we go back to the 'steady state' and the VMM is again doing a good job.

But, if I offer the opportunity to only store enough data so that I don't swap, this periodic process runs much faster - fast enough so that it completes in the available window and I don't lose data.  It's both quantity  (limit-x < x) of data to process AND the per item time, since I don't have the 1000s of cycles to swap in a page.

If I can figure out how much data I can store within the available working set, I can offer the user the ability to deploy his/her knowledge of what's important to keep the number of entries within limit-x.

While typically other processes distort any such measurement of resident set, available space, etc, in this narrow case, I can insist that our process be the only one on the box (except for the OS stuff itself).  So it's valid to say that if I can allocate xMB w/o swap, that's what I want to use.

-----Burton

 

by: ahoffmannPosted on 2005-05-17 at 09:13:04ID: 14019981

> .. store enough data so that I don't swap ..
If "I" here means *you* (as programmer or the program itself), then you make the wropng descission/assumtion:
Again: the OS/kermel desides when and how to swap in/out, not the process!

> I can insist that our process be the only one on the box
ok, if no kernel around this process, then that's ok. But ...

> .. (except for the OS stuff itself).
that's the culprit.
Even a system doing nothing except "idleing around" and switching to your process task ometinmes may decide to swap when the task is idle for xxx seconds/minutes/hours/days/etc.. That's what my "silly question" is about: how can you enshure that this will not happen?
I believe that your algorithm is correct, but you need to enshure that the process (or parts of it) cannot be swapped, and that's a root task (if even possible).

 

by: bstrauss3Posted on 2005-05-17 at 10:11:38ID: 14020565

AH: I know all these things ... 20+ years of coding.  On OSes you may never of heard of unless you're another grey beard.  Back before Jolt cola was invented and when real coders lived within 64K w/ overlays... And coded MFT mods for the SHARE tapes (which I've done).  Trust me, I really do understand how a VM works.

My point - assumption - is that a unix box running no applications reaches a steady state.  Sure there's lots of stuff that perturbs it (cron, logrotate, et al), but over say 5m or 10m, it is still a pretty steady state.  Definition of perturbation is that the system returns to that state pretty reliabily.  And, sure, if something changes that steady state, I'm in trouble.

But assuming that steady state, if I can increase my resident set from 48MB to say 128MB w/o starting the system swapping, that's much memory is pretty much going to be available to me at all times.

It's not a simple question - that's why I came here after a couple of weeks of research.  I know that...

BUT THAT IS THE QUESTION I ASKED...

Please try to answer my question... "I'm looking for a way to determine how much memory is available to a process before it [the system] begins to use swap."

TIA!

-----Burton

 

by: ahoffmannPosted on 2005-05-17 at 10:41:02ID: 14020826

> Trust me,
I'll do, silly question answered with this ;-)

> ..  looking for a way to determine how much memory is available to a process before it [the system] begins to use swap
hmm, think that this depends on your OS, could you please tell us your os (uname -srv)

getrlimit() gives you the amount of virtual space for the process, with rusage() you get the data, stack and heap size of your process. On some OS you may use /proc/<PID> to get that information too (/proc/meminfo  /proc/stat  /proc/<PID>/stat ).
Now you need your OS' function to get the current status of your virtual memory, a starting point here should be vmstat.

 

by: bstrauss3Posted on 2005-05-17 at 12:00:40ID: 14021515

rusage() returns the ulimit setting, e.g.

$ ulimit -m
unlimited

This is (1) advisory (2) valued only if ulimit set it and (3) not related to the actual RSS the kernel will permit.

All this does is limit what you can request be locked via madvise().

Same kinds of things for getrlimit().

As I've said, I've read vmstat et al - all they do it sscanf() from the /proc files.  

Problem with the /proc files is that they report what has happened.  Not capabilities.  With /proc, the best you can do is see to compare values over time.

at t-30 I was using 30MB and there was 0 swap in use.  At t I'm using 45MB and there is 10MB of swap in use.  So I can probably use about 35MB w/o paging.  That's what I had coded before asking the question...


BUZZ... not quite, but thanks for playing.

-----Burton

 

by: bstrauss3Posted on 2005-05-24 at 10:04:44ID: 14070288

Well this seems to have petered out...  I'm disappointed, but not surprised, as a lot of research didn't find a simple answer myself.

Anyway, gang, thanks for playing.

Both NovaDenizen and ahoffmann had some interesting thoughts.  Upon reflection mlock() probably would work - but I would have to allocate before I shed root and manage the memory using my own malloc() clone.

So I'll be splitting points 60/40

Thanks!

-----Burton

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...