| View previous topic :: View next topic |
| Author |
Message |
LiquidCode Moderator Team
Joined: 05 Dec 2000 Posts: 1752 Location: Space and Time
|
Posted: Mon Aug 10, 2009 4:47 pm Post subject: EmptyWorkingSet - ReduceMemory |
|
|
I have the following code to reduce the memory of my VDS apps and it works great. I wanted to know how I can make it so I can reduce the memory of a non VDS app, like IE?
| Code: |
:ReduceMemory
loadlib "psapi.dll"
%t = @lib("psapi.dll","EmptyWorkingSet",INT:,-1)
freelib "psapi.dll"
exit
|
I found this online but couldn't figure out how to convert it into VDS.
| Code: |
Func _ReduceMemory($i_PID = -1)
$memory_counter = 1
If $i_PID <> - 1 Then
Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
Else
Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
EndIf
Return $ai_Return[0]
EndFunc
|
Looks like it can reduce not only the memory of the parent application, but also one that the PID is passed to this function.
Thanks for any help!! _________________ Chris
Http://theblindhouse.com |
|
| Back to top |
|
 |
vdsalchemist Admin Team

Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Tue Aug 11, 2009 1:00 am Post subject: |
|
|
Chris,
Yes this will empty the working set for another process however I must caution everyone on using this. This is not something you should do especially with code you cannot see and code that use multiple threads. _________________ Home of
Give VDS a new purpose!
 |
|
| Back to top |
|
 |
LiquidCode Moderator Team
Joined: 05 Dec 2000 Posts: 1752 Location: Space and Time
|
Posted: Tue Aug 11, 2009 1:26 am Post subject: |
|
|
Um...Maybe you can PM me the info? I'll PM you with more info. _________________ Chris
Http://theblindhouse.com |
|
| Back to top |
|
 |
vdsalchemist Admin Team

Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Tue Aug 11, 2009 3:18 pm Post subject: |
|
|
Here is the translation for you...
| Code: |
#--------------------------------------------------------------------------------#
# #
# Description: Demo to show how to use the following API functions #
# OpenProcess, EmptyWorkingSet, and CloseProcess to reduce the #
# working memory of a Exe #
# #
# Author: Johnny Kinsey #
# Email: jkinsey @ dragonsphere . net #
# #
#--------------------------------------------------------------------------------#
#DEFINE COMMAND,REDUCEMEMORY
# create a list to hold the names and process ids of all
# running programs
%%ProcessId_Lst = @New(LIST)
List TaskList,%%ProcessId_Lst,NI
# locate the vds.exe program
%A = @match(%%ProcessId_Lst,vds.exe)
If %A
# extract the process id
PARSE "%%name;%%id",@Item(%%ProcessId_Lst)
# reduce the memory footprint of the exe.
ReduceMemory %%id
Else
Info could not find vds.exe
End
stop
:ReduceMemory
# This subroutine will reduce the memory footprint
# of the current exe or of the specified process ID
loadlib psapi.dll
loadlib kernel32.dll
If %1
# Get a handle to the specified process
%h = @lib(kernel32,OpenProcess,INT:,INT:$1F0FFF,INT:0,INT:%1)
If @unequal(%h,0)
# reduce its current memory
%t = @lib(psapi,EmptyWorkingSet,INT:,INT:%h)
# close the handle
%h = @lib(kernel32,CloseHandle,BOOL:,INT:%h)
End
Else
# reduce the memory usage of the current process
%t = @lib(psapi,EmptyWorkingSet,INT:,INT:-1)
End
freelib psapi
freelib kernel32
exit
|
_________________ Home of
Give VDS a new purpose!
 |
|
| Back to top |
|
 |
LiquidCode Moderator Team
Joined: 05 Dec 2000 Posts: 1752 Location: Space and Time
|
|
| Back to top |
|
 |
jwfv Valued Contributor

Joined: 19 Mar 2002 Posts: 422 Location: Beaufort, SC
|
Posted: Wed Aug 12, 2009 3:55 pm Post subject: |
|
|
Chris:
Where do you use the code in your VDS app? (Not the code for external programs.)
I am thinking about putting it in some of my programs. I have lots of users that are running XP on machines with very low RAM. Any drawbacks to it? _________________ Joe Floyd |
|
| Back to top |
|
 |
LiquidCode Moderator Team
Joined: 05 Dec 2000 Posts: 1752 Location: Space and Time
|
Posted: Wed Aug 12, 2009 4:35 pm Post subject: |
|
|
It depends on what type of program I am using it in. Sometimes I put it in a timer to reduce the memory every 10 - 15 min and sometime I just put it after a function that uses a lot of memory like lists with a lot of info or right after I create a dialog. I have not had any problems reducing the memory. _________________ Chris
Http://theblindhouse.com |
|
| Back to top |
|
 |
