Solved

Memory problems using Exec command.

Posted on 2001-06-07
30
304 Views
Last Modified: 2010-04-16
Hi,

I'm creating a bootdisk (DOS) and I wrote a Graphical interface in Pascal. I use a lot of external tools wich are loaded with Exec command. But these programs don't release their memory (I think?) Is there a way to clear the memory of the programs executed so there's room for other programs?
The problem is my network-drivers need to be loaded (at the end) but the net.exe tells me (Error 8 not enough memory)
I already lowered the heapsize to 60,000.
I'm not familiar with assembler and I have no clue how to overcome my problem. Can someone help me? I only have 75 points to give away. (A friend of mine recommended this site) and I just signed up. I will give away more points (hopefully) in the future.

Function ShellCommand(Command : String) : Integer;
Var
   Redirect        : Boolean;
   OutputTo        : String;
Begin
   SwapVectors;
   Redirect := (Pos('>', Command) <> 0);
   If Redirect Then
      OutputTo := ''
   Else
      OutputTo := ' >nul';
   Exec(GetEnv('COMSPEC'), '/C ' + Command + OutputTo);
   ShellCommand := DosError;
   SwapVectors;
End;
0
Comment
Question by:Rcm
  • 12
  • 7
  • 6
  • +2
30 Comments
 

Author Comment

by:Rcm
ID: 6163916
e.g. I run a tool to find the serial# of a Compaq-laptop. This program returns a number (wich I dump on disk and read with Pascal). But afterwards I cannot load "net.exe use ..." anymore (Error 8 not enough memory) and when I don't use the first tool, "net.exe use ..." works fine
0
 
LVL 47

Expert Comment

by:dbrunton
ID: 6165273
The programs concerned should be reducing memory once they close.

However there are two other alternatives to using EXEC.  Both reduce the amount of memory used concerned compared to EXEC.

One is

EXECSWAP by Kim Kokkonen

and the other

SPWNO413 by Ralf Brown  (that's an O and not a zero)

Do a search on Google for them and try those and see how more memory you get free.

Use the MEM command and see how much memory is left before you run your tools and after you run your tools.  It sounds like possibly a TSR has been left in mem or files have not been closed properly.
0
 
LVL 6

Expert Comment

by:My name is Mud
ID: 6166596
SWAPVECTORS is use just before and after using EXEC...
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6168145
Lower the heap size even more.
0
 

Author Comment

by:Rcm
ID: 6171334
To dbrunton:
I downloaded the SPWNO413 by Ralf Brown and it looks like it's what I need. I can't test it right away because I only have 1 week to make my project working. (Next week I get my grades on it) and 2 weeks later I'll have to defend it before a jury. After that I'll test it and I'll grant you the points (hopefully I'll be able to add 25 more points to it ...)
I didn't expect to get a response this quickly. Thanks
0
 

Author Comment

by:Rcm
ID: 6171344
To AvonWyss:
Can't lower heap forever, can I? I'm already had to "pull some tricks" to get it to work under 80,000 heap (I work graphically with a lot of variables and some pictures).
Currently I'm using 56,000 (or something) and I prefer not to have a runerror 203 when I do my presentation. But thanks anyways for the comment
0
 

Author Comment

