12

I would like to restart my zsh session, because I keep one persistent tmux session and change ~/.zshrc often and do source ~/.zshrc. However I realized that this gets slower over time (eg. for i inseq 50; do source ~/.zshrc; echo "a"; done starts printing 'a' s fast and gets slower quickly).

I read the suggestions here to restart zsh and the suggestion is to simply run zsh or zsh -l. However if I do that, I create a 'nested' zsh session, if I understood it correctly. By that I mean:

# Simulate slowed zsh session
for i in `seq 50`; do source ~/.zshrc; echo "a"; done
# use zsh to make it faster "child" zsh
zsh
# confirm fast
source ~/.zshrc; # fast
# revert back to "parent" zsh
exit
# confirm old slow session is still there
source ~/.zshrc; # slow

I have a tmux session with multiple windows and a command history that I care to keep persistent. That's why I am looking for a sustainable solution.

Bonus question: does anyone have any idea why source ~/.zshrc might be slowing down?

# Path to your oh-my-zsh installation.
export ZSH="/Users/username/.oh-my-zsh"

ZSH_THEME="themename"

# Which plugins would you like to load?
# Standard plugins can be found in ~/.oh-my-zsh/plugins/*
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
  git
)

source $ZSH/oh-my-zsh.sh


# activate zsh-syntax-highlighting (brew install zsh-syntax-highlighting)
source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

function proxyON() {
...redacted
}

function proxyOFF(){
 http_proxy=
 https_proxy=
 HTTP_PROXY=
 HTTPS_PROXY=
 export http_proxy https_proxy HTTP_PROXY HTTPS_PROXY
}

function nukeDS_Store(){
 find ~/Projects/mys/ -name '.DS_Store' -delete
}

function reload-ssh() {
   ssh-add -e /Library/OpenSC/lib/opensc-pkcs11.so >> /dev/null
   if [ $? -gt 0 ]; then
       echo "Failed to remove previous card"
   fi
   ssh-add -s /Library/OpenSC/lib/opensc-pkcs11.so 
}

alias fastBuild='mvn install --offline -DskipTests=true'

## History Settings
# set history size
export HISTSIZE=1000000
#save history after logout
export SAVEHIST=1000000
##history file
export HISTFILE=~/.zhistory
##save only one command if 2 common are same and consistent
setopt HIST_IGNORE_DUPS
##add timestamp for each entry
setopt EXTENDED_HISTORY   
##have seperate history for each
setopt nosharehistory
##dont append into history file
setopt NOINC_APPEND_HISTORY


# Set java version
export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_191`


# Maven
export M3_HOME="/Applications/apache-maven-3.6.0" # replace n.n.n with appropriate version
export M3=$M3_HOME/bin
export PATH=$M3:$PATH

## set node version
export PATH="/usr/local/opt/node@8/bin:$PATH"

## pic-tools
source /Projects/pic-tools/scripts/*.env

3
  • 1
    What is in your .zshrc?
    – slhck
    Jun 27, 2019 at 9:33
  • added the content
    – ozgeneral
    Jun 27, 2019 at 9:39
  • one thing I noticed was, path kept growing. I made sure it doesnt, but that wasnt enough
    – ozgeneral
    Jun 27, 2019 at 9:40

1 Answer 1

19

Just replace you're running zsh instance with a new one:

exec zsh

exec is a shell builtin command with the purpose to (see zshbuiltinsman page):

Replace the current shell with command rather than forking.

Why it is getting slower... my first speculation would be, that you redefine PATH in your zshrc, with maybe one directory on a rather slow drive. So after every time you source your zshrc, your search path gets longer and longer. And every time zsh has to rehash more and more directories...

Please read my answer to another question how to improve that situation.

8
  • Yep, this works. About the path, I adjusted that and retried but turns out that is not the real cause.
    – ozgeneral
    Jun 27, 2019 at 9:47
  • @ozgeneral: As you posted the contents of your zshrc in the meantime... one line immediately catches my eye: source $ZSH/oh-my-zsh.sh -- I don't know the internas of oh-my-zsh but I know it can do a lot of time consuming stuff, maybe there are also some arrays involved, which are loop over and over again. Maybe you can start tackling this down by comparing the output of env before and after sourcing zshrc again.
    – mpy
    Jun 27, 2019 at 11:34
  • I was also suspecting that, and I'm also not sure about what plugins=(git) does. I compared the environments before and after, they are the same -except the path which is now fixed-
    – ozgeneral
    Jun 27, 2019 at 11:48
  • @ozgeneral: Just to clarify, I meant that: env > old; source ~/.zshrc; env > new; diff old new. Maybe we had a missunderstanding...
    – mpy
    Jun 27, 2019 at 12:31
  • no, I have done exactly that :)
    – ozgeneral
    Jun 27, 2019 at 12:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .