sabell2012
Mega Sage
Mega Sage

NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.

 

DIFFICULTY LEVEL:    INTERMEDIATE
Assumes having taken the class SSNF and has good intermediate level of knowledge and/or familiarity with Scripting in ServiceNow.


Because JavaScript does it for us we don't often have to consider memory management. However, I bring this up because I have run into a problem with server resources myself; from time-to-time. It is possible to run out of memory! If you are working on a really large object array (several million records); you must remember that all of that is in the server's memory. If you keep adding to it you risk the possibility of running into problems with your instance such as impacting performance. Perhaps severely.

 

For example, let's say you are working with pulling data from a number of GlideRecord record sets.

 

Pseudo code:

 

1. Get GlideRecord Incident (all records)

2. Loop through all records and store lots of data into an object array.

3. Get GlideRecord Change (all records)

4. Loop through all records and store lots more data into our object array.

5. Get CMDB_CI (all records)

6. Loop through all records and store lots more data into our object array.

 

...and so on. Remember this is hypothetical and not necessarily something you would really want to do! 🙂

 

Now ALL of this stuff is in memory, and it could be literally millions of records!

 

The poor server on the ServiceNow side is trying to keep up with you, paging memory to disk, juggling things around to improve performance, but you are merciless, and keep throwing more stuff for it to manipulate...and it slowly goes to it's metaphorical knees.

 

So, what do you do? You free up what you no longer need!

 

Revise our Pseudo code:

 

1. Get GlideRecord Incident (all records)

2. Loop through all records and store lots of data into an object array.

3. Set the Incident GlideRecord to null.

4. Get GlideRecord Change (all records)

5. Loop through all records and store lots more data into our object array.

6. Set the Change GlideRecord to null.

7. Get CMDB_CI (all records)

8. Loop through all records and store lots more data into our object array.

9. Set the CMDB_CI GlideRecord to null.

 

So here is an example:

 

var incidentRecords = new GlideRecord('incident');
incidentRecords.addActiveQuery();
incidentRecords.query();

var incident = {};
var incidentList = [];

while (incidentRecords.next()) {
	incident = {};
	incident.number = incidentRecords.number + '';
	incident.status = incidentRecords.status + '';
	incident.assigned_to = incidentRecords.assigned_to.getDisplayValue();
	incidentList.push(incident);
}

incidentRecords = null; // This frees up the memory that incidentRecords is using.

gs.info(JSON.stringify(incidentList));

 

Should give you results like this (just outputting our captured values):

 

*** Script: [{"number":"INC0008001","status":"undefined","assigned_to":""},{"number":"INC0000015","status":"undefined","assigned_to":"Don Goodliffe"},{"number":"INC0000016","status":"undefined","assigned_to":"ITIL User"},{"number":"INC0000017","status":"undefined","assigned_to":"Fred Luddy"},{"number":"INC0000018","status":"undefined","assigned_to":"ITIL User"},{"number":"INC0000019","status":"undefined","assigned_to":"Bud Richman"},{"number":"INC0000020","status":"undefined","assigned_to":"ITIL User"},{"number":"INC0000025","status":"undefined","assigned_to":"ITIL User"},{"number":"INC0000027","status":"undefined","assigned_to":"ITIL User"},{"number":"INC0000029","status":"undefined","assigned_to":"Don Goodliffe"}]

 

So, again, what is happening here? We are nulling our the really dirty massive object of GlideRecord. Getting it out of memory by nulling it out. With recursive functions, or just massive recordsets you will want to utilize this method.

 

Obviously this is a best practice when manipulating large objects in memory, and a good general practice otherwise.

 

Enjoy!

Steven Bell.

 

If you find this article helps you, don't forget to log in and mark it as "Helpful"!

 

sabell2012_0-1698677859091.png


Originally published on: 09-08-2015 01:58 PM

I updated the code and brought the article into alignment with my new formatting standard.

5 Comments