请问高手:用什么方法可以不让应用程序使用虚拟内存?

hdzjcsdn 2003-08-18 11:18:30
目前,windows下的应用程序都会使用系统虚拟内存,在编程过程中,有没有办法在应该程序中编写代码使程序运行过程中不使用虚拟内存.Windows的虚拟内存管理实在是太烂了,总是释放不干净!
万望高手指点迷津!
...全文
427 73 打赏 收藏 转发到动态 举报
写回复
用AI写文章
73 条回复
切换为时间正序
请发表友善的回复…
发表回复
xxxno 2004-01-31
  • 打赏
  • 举报
回复
错了,我是一个三角的!
呵呵
xxxno 2004-01-31
  • 打赏
  • 举报
回复
halfdream(哈欠),不要一棍子打死一片,我就是一颗星的,不喜欢回答就不要来回答了
现在是在讨论问题,你插一句进来是什么意思? 你很厉害?
给出代码就服你!
ahjoe 2004-01-28
  • 打赏
  • 举报
回复
在Windows系统属性中禁用虚拟内存。
halfdream 2004-01-26
  • 打赏
  • 举报
回复
记得有个CSDN朋友说过这样的话,他最不喜欢回答等级为一个三角的人提的问题..
我很疑忌,后来才慢慢明白,大多数因为问题而初来CSDN的人,除了什么都不懂的人之外,
常会怀着一些情绪,常会怀着一些挑战的潜意识,先入为主,往往不会好好的讨论问题.
Eastunfail 2004-01-16
  • 打赏
  • 举报
回复

////////////////////////////////////////////////
// Memory Manager
////////////////////////////////////////////////

function TMemoryManager.GetInstanceSize:Cardinal;
begin
Result:=self.FList.InstanceSize;
end;
function TMemoryManager.FGetSize(addr:Pointer):Integer;
var i:integer;
begin
i:=Find(Addr);
Result:=-1;
if i=-1 then exit;
Result:=PMemBlock(FList[i]).Size ;
end;
procedure TMemoryManager.DisposeLocal;
var i:integer;
begin
for i:=0 to FList.Count -1 do
MemoryCollector.Unlog(PMemBlock(FList[i])^.Ptr);
FList.Clear ;
end;
constructor TMemoryManager.Create;
begin
FList:=TList.Create ;
MemoryCollector.FConnections.Add(Self);
MemoryCollector.FInstances.Add(self);
end;
destructor TMemoryManager.Free;
begin
DisposeLocal;
MemoryCollector.RemoveInstance(self);
MemoryCollector.FConnections.Remove(self);
FList.Free ;
end;
function TMemoryManager.FAllocateMemory(dwSize:Cardinal):Pointer;
var tmp:PMemBlock;
begin
lxcAssertThrow(dwSize>0,EErrorSize,LS_E_SIZE);
Result:=Allocate(dwSize);
//log globally
MemoryCollector.Log(Result,dwSize,DisposeCallback);
//log locally
GetMem(tmp,sizeof(TMemBlock));
tmp^.ReleaseFunc :=nil;
tmp^.Ptr :=Result;
tmp^.Size :=dwSize;
FList.Add(tmp);
end;
function TMemoryManager.Allocate(dwSize:Cardinal):Pointer;
begin
GetMem(Result,dwSize);
end;
function TMemoryManager.Find(Ptr:Pointer):Integer;
var i:integer;
begin
Result:=-1;
for i:=0 to FList.Count -1 do
if PMemBlock(FList[i]).Ptr=Ptr then
begin
Result:=i;
end;
end;
procedure TMemoryManager.FReleaseMemory(dwSize:Cardinal;Ptr:Pointer);
var i:integer;
begin
lxcAssertThrow(Assigned(Ptr),ENullMem,LS_E_NULL_RELEASE);

i:=Find(Ptr);
lxcAssertThrow(i>-1,EBadMem,LS_E_BAD_MEMORY);
//unlog it globally
MemoryCollector.Unlog(Ptr);
//Unlog it locally
FreeMem(FList[i]);
FList.Delete(i);
end;
procedure TMemoryManager.Dispose(dwSize:Cardinal;Ptr:Pointer);
begin
try
FreeMem(Ptr);
except
end;
end;
procedure TMemoryManager.DisposeCallback(dwSize:Cardinal;Ptr:Pointer);
begin
lxcAssertThrow(Assigned(Ptr),ENullMem,LS_E_NULL_RELEASE);
self.Dispose(dwSize,Ptr);
end;
function TMemoryManager.FGetBaseAddress(Addr:Pointer):Pointer;
var i:integer;lower,upper,addr2:cardinal;
begin
Result:=nil;
addr2:=Cardinal(addr);
for i:=0 to FList.Count -1 do
begin
lower:=Cardinal(PMemBlock(FList[i])^.Ptr) ;
upper:=lower+PMemBlock(FList[i])^.Size ;
if (addr2>=lower) and (addr2<upper) then
begin
Result:=PMemBlock(FList[i])^.Ptr;
Exit;
end;
end;
end;
/////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////
function TExecMemManager.Allocate(dwSize:Cardinal):Pointer;
begin
Result:=VirtualAlloc(nil,dwSize,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
end;
procedure TExecMemManager.Dispose(dwSize:Cardinal;Ptr:Pointer);
begin
VirtualFree(ptr,dwSize,MEM_RELEASE);
end;
/////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////
function TFixedMemManager.Allocate(dwSize:Cardinal):Pointer;
begin
Result:=Pointer(GlobalAlloc(GMEM_FIXED or GMEM_ZEROINIT,dwSize));
end;
procedure TFixedMemManager.Dispose(dwSize:Cardinal;Ptr:Pointer);
begin
lxcAssertThrow(Cardinal(GlobalFree(Ptr))=0,EReleaseFailure,LS_E_RELEASE_FAILURE);
end;
initialization
MemoryCollector:=TMemoryCollector.Create ;
finalization
MemoryCollector.Free ;
end.
Eastunfail 2004-01-16
  • 打赏
  • 举报
回复
////////////////////////////////////////////////
// Memory Collector
////////////////////////////////////////////////

function TMemoryCollector.AddInstance(AInstance:TObject):Integer;
begin
Result:=FInstances.Add(AInstance)
end;
procedure TMemoryCollector.DeleteInstance(AIndex:integer);
begin
FInstances.Delete(AIndex)
end;
procedure TMemoryCollector.RemoveInstance(AInstance:TObject);
begin
FInstances.Remove(AInstance);
end;
function TMemoryCollector.FGetInstances(AIndex:Integer):TObject;
begin
Result:=TObject(FInstances[AIndex]);
end;
function TMemoryCollector.FGetInstanceCount:Cardinal;
begin
Result:=FInstances.Count;
end;
function TMemoryCollector.GetInstanceSize:Cardinal;
begin
Result:=self.FList.InstanceSize +self.FConnections.InstanceSize +self.FInstances.InstanceSize;
end;
function TMemoryCollector.FGetInstanceSize:Cardinal;
var i:integer;_size:cardinal;
begin
_size:=0;
for i:=0 to self.FInstances.Count -1 do
_size:= _size+TDynamicCoderbase(FInstances[i]).GetInstanceSize+ Cardinal(TDynamicCoderbase(FInstances[i]).InstanceSize) ;
result:=_size;
end;
function TMemoryCollector.FGetConnections:Cardinal;
begin
Result:=FConnections.Count ;
end;
function TMemoryCollector.FGetSize(Ptr:Pointer):Integer;
var i:integer;
begin
Result:=-1;
i:=Find(Ptr);
if i=-1 then exit;
Result:=PMemBlock(FList[i]).Size;
end;
function TMemoryCollector.FGetCount:integer;
begin
Result:=FList.Count ;
end;
function TMemoryCollector.FGetSizes:Integer;
var i:integer;
begin
Result:=0;
for i:=0 to FList.Count -1 do
Inc(Result,PMemBlock(FList[i])^.Size);
end;
function TMemoryCollector.Find(Ptr:Pointer):integer;
var i:integer;
begin
Result:=-1;
for i:=0 to FList.Count -1 do
if PMemblock(FList[i])^.Ptr =Ptr then
begin
Result:=i;
exit;
end;
end;
procedure TMemoryCollector.Log(Ptr:Pointer;dwSize:Cardinal;ReleaseFunc:TReleaseFunc);
var MemBlock:PMemBlock;
begin
lxcAssertThrow(Assigned(Ptr),ENullMem,LS_E_NULL_LOG);
GetMem(MemBlock,sizeof(TMemBlock));
MemBlock^.ReleaseFunc :=ReleaseFunc;
MemBlock^.Size:=dwSize;
MemBlock^.Ptr :=Ptr;
FList.Add(MemBlock);
end;
procedure TMemoryCollector.Unlog(Ptr:Pointer);
var i:integer;tmp:PMemBlock;
begin
i:=Find(Ptr);
lxcAssertThrow(i>-1,EBadMem,LS_E_BAD_MEMORY);
tmp:=PMemBlock(FList[i]);
tmp^.ReleaseFunc(tmp.Size,tmp.Ptr);
FreeMem(tmp);
FList.Delete(i);
end;
procedure TMemoryCollector.Remove(Ptr:Pointer);
var i:integer;
begin
i:=Find(Ptr);
lxcAssertThrow(i>-1,EBadMem,LS_E_BAD_MEMORY);
FList.Delete(i);
end;
constructor TMemoryCollector.Create;
begin
FList:=TList.Create ;
FConnections:=TList.Create;
FInstances:=TList.Create ;
inherited;
end;
destructor TMemoryCollector.Free;
begin
DisposeAll;
FConnections.Free ;
FList.Free ;
FInstances.Free ;
end;
procedure TMemoryCollector.DisposeAll ;
var i:integer;tmp:PMemBlock;
begin
for i:=0 to FConnections.Count -1 do
TMemoryManager(FConnections[i]).DisposeLocal;
exit;

for i:=0 to FList.Count -1 do
begin
tmp:=PMemBlock(FList[i]);
tmp.ReleaseFunc(tmp^.Size,tmp^.Ptr);
FreeMem(tmp);
end;
FList.Clear ;
end;
Eastunfail 2004-01-16
  • 打赏
  • 举报
回复
我的DC库中有个内存管理的类,有个是申请固定线性地址,估计对你有用:
unit lxcMemoryManager;

interface
uses Windows, Messages, SysUtils, Variants, Classes,
lxcGlobal, lxcExceptions,lxcStrings,lxcDynamicCoder;
type
TReleaseFunc=procedure(dwSize:Cardinal;Ptr:Pointer) of object;
PMemBlock=^TMemBlock;
TMemBlock=record
ReleaseFunc:TReleaseFunc;
Ptr:Pointer;
Size:Cardinal;
end;
TMemoryManager=class;
TMemoryCollector=class(TDynamicCoderBase)
private
FList:TList;
FConnections:TList;
FInstances:TList;
function Find(Ptr:Pointer):integer;
function FGetSize(Ptr:Pointer):Integer;
function FGetCount:integer;
function FGetSizes:Integer;
function FGetConnections:Cardinal;
function FGetInstances(AIndex:Integer):TObject;
function FGetInstanceCount:Cardinal;
function FGetInstanceSize:Cardinal;
public
property Size[Ptr:Pointer]:Integer read FGetSize;
property Count:Integer read FGetCount;
property Connections:Cardinal read FGetConnections;
property Sizes:Integer read FGetSizes;
property Instances[AIndex:integer]:TObject read FGetInstances;
property InstanceCount:Cardinal read FGetInstanceCount;
property InstanceSizes:Cardinal read FGetInstanceSize;

function GetInstanceSize:Cardinal;override;
function AddInstance(AInstance:TObject):Integer;
procedure DeleteInstance(AIndex:integer);
procedure RemoveInstance(AInstance:TObject);




procedure Log(Ptr:Pointer;dwSize:Cardinal;ReleaseFunc:TReleaseFunc);
procedure Unlog(Ptr:Pointer);
procedure Remove(Ptr:Pointer);
procedure DisposeAll;
constructor Create;
destructor Free;
end;
TMemoryManager=class(TDynamicCoderBase)
private
FList:TList;
function FAllocateMemory(dwSize:Cardinal):Pointer;
procedure FReleaseMemory(dwSize:Cardinal;Ptr:Pointer);
procedure DisposeCallback(dwSize:Cardinal;Ptr:Pointer);
function Allocate(dwSize:Cardinal):Pointer;virtual;
procedure Dispose(dwSize:Cardinal;Ptr:Pointer);virtual;
function FGetSize(addr:Pointer):Integer;
function Find(Ptr:Pointer):Integer;
function FGetBaseAddress(Addr:Pointer):Pointer;
public
constructor Create;
destructor Free;
function GetInstanceSize:Cardinal;override;
procedure DisposeLocal;
property Memory[dwSize:Cardinal]:Pointer read FAllocateMemory write FReleaseMemory;default;
property Size[addr:Pointer]:Integer read FGetSize;
property BaseAddress[addr:Pointer]:Pointer read FGetBaseAddress;
end;
TExecMemManager=class(TMemoryManager)
function Allocate(dwSize:Cardinal):Pointer;override;
procedure Dispose(dwSize:Cardinal;Ptr:Pointer);override;
end;
TFixedMemManager=class(TMemoryManager)
function Allocate(dwSize:Cardinal):Pointer;override;
procedure Dispose(dwSize:Cardinal;Ptr:Pointer);override;
end;

var MemoryCollector:TMemoryCollector;
implementation
月光 2004-01-15
  • 打赏
  • 举报
回复
sz-oasis.8u8.com/new.htm
hdzjcsdn 2004-01-12
  • 打赏
  • 举报
回复
qu
hdzjcsdn 2003-12-15
  • 打赏
  • 举报
回复
伤心!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
失望!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
伤心!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
失望!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
伤心!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
失望!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
伤心!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
失望!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
伤心!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
失望!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
伤心!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
失望!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
伤心!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
失望!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!



hdzjcsdn 2003-11-13
  • 打赏
  • 举报
回复
TO:Xleep(笑尽天下事)
可是我怎么知道应该锁哪一页呢
Xleep 2003-11-13
  • 打赏
  • 举报
回复
看MSDN,
BOOL VirtualLock(

LPVOID lpAddress, // address of first byte of range to lock
DWORD dwSize // number of bytes in range to lock
);
lpAddress是你要锁定的虚拟内存。
Xleep 2003-11-11
  • 打赏
  • 举报
回复
不是有位高手说过嘛?
virtuallock

这个是把你的虚拟地址的某个页面锁进物理内存的API.
耙子 2003-11-11
  • 打赏
  • 举报
回复
bt
hdzjcsdn 2003-11-11
  • 打赏
  • 举报
回复
到底有没有高手呀??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
风中的猴尾巴 2003-11-04
  • 打赏
  • 举报
回复
关注,学习,收藏......
hdzjcsdn 2003-11-04
  • 打赏
  • 举报
回复
郁闷死了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
hdzjcsdn 2003-10-19
  • 打赏
  • 举报
回复
急!!!!!!!!!!!!!!!!!!!!!!!!!
idilent 2003-10-11
  • 打赏
  • 举报
回复
没有看到你的帖子,今天给你发
hdzjcsdn 2003-10-11
  • 打赏
  • 举报
回复
呼吁高手出招!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
加载更多回复(53)

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