How to lock pages in memory using WinAPI? - c

I need to prevent application's memory pages from being swapped out of RAM on Windows. Is there a WinAPI function equivalent of POSIX mlockall() to achieve that?

Yes, VirtualLock(). There's a limit on how many pages you can lock, you can't hog RAM. Details are in the MSDN article.

I have to ask, why do you need to do this? If every app thought its pages were so important that they shouldn't be paged out ever, it would be a giant waste of memory.
If the pages are in use, they won't be sent to the pagefile, and if they're not in use, why keep them around? Trust in Mm, it was written by a very smart guy :)

Hi you can set the windows option of lock pages in memory. Usually this setting is mostly used by SQL Server, but works also for other applications. Check this site on msdn
enable addressing windows extensions (AWE) for your application. See this link on msdn

Related

is it safe to use EmptyWorkingSet like this?

I've been working on an app that sends keystrokes to mimic user actions. For this, I want to record my keystrokes. I looked around on the internet on how to go about such a task, and I found a program called Key Catcher. Because I'm worried of getting a malicious keylogger on my device I'm reading the source code first, and I found this line:
return, dllcall("psapi.dll\EmptyWorkingSet", "UInt", -1)
I didn't know what this command was and a google search gave internet forums warning not to use EmptWorkingSet yourself, but also examples of specific programs using this without problems. Could anyone explain how this should be used? or if this will give problems? or could someone give a better alternative?
PS: this command is used everytime a process finishes if that helps
The "EmptyWorkingSet" operation removes as many of the application's pages as possible from memory. It is often used mistakenly by people who think that having lots of free RAM is good.
It generally won't do very much harm. The pages can be loaded back into RAM fairly quickly if needed. But the only good it does is make the amount of free memory pages go up, which is actually slightly harmful.
It's basically neither here nor there. It's very bad to call it from a process that's performance critical because it will slow the process down. It's possibly useful to call it from a process that has accumulated a lot of cruft in RAM that doesn't need to be there. But the OS will already remove that stuff from RAM itself.

Silverlight and memory usage

My silverlight application is taking up 75 megs of memory. This seems high. How can I troubleshoot the application to see where the memory is being used. Is there any trick like running a low memory mode that would reduce my footprint for S light!
#Matt Bridges suggestion for a profiler is correct. I have also used ANTS. Other alternatives include Yourkit and using WinDbg SOS.
There isn't one answer to your question so it might get closed, however, there are memory leaks with inline Data Templates in your controls. Here is an example, but there are lots of pages when you google for it: http://www.devtoolshed.com/silverlight-memory-leak-datagrid-dataform-datatemplate-etc
One place to start is the ANTS Memory Profiler, which works with Silverlight.

C: memory pool library?

I need some fast, thread safe memory pooling library.
I've googled a lot, but fast solutions don't have thread safety, while others are really big.
Any suggestions?
Both nedmalloc and ptmalloc are C based thread caching memory managers, based around doug lea's malloc(the core of most linux allocators). They are both under good licences as well, unlike hoard, which requires payment for commercial use, last I looked. Googles tcmalloc also has C bindings iirc, and is built from the ground up as a thread caching allocator, as well as some built in heap and cpu profiling tools, it is however build for massive memory usage(the example they give is 300mb+ per thread), and as such many not work as well as expected for smaller scale apps
You're supposed to use one memory pool per thread.
The Apache Portable Runtime works well and shouldn't be all that big.
Have you tried Hoard?
See also these two articles from Intel.com

Why does an empty WPF Application consume 50MB?

I'm on Vista 64 and a blank 'WPF Application' template allocates 50MB when I press compile and run.
Surly this is way too much for an empty white box?!
Is there anything I can do to make my WPF applications less thirsty?
Jan
50 MB doesn't sound like that much for a modern application that makes heavy use of shared libraries.
Measuring memory usage is something of a black art. On some systems, apps which display the memory usage of a given app include in that total memory used by any shared libraries used by that app. But that memory is in fact being used by all apps using that library.
What is reporting the "50Mb" number to you? Task manager?
Generally speaking, I'd say that rather than worrying about unavoidable overhead for abstract use cases, it's better to develop your application and then analyze its memory usage in context to how it impacts performance.
Hope that helps.

Lightweight open-source shared file system over network

We have two web servers with load balancing. We need to share some files between those servers. These would be uploaded files, session files, various files that php applications create.
We don't want to use a heavyweight, no longer maintained or a commercial solution. We're looking for some lightweight open-source software that would work as shared file system. It should be really easy to set up, must be HA available, must be very fast. It should work with RedHat Linux.
We looked at such solutions like drbd with synchronous file sharing but we can't use them because it can't work on an underlying filesystem like ext3.
OCFS may be up to snuff by now; it's worth checkout out at least. It's in the mainline linux kernel tree, http://oss.oracle.com/projects/ocfs2/ has some info on it. I've set it up before, it was pretty easy to get going.
DRBD is good for syncing over a network (direct crossover connection if at all possible), but EXT3 is not designed to be aware of changes that occur underneath it, at the block device level. For that reason you need a filesystem designed for such purposes such as the Global File System (GFS). To the best of my knowledge Red Hat has support for GFS.
The DRBD manual will give you an overview of how to use GFS with DRBD.
http://www.drbd.org/users-guide/ch-gfs.html
Don't take this as a final answer - I have not researched or used a multi-master system before, but at least this might give you something to go on.
Ideally, you would only sync the part of the data that's shared between the webservers.

Resources