What causes swap files to be created?

As part of the program I wrote, I constantly read and write data from files. I noticed that as part of doing so, I am inadvertently creating swap .swp files.

What do you think is going on? What would cause swap files to appear if you had to reproduce the problem?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The .swp file is not a swap file in the OS sense. It is a state file. It keeps your changes since the last save (except the last 200 characters), buffers that you have saved, unsaved macros and the undo structure.

You can read more in VIM’s help: vim +help swap-file. If there is a crash (power failure, OS crash, etc.), then you can recover your changes using this swap-file. After saving the changes from the swap file to the original file, you will need to exit vim and remove the swap file yourself.

Method 2

.swp files are created by Vim, they should be automatically deleted when you close the file and exit the editor.

Method 3

.swp files are nothing but a kind of lock file which you editor, generally vim, creates to indicate that file is being edited. This way if you open the file in another vim instance of if someone in the network did that, they’ll see a warning that the file is being edited.
You need not to delete them manually. You editor will remove the swap file once you close the file in your editor.

Method 4

I just executed a mv command on a 31GB file and a 13GB .swp file was created, no editing involved.

mv X.csv Y.csv created Y.csv and .Y.csv.swp

CentOS 6.5, ext4 file system


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x