CFMX Memory usage and system info

Continuing my hacking CF series - I've been looking to see how much useful information I can pull out of the underlying java objects.

I've put together a collection of useful System functions:

getJavaMemoryInfo

Returns a struct which contains info about the memory usage of the underlying java runtime
<cffunction name="getJavaMemoryInfo" returntype="struct" output="false">
   <cfscript>
      var runtime = createObject("java","java.lang.Runtime").getRuntime();
      var stMemInfo = structNew();
      
      stMemInfo.freeMemory = runtime.freeMemory();
      stMemInfo.maxMemory = runtime.maxMemory();
      stMemInfo.totalMemory = runtime.totalMemory();
      stMemInfo.heapMemory = runtime.totalMemory()-runtime.freeMemory();
      
      return stMemInfo;
   </cfscript>
</cffunction>

getProcessorCount

Obviously enough returns the number of processors (that the java runtime can access) in the machine.

<cffunction name="getProcessorCount" returntype="numeric" output="false">
   <cfscript>
      var runtime = createObject("java","java.lang.Runtime").getRuntime();
      return runtime.availableProcessors();
   </cfscript>
</cffunction>

getServerName

Get the servername from the system (should work in both windows and unix platforms) but I haven't tested it recently.

<cffunction name="getServerName" returntype="string" output="false">
   <cfset var machineName = "">
   <cfset var factory = createObject("java", "coldfusion.server.ServiceFactory")>
   <cfif factory.runtimeservice.getServerScope().os.name CONTAINS "Windows">
      <cfregistry action="get"
         branch="HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\TCPIP\Parameters"
         entry="Hostname"
         variable="machineName"
         type="String">

   <cfelse>
      <cfexecute name="/bin/hostname" variable="machineName" timeout="5" />
   </cfif>
   <cfreturn LCase(trim(machineName))>
</cffunction>

getCFInstanceName

Return the instance name - useful in clusters to see which instance is handling the request.

<cffunction name="getCFInstanceName" returntype="string" output="false">
   <cfscript>
   // code to display the java instance name    var jrunObj = createObject("java", "jrunx.kernel.JRun");
   return jrunObj.getServerName();
   </cfscript>
</cffunction>

Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.1.004.