4

Can some one please explain what exactly happens when I click on RunTest button? How the code is internally compiled and run? For example In java when we have JVM, JRE. and When we compile, a .class file is generated. Similarly in Salesforce what is happening internally?

1 Answer 1

7

When you call the RunTest command, the system first checks the local code cache for valid byte code. If it fails to find it there, it then checks the pod-wide shared code cache; if the code is there, it's loaded locally and stored in the local cache.

If that fails, it then retrieves your source code from the database, compiles it to byte code, and places a copy in the pod-wide shared cache and local cache. Once the byte code is available, it then runs the code.

The Apex Code execution system is a type of VM like Java, with hooks into various Java libraries-- a process called marshaling-- to help improve the performance of the code when possible. It's also responsible for things like checking governor limits, allocating memory, presenting queries to the database, and so on.

You can watch Peek Under the Hood of the New Apex Compiler for a more in-depth explanation if you'd like. Note that there's only so much salesforce.com can share with you, because they need to protect their Intellectual Property. That said, it works in a manner comparable to Java.

3
  • Thank you very much. I watched the video link that u suggested. It was said in the video that the Apex class is complied into java byte code and then interpreted using their own interpreter. Are they using JMV to compile it into java byte code? Then what is Apex compiler? What is its role?
    – user52800
    Jan 14, 2018 at 2:00
  • I didn't even understand why he said they are using their own interpreter why is it unsafe to use JRE
    – user52800
    Jan 14, 2018 at 2:11
  • @user52800 the Apex compiler converts Apex source to an unspecified byte code format, which is then run by the Apex interpreter. I don't know why they do it that way, but that's how it's done. Only an internal employee could possibly provide a real reason, but I'm pretty sure it's security related.
    – sfdcfox
    Jan 14, 2018 at 3:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .