# Mmap.mmap leaves the file open

**URL:** https://discourse.julialang.org/t/mmap-mmap-leaves-the-file-open/19413
**Category:** New to Julia
**Tags:** mmap
**Created:** [January 9, 2019, 7:19am UTC](https://discourse.julialang.org/t/mmap-mmap-leaves-the-file-open/19413 "2019-01-09T07:19:03Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![1112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1112/32/9325_2.png) [@1112](https://discourse.julialang.org/u/1112)
#### Post date: [January 9, 2019, 7:19am UTC](https://discourse.julialang.org/t/mmap-mmap-leaves-the-file-open/19413/1 "2019-01-09T07:19:03Z")

</div>

Hello, here is my code

```julia
open(filename, "w+") do io
			C = Mmap.mmap(io, BitArray, (64,10^3));
			BinX = getHash(r.x)
			for p=1:length(BinX)
				C[p, 1] = parse(Int, BinX[p])
			end
			Mmap.sync!(C);		
end

```

After I try to open or delete the created file, but I receive a message that the file is occupied by another program. How to close a file?

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [January 9, 2019, 9:26am UTC](https://discourse.julialang.org/t/mmap-mmap-leaves-the-file-open/19413/2 "2019-01-09T09:26:55Z")

</div>

Most likely, `C` hasn’t been garbage collected. First make sure you’re not storing the return value somewhere (the fact that you need that semicolon means this block returns `C` to the caller). Then try calling `GC.gc()`.

---

<div class="post-metadata">

### Author: ![1112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1112/32/9325_2.png) [@1112](https://discourse.julialang.org/u/1112)
#### Post date: [January 9, 2019, 9:29am UTC](https://discourse.julialang.org/t/mmap-mmap-leaves-the-file-open/19413/3 "2019-01-09T09:29:25Z")

</div>

> [@tim.holy](#):
>
> `GC.gc()` .

I tried GC.gc (), but the result is the same, with the exception of performance degradation.

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [January 9, 2019, 9:37am UTC](https://discourse.julialang.org/t/mmap-mmap-leaves-the-file-open/19413/4 "2019-01-09T09:37:39Z")

</div>

A runnable example might help (your example references lots of variables that we don’t have). Also details about your platform: Windows is quite different from Linux, for example.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

### Author: ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)
#### Post date: [January 9, 2019, 5:03pm UTC](https://discourse.julialang.org/t/mmap-mmap-leaves-the-file-open/19413/5 "2019-01-09T17:03:37Z")

</div>

I’m not entirely sure whether this works, but you could also try to call `finalize(C)` after the `Mmap.sync!(C);` call.

---

<div class="post-metadata">

### Author: ![1112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1112/32/9325_2.png) [@1112](https://discourse.julialang.org/u/1112)
#### Post date: [January 10, 2019, 5:22am UTC](https://discourse.julialang.org/t/mmap-mmap-leaves-the-file-open/19413/6 "2019-01-10T05:22:31Z")

</div>

Sorry, really GC.gc () helped
