Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Possible to limit CPU usage

Status
Not open for further replies.

Antithott

IS-IT--Management
Jan 20, 2006
90
DK
HI all :)

Is there anyway to limit the CPU usage a program uses?

I have a app which go thru all Users of my Active Directory, and for each of them insert a line of text in the notes box.

while it does this, the CPU uses 95% cpu power, and the memory also keep rising for each user, Once the program ends its cycle, then the memory go back down to 23mb.

I have 2 questions, is there a way to limit the cpu usage to 50% instead of the 95%? and a way to release used memory after each user ?

Im using a For each loop.
Is there a way to limit the CPU usage from this program to only allow it to use 50% instead of 95%.
 
There is not really a way to "limit" cpu utilization that I know of... but you could add a line that slows the thread down if it was important. Telling the thread to sleep for some number of milliseconds would help.

System.Threading.Thread.Sleep(100)

As for the rising memory, it sounds like you might be creating a new instance of an object within each loop. You might have to post some code and tell us which object you believe it is that is causing this. I, or someone else here, could probably tell you how to restructure the code to use less RAM.


Senior Software Developer
 
Another thing, you may also be able to use a slider control to set the number of milliseconds and use it like a throttle. If you do this then you may find that you have to use an “Application.DoEvents()” line within your loop.

Senior Software Developer
 
thansk for the reply.

Im gonna take a look at what you said about putting in a small timer.

As for the memory, ill post some code and see if you can help me out.
 
There is a setting in System.Threading.Thread.CurrentThread that will allow you to set the importance of the thread. I can't recall the names or values off the top of my head, but RealTime means it is a very critical thread and it will not give up resources. The lower on the scale you go, the less critical the OS thinks the thread is. So if you put that thread at the lower priority settings, and the user tries to do something else that is resource intensive (say, like open IE), the thread that is opening IE will take the resources and your thread will wait for that thread to finish.

If nothing else is happening, your thread will still suck up 95% of the processor time, but it will give up processor time as soon as anything else asks for it.

As for your memory issue, that is likely a code problem with some object being left in memory each iteration through your loop.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I tried setting the thread to lowest importance, And it worked out great. It doesnt matter it take up 95% if it just free up CPU time when other stuff needs it.

As for the Memory Problem, Is it possible to somehow free up all the memory used in a thread with a command? or how is the correct way to clean up whena thraed closes.

Normally i do this :

Set object and variables to nothing or dispose.
then do a Gc.collect

Is there another way to clean up betteR?
 
Setting variables to nothing doesn't really help a lot disposing and closing helps more.

Doing GC.Collect all the time won't help a lot either.

Christiaan Baes
Belgium

"My old site" - Me
 
Set object and variables to nothing or dispose.
then do a Gc.collect

This is about the only way you can clear up memory,

There are ways to improve memory allocation, SiriusBlackOp did mention this. If you are using variables within a loop, it is best to declare them before the loop starts, this will preallocate the memory once (not on every cycle). You can also do this on functions/subs that will only be called once at any given moment, by declaring the variables at module level.

Examples:
Code:
Dim TempStore As String

Do
   'Open files in sequance...
   TempStore = MyFile.ReadToEnd

   'Do something to the TempStore...
Loop

And...
Code:
Public Module MyVariables
  Dim TempStore As String
  Dim FileName As String
End Module

Note, do not assign a value to the Module level variables when using them in this way, because the runtime could (and does) randomly reassign the declaring variable.
 
First off, Thanks for all the help in this thread.

I still have huge trouble with the memory, everytime this sub runs it uses around 2mb of memory, and just keeps raising.

I cant see what i should do diffrent. but i tried the previous suggestion of putting variables in a module and just assigning values to them instead of calling em in the start of the sub, but it made no noticable diffrentc.

I run this sub every 10minutes. just for info.

Any help or suggestion as to what i can do to eliminate the memory risings would be most welcome

Code:
    Private Sub Search500Users()
        Amount = 0
        StartTime = Date.Now

        Dim Entry As New DirectoryEntry
        Dim obj As DirectoryEntry
        Dim Searcher1 As New DirectorySearcher(Entry)

        Dim sqlcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("INSERT INTO index.dbo.users (CommonName,Description,DisplayName,distinguishedname,GivenName,Surname,
Name,Office,profilepath,SamaccountName,userprincipalname,mail,country,
company,Department,HomePhoneNumber,City,Manager,Mobile,PostalCode,State,
Street,TelephoneNumber,notes,lastSync) VALUES ('" & obj.Properties("cn").Value & "','" & obj.Properties("description").Value & "','" & obj.Properties("displayname").Value & "','" & obj.Properties("distinguishedname").Value & "','" & obj.Properties("givenname").Value & "','" & obj.Properties("sn").Value & "','" & obj.Properties("name").Value & "','" & obj.Properties("physicalDeliveryofficename").Value & "','" & obj.Properties("profilepath").Value & "','" & obj.Properties("SamAccountName").Value & "','" & obj.Properties("UserPrincipalName").Value & "','" & obj.Properties("mail").Value & "','" & obj.Properties("c").Value & "','" & obj.Properties("company").Value & "','" & obj.Properties("department").Value & "','" & obj.Properties("homephone").Value & "','" & obj.Properties("l").Value & "','" & obj.Properties("manager").Value & "','" & obj.Properties("mobile").Value & "','" & obj.Properties("postalcode").Value & "','" & obj.Properties("st").Value & "','" & obj.Properties("streetaddress").Value & "','" & obj.Properties("telephonenumber").Value & "','" & obj.Properties("info").Value & "','" & obj.Properties("OtherPager").Value & "')", connection)
        Dim sqllog As OleDb.OleDbCommand = New OleDb.OleDbCommand("INSERT INTO index.dbo.SyncLog (StartTime,Endtime,Amount,Notes) VALUES ('" & StartTime & "','" & Date.Now & "','" & Amount & "','Nothing')", connection)

        Searcher1.SearchScope = SearchScope.Subtree
        Searcher1.Filter() = "(&(objectClass=user)(sAMAccountName=*) (objectCategory=person) (!OtherPager=*SynkOK*) (!OtherPager=*SynkNot*) (!info=*SynkNot*))"
        Searcher1.SizeLimit = 500
        Searcher1.CacheResults = False

        connection.Open()
        For Each AdObj As SearchResult In Searcher1.FindAll


            obj = AdObj.GetDirectoryEntry
            obj.Properties("OtherPager").Value = "SynkOK : " & Date.Now
            obj.CommitChanges()
            sqlcmd.ExecuteNonQuery()
            Amount += 1
        Next

        sqllog.ExecuteNonQuery()
        connection.Close()

        sqllog.Dispose()
        sqlcmd.Dispose()
        Searcher1.Dispose()
        obj.Dispose()
        Entry.Dispose()
        GC.Collect()
    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top