Ive created a server monitoring script written in classic ASP (Hosted on IIS 7.5).
It has a main page which uses jQuery to automatically reload the content (updating WMI information) It works great, but seems to have an appetite for memory!
There are 4 pages (Service status for 2 servers, printer status on a main DC and disk usage on the same server) The service status pages update every 10 seconds, the printers every 5 seconds and disk usage every minute.
The script basically works for a few hours, then i get code 500 internal server errors, once checking the IIS logs its because of it being "Out of memory" and look at the processes on the server and WmiPrvSvc.exe (NETWORK SERVICE account) is in the hundreds (500mb) and the ISS worker process (w3wp.exe) is using about 50mb (there are other w3ps.exe processes that are higher)
Once i end them both it kicks back into action… Or if i stop the page/requests for a while (varies between 30 sec and 5 mins) the WmiPrvScs ends and i don't get the problem.
Is there a way to minimise the memory usage or is there a command to properly disconnect / clear the memory?
Below is the basics of one of my pages to give you an idea of what's going on...
Thanks, Paul.
<%
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery("SELECT Caption, Started FROM Win32_Service WHERE StartMode = 'Auto'")
Set arrServices = Server.CreateObject("System.Collections.ArrayList")
intServices = colListOfServices.Count
For Each objService in colListOfServices
arrServices.Add objService.Caption & "#" & objService.Started
Next
arrServices.Sort()
Response.Write "<table width=""100%"" class=""tblServices"">" & vbCr
For Each strService in arrServices
If InStr(strService, ".NET Framework") = 0 AND InStr(strService, "Sophos Web Intelligence Update") = 0 AND InStr(strService, "Shell Hardware Detection") = 0 Then
' Above services, start at system startup and then stop after nothing to do
arrServiceDetails = Split(strService, "#")
strServiceName = arrServiceDetails(0)
strServiceStatus = arrServiceDetails(1)
End If
Next
Response.Write "</table>"
Set objWMIService = Nothing
Set colListOfServices = Nothing
Set arrServices = Nothing
%>
Are you calling the above code every 10 seconds?
I´m not sure, but i looks like you are creating a new WMI object / connection everytime and that adds up.
Instead try to keep the object around instead of recreating it. Something like a SingleTon class to hold the connection.
Also, try making a strongly typed class instead of the WMI query, you can do that by looking at this site: MGMTClassGen
Related
I automated our SAP system using SAP Gui Scripting. After 30 minutes where the script works, our SAP system logs off (auto-logoff) because it doesn't recognize the activities of the script. Is there a parameter to change this?
Don't missunterstand me - I don't want to deactivate auto logoff. I want SAP to recognize the script activities so auto logoff woulnd't be performed as long as my script is working.
Regards and thank you,
Jan
For me, for this reason, the following script runs automatically the whole day after logging in:
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
End If
If Not IsObject(session) Then
Set session = connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
wscript.sleep 2000
do
'if time() > "16:00" then exit do
on error resume next
session.findById("wnd[0]").maximize
if err.number <> 0 then exit do
on error goto 0
session.findById("wnd[0]/tbar[0]/okcd").text = "/o"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]").close
wscript.sleep 29000
Loop
Regards,
ScriptMan
I am developing a website in asp.net. Site is running in localhost at satisfactory speed. As I deploy the site in hosting server with database stored in that hosting server, the website is taking very long time to load. Log In page loads instantly. After that, home page takes around 45 - 60 seconds to load. In home page, I have a small reporting section to perform calculation and grid view that has 70-80 rows. Data sets for both object are brought from stored procedure.
After debugging, I found out that
calculation for reporting takes around 1-2 seconds
and calling stored procedure and assigning it in data set takes almost 6-10 seconds
but binding the data set in grid view is taking very long time i.e. 30 -45 seconds.
From log in page to home page, I have used two session and i have set <compilation debug="false"/> in web.config. How can I improve the loading performance ?
The code for grid view is :::
installationDate = Convert.ToDateTime(companyInformation.Tables[0].Rows[0] ["InstallationDate"]);
employeeChecks = clsBLogic.GetAllChecksOfEmployee(tblEmployeeNo, installationDate, currentDate);
SqlDataSource sqlDataSource = new SqlDataSource();
DataTable dt = employeeChecks.Tables[0];
DataView dv = new DataView(dt);
dv.Sort = "Date" + " " + "desc";
gvEmployeeStatus.DataSource = dv;
gvEmployeeStatus.DataBind();
Any help?
Thanks in advance..
I have a requirement when a user clicks a specific arrangement of radio buttons to run a series of vbscripts (and soon Perl scripts).
I have all of the vbscripts stored server side, they do not need to be on the remote system to run. Oh yes, the scripts are gathering information on remote system in our intranet.
What would be the best way. Currently I have this to run just one script, not multiple...should I keep this or dispose of this idea.
Protected Sub windowsScript(ByVal COMPUTERNAME As String)
' Create an array to store VBScript results
Dim winVariables(1) As String
Dim filePath As String = COMPUTERNAME & "\C$\Windows\somefile.txt"
'Execute PsExec on script
runPsExec(COMPUTERNAME, "systemInfo.vbs", 1)
'Import data from text file into variables
textRead(filePath, winVariables)
System.Threading.Thread.Sleep(1000)
'Delete the file on server - we don't need it anymore
runPsExec(COMPUTERNAME, "systemInfo.vbs", 2)
MsgBox("Windows OS: " & winVariables(0).ToString())
MsgBox("Service Pack: " & winVariables(1).ToString())
End Sub
Also, it is hard to see here because I do have another function "textRead" but what is going on is this particular script is stored client side and the vbscript it outputting to a text file. textRead will read the variable and send a text file back to the server to read it.
This is definitely not what I want to do.
I want to be a little more dynamic, plus with my new scripts...they don't need to be on the client at all.
Any help would be appreciated :)
I'm thinking of making some type of While loop, not sure if that would work.
It's kind of strange to do this through the browser. In my company we collect systeminfo at logontime with a vbscript logonscript and add the result to a logfile which we can access through a webapp to do research. Occasionally when the need rises we run a specific script to gather more data or change some system setting through windows SCCM.
If the goal is to provide the user with info about his system there are some good utilities around which can be run locally (but from a location on a server share).
EDIT
a simple way to start multiple processes
dim scripts_to_run, script
const COMPUTERNAME = 0, SCRIPTNAME = 1, EXTRA_PARAMS = 2
scripts_to_run = Array(_
Array("computer1","script1.vbs",1),_
Array("computer2","script1.vbs",0),_
Array("computer3","script3.vbs",3)_
)
for each script in scripts_to_run
runPsExec script(COMPUTERNAME), script(SCRIPTNAME), script(EXTRA_PARAMS)
runPsExec join(script,",")
next
sub runPsExec(p1, p2, p3)
'here coms your code shat runs the script
wscript.echo p1 & p2 & p3
end sub
or a shorter version
dim scripts_to_run, aArgs
scripts_to_run = Array(_
Array("computer1","script1.vbs",1),_
Array("computer2","script1.vbs",0),_
Array("computer3","script3.vbs",3)_
)
for each aArgs in scripts_to_run
runPsExec aArgs
next
sub runPsExec(aArgs)
'here coms your code shat runs the script
wscript.echo aArgs(0) & aArgs(1) & aArgs(2)
end sub
I have a classic ASP CRM that was built by a third party company. Currently, I have access to the source code and am able to make any changes required.
Randomly throughout the day, usually after some prolonged usage by users, most of my pages start getting an Out of Memory error.
The way that the application is built, is all the pages and scripts pull core functions from a Global.asp file. In that file are embeds to other global files as well, but the error presented shows
Out Of Memory
WhateverScriptYouTriedToRun.asp Line 0
Line 0 is the include for the global.asp file. Once the error occurs, after an unspecified amount of time the error occurence subsides for some time but then begins to reoccur again. With how the application is written, and the functions it uses, and the "diagnostics" I've already done - it seems to be a common used function that is withholding data such as recordset or something of that nature and then not releasing it properly. Other users then try to use the same function and eventually it just fills up causing the error. The only way for me to effectively clear the error is to actually restart IIS, Recycle the App Pool, and Restart the SQL Server Services.
Needless to say, myself and my users are getting annoyed....
I can't pinpoint the error due to the actual error message presented being Line 0 - but from there I have no idea where in the 20K lines of code it could be hanging up. Any thoughts or ideas on how to isolate or at least point me in the right direction to begin clearing this up? Is there a way for me to increase "memory" size for VBScript? I know there is a limitation but is it set at say...512K and you can increase it to 1GB?
Here are things I have tried:
Removing SQL Inline statements into Views
Going through several hundred scripts and ensuring that every OpenConnection & OpenRecordSet is followed by an appropriate Close.
Going through the Global File and commenting out any large SQL statements such as ApplicationLog (A function that writes the executed query into a table).
Some smaller script edits.
Common Memory Leak
You say you are closing all recordsets and connections which is good.
But are you deleting objects?
For example:
Set adoCon = new
Set rsCommon = new
'Do query stuff
'You do this:
rsCommon.close
adocon.close
'But do you do this?
Set adoCon = nothing
Set rsCommon = nothing
No garbage collection in classic ASP, so any objects not destroyed will remain in memory.
Also, ensure your closes/nothings are run in every branch. For example:
adocon.open
rscommon.open etc
'Sql query
myData = rscommon("condition")
if(myData) then
response.write("ok")
else
response.redirect("error.asp")
end if
'close
rsCommon.close
adocon.close
Set adoCon = nothing
Set rsCommon = nothing
Nothing is closed/destroyed before the redirect so it will only empty memory some of the time as not all branches of logic lead to the proper memory clearance.
Better Design
Also unfortunately it sounds like the website wasn't designed well. I always structure my classic ASP as:
<%
Option Explicit
'Declare all vars
Dim this
Dim that
'Open connections
Set adoCon...
adocon.open()
'Fetch required data
rscommon.open strSQL, adoCon
this = rsCommon.getRows()
rsCommon.close
'Fetch something else
rscommon.open strSQL, adoCon
that = rsCommon.getRows()
rsCommon.close
'Close connections and drop objects
adoCon.close
set adoCon = nothing
set rscommon = nothing
'Process redirects
if(condition) then
response.redirect(url)
end if
%>
<html>
<body>
<%
'Use data
for(i = 0 to ubound(this,2)
response.write(this(0, i) & " " & this(1, i) & "<br />")
next
%>
</body>
</html>
Hope some of this helped.
Have you looked at using a memory monitoring tool to see how much memory fragmentation is happening? My guess at a possible cause is that some object of a size is trying to be created but there isn't enough room in the memory to store it as one contiguous chunk. Imagine needing room to store an object that would take 100 MB and while there may be several hundred megabytes free, the largest contiguous chunk is 90MB then this doesn't fit.
Debug Diagnostic Tool v1.1 would be a tool where Bernard's articles may help in understanding how to use the tool.
Another thought is the question of how much string concatenation is there in the code? I remember where I used to work had problems with doing a lot of string concatenation operations that sucked up memory that may be another idea to consider.
Yeah, I could see some shock at that kind of number the first few times you see it but then if you understand what the code is doing it may make sense for why so much space gets reserved right off the bat at times.
I haven't used that debug tool specifically but I did have a tool that took a snapshot of memory when pages were hung so I couldn't tell if there was a performance impact of the tool or not. Course in my case I used a similar tool in 2004 so it has been a few years since I've had to research this kind of issue.
Just going to throw this in here, but this problem has taken a long time to solve. Here's a breakdown of what we did:
We took all the inline SQL and made SQL Views, every SELECT statement is now handled with a VIEW first.
I took every single SQL INSERT and UPDATE (as much as I could without breaking the system) and put them into Stored Procedures.
#2 was the one item that really made the biggest difference
Went through several thousand scripts, and ensured that variables were properly disposed of, and all the DB Open Connections were followed correctly with a Close Connection and same with Open/Close RecordSet.
One of the slow killers was doing something like:
ID = Request.QueryString("ID)
at the top of the page. Before redirecting, or closing a page, there is always a:
Set ID = Nothing
or the complete removal of the inference.
I am using aspcompat page attribute in ASP.NET so the com components I call can get at ASP intrinsic objects (Request, Response, Application, etc)
I have quickly created a new test project, one asp.net page and a vb6 com component.
The page does this:
for (int i = 0; i < 1000; i++)
Application["string" + i] = i.ToString();
Debug.WriteLine(string.Format("{0} done adding strings to app",Environment.TickCount));
var asp = new ASPTest.CompClass();
asp.SetProcessId();
Basically I add stuff in the application object and then call the com component.
Set context = GetObjectContext
If Not context Is Nothing Then
Set app = context("Application")
Set ses = context("Session")
Set resp = context("Response")
If Not app Is Nothing Then
OutputDebugString "" & GetTickCount & " writing response"
resp.Write "I see application from vb <br/>"
OutputDebugString "" & GetTickCount & " before "
For i = 100 To 200
resp.Write GetTickCount & " i = " & app("string" & i) & "<br/>"
Next
OutputDebugString "" & GetTickCount & " after "
End If
Else
OutputDebugString "No context"
End If
The problem is that the more stuff I put in the application the slower the vb code becomes. Not all the code though, just when I first try to invoke any method on intrinsic objects, in my example my first resp.Write call. If I add 10000 items in the app resp.Write takes ten seconds to complete, 60000 => timeout.
It seems that when I touch any intrinsic object the first time the runtime does something nasty to make the objects available to COM.
Have you ever seen this? Any suggestion is much appreciated.
This is almost certainly a problem with threading, probably having the Interop performing thread context switches with every call from the VB component to the classes in the context.
I'm not the expert here, so I might (probably wil) get corrected, but I believe you need to make sure your VB component is also STA, just like the ASP.NET page marked with the aspcompat=true sets it. If you keep all the components in the same thread, there won't be any context switches (which require very expensive marshalling).