by:Rcm
ID: 6171352
To WhatBoy:
Hi, Thank you for this comment. I changed my code, but I'm somewhat sceptical this will solve the problem. It's possible this will solve some problems temporarily but the amount of memory these external tools use will differ because it depends on the user input (he's able to run even more tools from within my program ... so swapping to disk or to ems seems the solution I want to go for).
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6171358
RCM, you may consider two techiques to lower the heap even more:

* Allocate memory directly via calls to the operating system. Note that you cannot do this using New/Dispose, but you can use pointers to such blocks allocated yourself. When you free such a block, it becomes available for the OS and thus also child processes. With careful design, you could even swap such memory to a disk file when starting a program.

* Allocate XMS or EMS memory. This also requires some manual allocation/deallocation, but you do not have to clear this memory when starting an external program.

Also note that the data segment and stack are not larger than absolutely necessary; if you run short on stack space in some situations, you can also change the stack to another location temporarily (but this will not work in conjunction with TP's stack checks enabled).
0
 

Author Comment

by:Rcm
ID: 6171636
To AvonWyss:
Well, that sounds a solution too, but is a bit too advanced for me now (I think). I'm in my final year of College (programmer) and we mostly got 4th generation languages (In Windows) ... the company who asked me to do this assignment uses DOS on the bootdisk (wich is logically). This assignment has to be "finished" next week because I will be graded then. And if I have a spare moments I'll try to use the SpawNo dbrunton "recommended".
When I finish College (over a few weeks) I'll probably stay at that company to finetune and enhance the bootdisk then I'm intended rewrite it in C.
Is the method you suggest "easy" to write and use ... it also sound a bit dangerous if you don't know what you're doing? (I'm just a beginner in Pascal and Dos-programming)
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6171736
My method is not hard, but you have to remember to use your memory functions all the time when allocating or releasing memory. Plus you always have to specify the size when allocating (as if you were using GetMem instead of New). Its not more dangerous than anything else, but may require some more work in some cases.

I'll put some code togehter and post it here for you if you want. But in that case, some more points would be nice... ;-)
0
 

Author Comment

by:Rcm
ID: 6172852
To AvonWyss:
Well, currently, all my points went to this question so I need to answer someone elses questions first before I can award more points to my question. This question was worth 100 points to me ... I hope that's sufficient. (it can take a while before I have the extra 25 points cos I'm not an expert in any field ... yet :-))
Thanks for helping me out. I really appreciate it. :-)
0
 

Author Comment

by:Rcm
ID: 6172869
Erm ... I know this is kinda like an extra question ... but I hope there's a simple answer to it and someone wants to answer it for free.
The programs I'm running using the Exec-command return an Errorlevel. Well, the doserror thingy always returns value 0 (Even if the program doesn't load correctly like the mem-error 8 when using net.exe).
The tools I use to detect a nic returns an errorlevel ... and the errorlevel corresponds to a certain nic. (I currently handle this in a batch file, but I want it in Pascal)
I can't award him/her any points, but (s)he is just a nice person in my opinion (as far as my opinion is worth anything)
0
 

Author Comment

