We have seen a number of instances of popular smart contracts that stored a lot of data and that "clean up" their data with a single function call. Sometimes that clean-up code uses a lot of gas and hits the maximum gas limit for a single call.

Is there a function to tell the contract writer the "remaining gas limit" during execution and then write code to exit when that level gets too low, so a clean-up could be performed with multiple calls without running out of gas?

Alternatively, is there at least a "gas used so far during this call" function?

2 Answers 2


In Solidity, this is available as msg.gas, documented here. There's no direct way to obtain the initial gas; the best option is to store msg.gas to a variable as soon in the contract execution as is practical.


on: live.ether.camp Ethereum Studio you have a complete Ethereum sandbox where you can imitate transaction execution and have very good estimation of the used gas.

More info to start is here:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.