{"action":"created","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/3137","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/3137/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/3137/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/3137/events","html_url":"https://github.com/sequelize/sequelize/issues/3137","id":57691201,"number":3137,"title":"findOrCreate crashes the process with Out of Memory","user":{"login":"arcanis","id":1037931,"avatar_url":"https://avatars.githubusercontent.com/u/1037931?v=3","gravatar_id":"","url":"https://api.github.com/users/arcanis","html_url":"https://github.com/arcanis","followers_url":"https://api.github.com/users/arcanis/followers","following_url":"https://api.github.com/users/arcanis/following{/other_user}","gists_url":"https://api.github.com/users/arcanis/gists{/gist_id}","starred_url":"https://api.github.com/users/arcanis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arcanis/subscriptions","organizations_url":"https://api.github.com/users/arcanis/orgs","repos_url":"https://api.github.com/users/arcanis/repos","events_url":"https://api.github.com/users/arcanis/events{/privacy}","received_events_url":"https://api.github.com/users/arcanis/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/sequelize/sequelize/labels/bug","name":"bug","color":"d7e102"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-02-14T12:11:35Z","updated_at":"2015-05-18T06:10:43Z","closed_at":null,"body":"Here is a showcase. If you set the `TESTED` function to `works`, it ... works. And doesn't work anymore with `doesNotWork` (the process goes out of memory).\r\n\r\nI suggest executing `ulimit -m 2048000` (or lower) before trying the `doesNotWork` branch. I haven't used it the first time I launched the script, and my laptop crashed horribly.\r\n\r\nNote that if sequelize has to create an element each time (if we use random ids in the where clause), the `works` branch crashes too. That may be another issue, I would expect everything to be garbage collectable if Node needs to free some space during the execution. Even forcing the garbage collection doesn't work.\r\n\r\n```js\r\nvar Sequelize = require( 'sequelize' );\r\nvar sequelize = new Sequelize( 'database', 'user', 'pass', { dialect : 'postgres', logging : false } );\r\n\r\nvar buffer = new Buffer( 5 * 1024 * 1024 );\r\n\r\nvar works = function ( Model, where, defaults ) {\r\n\r\n    return Model.find( { where : where } ).then( function ( model ) {\r\n        return model ? model : Model.create( require( 'lodash' ).merge( { }, where, defaults ) );\r\n    } );\r\n\r\n};\r\n\r\nvar doesNotWorks = function ( Model, where, defaults ) {\r\n\r\n    return Model.findOrCreate( { where : where, defaults : defaults } );\r\n\r\n};\r\n\r\nvar Foo = sequelize.define( 'Foo', {\r\n    id : { type : Sequelize.INTEGER, primary : true },\r\n    data : { type : Sequelize.BLOB }\r\n} );\r\n\r\nvar promise = sequelize.sync( { force : true } );\r\n\r\nvar TESTED = works;//doesNotWorks;                                                                                                                                                                                 \r\n\r\nfor ( var t = 0; t < 1000; ++ t ) {\r\n    promise = promise.then( function ( ) { return TESTED( Foo,\r\n        { id : 0 }, { data : buffer }\r\n    ); } );\r\n}\r\n\r\npromise.catch( function ( error ) {\r\n    console.log( error.stack || error );\r\n} );\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/comments/102931359","html_url":"https://github.com/sequelize/sequelize/issues/3137#issuecomment-102931359","issue_url":"https://api.github.com/repos/sequelize/sequelize/issues/3137","id":102931359,"user":{"login":"arcanis","id":1037931,"avatar_url":"https://avatars.githubusercontent.com/u/1037931?v=3","gravatar_id":"","url":"https://api.github.com/users/arcanis","html_url":"https://github.com/arcanis","followers_url":"https://api.github.com/users/arcanis/followers","following_url":"https://api.github.com/users/arcanis/following{/other_user}","gists_url":"https://api.github.com/users/arcanis/gists{/gist_id}","starred_url":"https://api.github.com/users/arcanis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arcanis/subscriptions","organizations_url":"https://api.github.com/users/arcanis/orgs","repos_url":"https://api.github.com/users/arcanis/repos","events_url":"https://api.github.com/users/arcanis/events{/privacy}","received_events_url":"https://api.github.com/users/arcanis/received_events","type":"User","site_admin":false},"created_at":"2015-05-18T06:10:43Z","updated_at":"2015-05-18T06:10:43Z","body":"@BridgeAR I don't create any buffer except the initial one :)\r\n\r\nIf a buffer is created without being released, it can't come from this program and must come from either sequelize or pg. You can test this fairly easily by replacing the findOrCreate call by a setTimeout-based promise."}}