by:Rcm
ID: 6172873
I just noticed I DO have 5 points (I didn't answer any questions?)
So i'll ask it again in a 'real' question. :-)
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6172937
RCM, you should in fact get 5 points each day. Note that question points are in no way dependent of expert points.
0
 
LVL 47

Expert Comment

by:dbrunton
ID: 6172948
Execswap returns error levels.
So does spawno

but these are dos errors and refer more to the ability to run the app concerned or not enough memory rather than any errors that the app itself reports back.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Rcm
ID: 6195564
Spawno doesn't seem to work. My system crashes when I use it (hangs or I get a fancy unhandled dos-error wich asks me nicely to reboot)
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6195791
function DOSGetMem(Paragraphs: Word): Word; assembler;
asm
     mov ah,$48
     mov bx,Paragraphs
     int $21
     jnc @end
     sub ax,ax
@end:
end;

function DOSGetFreeMem: Word; assembler;
asm
     mov ah,$48
     mov bx,-1
     int $21
     mov ax,bx
end;

function DOSFreeMem(SegAddr: Word): Boolean; assembler;
asm
     mov ah,$49
     mov es,SegAddr
     int $21
     mov al,1
     sbb al,0
end;

A paragraph is 16 bytes large. Thus, using the functions above, you can make the following custom memory management functions which do not require TP's heap manager:

function GetMemEx(Size: Word): Pointer;
begin
     if Size=0 then
          GetMemEx:=nil
     else
          GetMemEx:=Ptr(DOSGetMem((Size-1) shr 4+1),0);
end;

procedure FreeMemEx(Block: Pointer);
begin
     if Block<>nil then
          DOSFreeMem(Seg(Block));
end;

Note, however, that you should not allocate many many small blocks using DOS calls because of efficiency reasons.
0
 
LVL 6

Expert Comment

by:My name is Mud
ID: 6197876
ftp://x2ftp.oulu.fi/pub/msdos/programming/memory/00index.html


ftp://x2ftp.oulu.fi/pub/msdos/programming/memory/exec33b.zip  swap to EMS/XMS/disk and spawn - asm/c/pascal source
ftp://x2ftp.oulu.fi/pub/msdos/programming/memory/spwno413.zip  SPAWNO 4.13 - disk/EMS/XMS/INT15 swapping by R.Brown (asm/C/Pascal)
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6243855
RCM? Still there?
0
 

Author Comment

by:Rcm
ID: 6250619
To AvonWyss:

Yes.
But none of the solutions is "working".
I found some code on the net though, but now I get an error when leaving the program. It says there's not enough memory to load command.com (NOT within my program but when my program terminates)
Code of Exec-routine:
Function ShellCommand(command:String):Integer;
Var
 OldHeapEnd,
 NewHeapEnd: Word;
 Error:Integer;
 OutputTo:String[4];
Begin
 Error:=0;
 If Pos('>', Command) <> 0 Then
      OutputTo := ''
 Else
      OutputTo := '>nul';
 If MemAvail<$1000 then Error:=8;
 If Error=0 then Begin
  NewHeapEnd:=Seg(HeapPtr^)-PrefixSeg;
  OldHeapEnd:=Seg(HeapEnd^)-PrefixSeg;
   asm
    mov ah,4Ah
    mov bx,NewHeapEnd
    mov es,PrefixSeg
    Int 21h
    jnc @EXIT
    mov Error,ax
    @EXIT:
   end; {asm}
  If Error=0 then begin
   SwapVectors;
   Exec(GetEnv('COMSPEC'),' /C ' + command + OutputTo);
   SwapVectors;
    asm
     mov ah,4Ah
     mov bx,OldHeapEnd
     mov es,PrefixSeg
     Int 21h
     jnc @EXIT
     mov Error,ax
     @EXIT:
    end; {asm}
  end;   {If}
 end;    {If}
 ShellCommand:=Error;
end;     {Function}
0
 
LVL 47

Expert Comment

by:dbrunton
ID: 6251661
Without looking at your code I would suggest that when you are exiting your application you have not freed up memory used.  

That is the most likely cause of the error you are reporting.
0
 

Author Comment

by:Rcm
ID: 6252018
To dbrunton:
I free up all memory I use.
Well, if I don't run a whole bunch of external tools the program exits perfectly ... it's just when I load the network and all the other stuff that I get this error ...

I increased the points even more for someone who can come up with a solution I can use.
There are even some extra problems I haven't talked about earlier.
I need to be able to catch the Errorlevel (DosExitCode), redirect StdOut AND StdErr (to file) AND on top of that I need to solve the memory-problems I'm facing right now ...
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6252105
RCM, are you saying that you're loading drivers/TSR's while your program is "active"? That's a bad idea...
0
 
LVL 47

Expert Comment

by:dbrunton
ID: 6253671
In that case can you unload the network and other stuff?

There are/were utils out there that could force memory to be released.  Freemem and markmem were two of them from recall.  Turbopower had them at one stage and they were freeware.
0
 
LVL 47

Expert Comment

by:dbrunton
ID: 6258674
http://pascal.sources.ru/tsr/tsrcom32.htm

Use these utils to release your memory occupied by your network drivers and tsrs before you return to your graphic shell.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6934198
Rcm - Please update / finalize this question.  If you've been helped, please accept the comment that helped you to convert to the answer, grade and close.  If more is needed, please let the participants here know.

Thanks,
Moondancer - EE Moderator
0
 

Author Comment

by:Rcm
ID: 6935508
To Moondancer:

I tried most of the answers which were proposed, but it either did not work or they did not meet the requirements I needed.
Unfortunately the question was for my thesis (which I turned in in June 2001)

Although it's no longer usefull for me, I'm still curious about the answer though.

But I can delete the question if you like.
0
 
LVL 6

Expert Comment

by:My name is Mud
ID: 6935983
Moon:
Instead of deleting it, why don't you PAQed with refund points???
0
 
LVL 47

Expert Comment

by:dbrunton
ID: 6936216
Yep, I like that idea.  Lot's of useful stuff here.
0
 
LVL 1

Accepted Solution

by:
Moondancer earned 0 total points
ID: 6941934
Thank you very much for your response and guidance given here.

I have refunded 150 points to Rcm for this question, and moved it to the PAQ at zero points to close it, so others may benefit from the information you've given here.

Moondancer - EE Moderator
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

A procedure for exporting installed hotfix details of remote computers using powershell
Moving applications to the cloud or switching services to cloud-based ones, is a stressful job.  Here's how you can make it easier.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now