ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Common Board

Memory Measuring -- A funny story
Posted by Alexander Shulgin 29 Jul 2004 16:43
Hello, everyone!

I have tried to solve the 1002 problem, but got Memory Limit (on the test 7).

Since in the description stated: ``The total size of the input file doesn't exceed 300KB.'' and I am loading the file plus no more than 400K, the total amount of memory used by the program should not exceed a megabyte. But I have got something around 1.5M in the online status.

I wondered: ``How does the server measure the memory usage ?''

So I experimented few times. I have put this line in the very begining of `main':

malloc (1024 * 1024);

And got the MLE on the same 7th test, but the amount was not different a lot: it was around 1.5M again.

Then I suggested that `malloc' just failed and returned `NULL', which no one noticed; and the program ran trough, giving the same result.

So, I have put the `if' to check, wether that `malloc' succeded.

if (malloc (1024 * 1024) == NULL)
  *((char *) main) = '!';

I have expected that this will crash, but it did not and the overall result was the same: MLE on test 7.

Fortunately, that time the lunch-break approached and I have got the walk instead of pulling my hair off the head :)

I tought a few about what could that mean and th idea came to my mind instantly: yes, that `malloc' succeded, but the virtual pages, allocated by the call, have no phisycal backing in the system, until they are filled with something.

So, ultimately, I have put the `memset', to fill whole the allocated area and got the MLE on the first test... finally.

That funny story gives You, dear reader, the example of famous russian proverb: ``Then You are in hurry, people would get laughing''.

Alex

PS: Does one can explain, nonetheless, why I have got that limit ? And why nearly the same code can vary memory usage so havely ? (You can look in the online status: somewhere around # 664444 are my posts; the difference is around 100K - 300K !)
Re: Memory Measuring -- A funny story
Posted by Problemset Maintainer 29 Jul 2004 17:15
Try to work more optimal with memory. I got AC in 0.031s and 289K.
Re: Memory Measuring -- A funny story
Posted by Alexander Shulgin 29 Jul 2004 17:29
Thank You.

That montsrous memory usage was due to faster dictionary look-up.

Alex