Sharing is caring: friends, manage your resources!*
Lots of modern languages help us out with automatic memory management, but for other types of resources, we're left in charge. I'll talk about some of the problems that can come out of poorly managing resources like files and database connections, and show you a few of the tools that language designers have given us to make this easier.
We’re pretty familiar with memory being a resource that we need to manage, but it’s definitely not the only one. For example, if you open too many files and forget to close them, your process will eventually be unable to open any more; if you’re determined, you could even hit the kernel’s global limit on open files. If you forget to close your database connections, you’re wasting a valuable server resource and possibly preventing other users from accessing the database.
Unlike memory, where you can often offload the burden to a garbage collector, resources like these need to be managed manually. Luckily, this is so common that most modern languages have built-in features to help out. In this talk I’ll walk through a few kinds of resources and what can go wrong if they don’t get released. Then I’ll give an overview of some of the tools that languages provide to make managing them easier, like try / finally in Java, and Python’s context managers and with statement.
resource, management, files, connections, leaks
I have spoken at Montreal's Python and Haskell meetups, as well as at the Vim meetup that I organise. I've also given a lightning talk at PyCon Canada.
Here are slides for a couple of these talks:
Haskell typeclasses: http://kmrhb.ca/typeclass-talk/
Vim text objects: http://kmrhb.ca/talk-vim-text-objects/
Amazon EC2 spot instances: https://speakerdeck.com/pyconca/hour-ec2-spot-instances-kamal-marhubi
This talk is new.
Kamal Marhubi is a polyglot programmer who works for Google in Montreal, where he helps keep the internet clean and malware-free. He once wrote a game AI in Haskell, and after five years of being an emacs user, he’s now the organiser of vimtl, the Montreal Vim user group. He is secretly addicted to English change-ringing, and has rung the bells of over twenty churches in England, Canada, and the US.