I'm not sure if this counts as writing malicious code. What does the community think?
A small, finite memory leak is not malicious
Using up all a computer's memory is a denial of service attack, and thus arguably malicious (although it's something that's easily done by mistake and easily done intentionally, and also has many existing safeguards against it, so PPCG wouldn't really be helping any malware authors there). But that's this question, which is a different question.
Wasting a small amount of a computer's memory in a way that can't be recovered until the program ends is, on most operating systems, entirely harmless, and something that nonmalicious programs do all the time intentionally. (After all, keeping the memory allocated but nonleaked has exactly the same effect on other processes.) It would be potentially problematic on operating systems like DOS, in which a memory leak makes the memory unusable until the system is rebooted (!), but those systems are in the minority nowadays (and DOS users are likely used to rebooting fairly often anyway).