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aaaaaaaaaaaaaaaaaaaaaaaa";
    return oneKb;

According to this post it should cost something like 21,000 (For the base transaction cost) + 24,000 + (64,000,000 / 512), because my a is 8000.

However, if I test it out on remix, it says that test_1kb took about 24314 gas, which is significantly lower than I expected.

1 Answer 1


Your a is not 8000

Citing from the post you've included: G_memory is 3, and a is the number of 256-bit words (ints) allocated.

Every character in a string takes up 2 bytes in memory. Solidity uses 32 bytes (256 bit) memory slots, so one slot can hold exactly 16 characters.

You can even verify that in Remix. enter image description here

Now, you string has 1000 characters, that means you're going to need 62,5 (63) memory slots, which is your a to plug into that equation.

Cmem = 3 * 62,5 + ((62,5^2)/512) = 195

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.