DaveR Valued Contributor


Joined: 03 Sep 2005 Posts: 413 Location: Australia
|
Posted: Fri Aug 14, 2009 12:44 pm Post subject: |
|
|
| LiquidCode wrote: | | It depends on what type of program I am using it in. |
I only use it in my tray applications, and I put it in the event loop. It reduces their memory use from 4.4MB to around 300KB.
| LiquidCode wrote: | | I have not had any problems reducing the memory. |
It's never caused a problem for me either. _________________ cheers
Dave |
|
| Back to top |
|
 |
jwfv Valued Contributor

Joined: 19 Mar 2002 Posts: 422 Location: Beaufort, SC
|
Posted: Fri Aug 14, 2009 1:41 pm Post subject: |
|
|
I appreciate the info - _________________ Joe Floyd |
|
| Back to top |
|
 |
DaveR Valued Contributor


Joined: 03 Sep 2005 Posts: 413 Location: Australia
|
Posted: Sat Aug 15, 2009 5:22 am Post subject: |
|
|
| LiquidCode wrote: | | and sometime I just put it after a function that uses a lot of memory like lists with a lot of info or right after I create a dialog. |
Interesting. I just added the 'reduce memory' code (to one of my 'non-tray' applications) immediately after the main 'dialog show' and at the end of a user function that heavily uses lists. The results were as follows:
Original application code:
4,400 KB (4,400 KB peak)
Original application code plus reduce memory code added:
2000 KB (8,200 KB peak !!!)
The larger peak memory usage was a concern. So I ran both a few more times but now they both always peaked at 8,200 KB.
Original application code:
8,200 KB (8,200 KB peak)
8,200 KB (8,200 KB peak)
8,200 KB (8,200 KB peak)
Original application code plus reduce memory code added:
2,000 KB (8,200 KB peak)
2,000 KB (8,200 KB peak)
2,000 KB (8,200 KB peak)
So it seems that the small 4,400 KB peak was just a one off, and the 8,200 KB peak memory usage is not because of the 'reduce memory' code. _________________ cheers
Dave |
|
| Back to top |
|
 |
vdsalchemist Admin Team

Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Sun Aug 16, 2009 1:29 am Post subject: |
|
|
The only memory usage that this function affects is the physical memory. The Working set refers to the heap memory being used by the application. If you use this function too often you could impact the performance of the application because if it is using this memory for a cache or for buffers it will have to reallocate after you remove it. While you are keeping the application from being a memory hog you could actually be causing it to use the processor more and you are causing it to use the Swap file more. This is only a means of signalling the operating system that the executable is idle and that memory used for its working set can be freed for other processes. The only real time that this could be effective is for an application that is mostly idle such as a service or a update monitor or the likes. I personally would never use it since I value performance over memory usage. It is actually better for the performance of your application to use as much memory as the os can afford to give it. If it is a well behaved app which VDS apps usually are since it has a built in garbage collector it will release the memory as soon as it thinks your done with it. I am not aware of any memory leaks in VDS 6 at this time. A better way to use this function is described in this Dr. Dobb's article http://www.ddj.com/windows/184416804 This article describes a way to control how big the Working set can grow. If you know how much memory your application needs the set it to that level then you don't have to worry about it going beyond that. _________________ Home of
Give VDS a new purpose!
 |
|
| Back to top |
|
 |
SnarlingSheep Professional Member


Joined: 13 Mar 2001 Posts: 759 Location: Michigan
|
Posted: Sun Aug 16, 2009 1:30 pm Post subject: |
|
|
I completely agree, and this should never be used in a timer.
If you are really worried about sharing memory with other apps, use it when your program is minimized. Even then it will impact performance when your program is restored. _________________ -Sheep
My pockets hurt... |
|
| Back to top |
|
 |
cnodnarb Professional Member


Joined: 11 Sep 2002 Posts: 765 Location: Eastman, GA
|
Posted: Mon Jul 05, 2010 5:05 pm Post subject: |
|
|
Kind of offtopic, this is about the original code.
| Code: | :ReduceMemory
loadlib "psapi.dll"
%t = @lib("psapi.dll","EmptyWorkingSet",INT:,-1)
freelib "psapi.dll"
exit |
It's been my experience this is most useful directly after all elements are added to a form and the controls are shown. Generally reduces the memory use of the program by half...and reduces the high peak of the program tremendously. |
|
| Back to top |
|
 |
Boo Valued Contributor


Joined: 31 Oct 2003 Posts: 599 Location: Gulf Breeze, Florida USA
|
Posted: Fri Mar 04, 2011 11:17 am Post subject: |
|
|
Hi All,
Regarding vdsalchemist's translation code above, will this work on 64-bit Windows, too? (Might be a dumb question, but...)
Thanks,
- Boo |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|