V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
A3m0n
V2EX  ›  macOS

启动终端的时候速度特别慢是什么原因?

  •  2
     
  •   A3m0n · 2022-03-21 15:53:55 +08:00 · 2945 次点击
    这是一个创建于 962 天前的主题,其中的信息可能已经有所发展或是发生改变。

    启动终端的时候速度特别慢是什么原因?

    启动的时候标题栏有 bash-zsh 等交替出现,具体可以看下图:

    test.gif

    大概持续 10 多秒才会能真正进入(动图的速度比实际速度慢)。

    用 iTerm 也是一样的问题,ssh 连接也是。

    网上查到的一些 nvm 相关的方法也尝试过了,没有效果。

    贴一下 .zshrc

    # Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
    # Initialization code that may require console input (password prompts, [y/n]
    # confirmations, etc.) must go above this block; everything else may go below.
    if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
      source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
    fi
    
    # If you come from bash you might have to change your $PATH.
    # export PATH=$HOME/bin:/usr/local/bin:$PATH
    
    # Path to your oh-my-zsh installation.
    export ZSH="/Users/aemon/.oh-my-zsh"
    
    # Set name of the theme to load --- if set to "random", it will
    # load a random theme each time oh-my-zsh is loaded, in which case,
    # to know which specific one was loaded, run: echo $RANDOM_THEME
    # See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
    # ZSH_THEME="jonathan"
    # ZSH_THEME="random"
    ZSH_THEME="powerlevel10k/powerlevel10k"
    
    # Set list of themes to pick from when loading at random
    # Setting this variable when ZSH_THEME=random will cause zsh to load
    # a theme from this variable instead of looking in $ZSH/themes/
    # If set to an empty array, this variable will have no effect.
    # ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )
    
    # Uncomment the following line to use case-sensitive completion.
    # CASE_SENSITIVE="true"
    
    # Uncomment the following line to use hyphen-insensitive completion.
    # Case-sensitive completion must be off. _ and - will be interchangeable.
    # HYPHEN_INSENSITIVE="true"
    
    # Uncomment the following line to disable bi-weekly auto-update checks.
    # DISABLE_AUTO_UPDATE="true"
    
    # Uncomment the following line to automatically update without prompting.
    # DISABLE_UPDATE_PROMPT="true"
    
    # Uncomment the following line to change how often to auto-update (in days).
    # export UPDATE_ZSH_DAYS=13
    
    # Uncomment the following line if pasting URLs and other text is messed up.
    # DISABLE_MAGIC_FUNCTIONS="true"
    
    # Uncomment the following line to disable colors in ls.
    # DISABLE_LS_COLORS="true"
    
    # Uncomment the following line to disable auto-setting terminal title.
    # DISABLE_AUTO_TITLE="true"
    
    # Uncomment the following line to enable command auto-correction.
    # ENABLE_CORRECTION="true"
    
    # Uncomment the following line to display red dots whilst waiting for completion.
    # Caution: this setting can cause issues with multiline prompts (zsh 5.7.1 and newer seem to work)
    # See https://github.com/ohmyzsh/ohmyzsh/issues/5765
    # COMPLETION_WAITING_DOTS="true"
    
    # Uncomment the following line if you want to disable marking untracked files
    # under VCS as dirty. This makes repository status check for large repositories
    # much, much faster.
    # DISABLE_UNTRACKED_FILES_DIRTY="true"
    
    # Uncomment the following line if you want to change the command execution time
    # stamp shown in the history command output.
    # You can set one of the optional three formats:
    # "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
    # or set a custom format using the strftime function format specifications,
    # see 'man strftime' for details.
    # HIST_STAMPS="mm/dd/yyyy"
    
    # Would you like to use another custom folder than $ZSH/custom?
    # ZSH_CUSTOM=/path/to/new-custom-folder
    
    # Which plugins would you like to load?
    # Standard plugins can be found in $ZSH/plugins/
    # Custom plugins may be added to $ZSH_CUSTOM/plugins/
    # Example format: plugins=(rails git textmate ruby lighthouse)
    # Add wisely, as too many plugins slow down shell startup.
    plugins=(git zsh-autosuggestions)
    
    source $ZSH/oh-my-zsh.sh
    
    # User configuration
    
    # export MANPATH="/usr/local/man:$MANPATH"
    
    # You may need to manually set your language environment
    # export LANG=en_US.UTF-8
    
    # Preferred editor for local and remote sessions
    # if [[ -n $SSH_CONNECTION ]]; then
    #   export EDITOR='vim'
    # else
    #   export EDITOR='mvim'
    # fi
    
    # Compilation flags
    # export ARCHFLAGS="-arch x86_64"
    
    # Set personal aliases, overriding those provided by oh-my-zsh libs,
    # plugins, and themes. Aliases can be placed here, though oh-my-zsh
    # users are encouraged to define aliases within the ZSH_CUSTOM folder.
    # For a full list of active aliases, run `alias`.
    #
    # Example aliases
    # alias zshconfig="mate ~/.zshrc"
    # alias ohmyzsh="mate ~/.oh-my-zsh"
    
    export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
    
    echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/aemon/.zprofile
    eval "$(/opt/homebrew/bin/brew shellenv)"
    
    # Created by mirror-config-china
    export IOJS_ORG_MIRROR=https://npm.taobao.org/mirrors/iojs
    export NODIST_IOJS_MIRROR=https://npm.taobao.org/mirrors/iojs
    export NVM_IOJS_ORG_MIRROR=https://npm.taobao.org/mirrors/iojs
    export NVMW_IOJS_ORG_MIRROR=https://npm.taobao.org/mirrors/iojs
    export NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node
    export NODIST_NODE_MIRROR=https://npm.taobao.org/mirrors/node
    export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node
    export NVMW_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node
    export NVMW_NPM_MIRROR=https://npm.taobao.org/mirrors/npm
    # End of mirror-config-china
    # export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles
    # 如果你不是通过 Android Studio 安装的 sdk ,则其路径可能不同,请自行确定清楚
    export ANDROID_HOME=$HOME/Library/Android/sdk
    export PATH=$PATH:$ANDROID_HOME/emulator
    export PATH=$PATH:$ANDROID_HOME/tools
    export PATH=$PATH:$ANDROID_HOME/tools/bin
    export PATH=$PATH:$ANDROID_HOME/platform-tools
    
    # To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
    [[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
    

    使用了网上查询到的计算终端启动速度的命令:

    /usr/bin/time /bin/zsh -i -c exit
    

    结果如下:

    0.35 real         0.15 user         0.17 sys
    

    感觉光 zsh 的启动速度其实并不慢,那么在启动前的这几十秒到底在做什么呢?

    希望大家能帮帮忙。

    第 1 条附言  ·  2022-03-21 16:56:16 +08:00

    我研究了一下,发现是 /usr/bin/login 命令过慢引起的,想问一下大家运行这个命令的速度怎么样?

    我有什么办办法可以优化这个运行速度么?

    第 2 条附言  ·  2022-03-22 09:29:58 +08:00

    问题解决了,感谢@Kobayashi

    echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/aemon/.zprofile
    

    是这一句导致 . zprofile 文件一天比一天大,最后 zsh 启动速度越来越慢。

    删除了这一句,并清空了 .zprofile 文件后,问题不再出现。

    再次感谢 @Kobayashi

    12 条回复    2022-03-22 09:26:19 +08:00
    minbaby
        1
    minbaby  
       2022-03-21 15:59:39 +08:00
    # 这个扔到 .zshrc 最前边
    if [[ "$ZPROF" = true ]]; then
    zmodload zsh/zprof
    fi

    #####################
    # prof zsh
    #####################
    profzsh() {
    shell=${1-$SHELL}
    ZPROF=true $shell -i -c exit
    }
    if [[ "$ZPROF" = true ]]; then
    zprof
    fi

    # 可以看 zsh 不同函数的执行时间
    minbaby
        2
    minbaby  
       2022-03-21 16:00:54 +08:00
    如果你用了什么 nvm ,之类的东西,会导致 zsh 启动特别慢。。。可以改一下按需加载
    mcluyu
        3
    mcluyu  
       2022-03-21 16:00:58 +08:00
    可能跟 them 有关吧, 可以把 theme 先去掉试试,export 过多? 不太懂
    A3m0n
        4
    A3m0n  
    OP
       2022-03-21 16:05:07 +08:00
    @minbaby 我加上去了,没看到有执行时间的输出,应该去哪里看呢? nvm 我已经改过了,问题依旧。


    @mcluyu 现在是使用了 p10k 的样式,但是有试过卸载,问题依旧。
    A3m0n
        5
    A3m0n  
    OP
       2022-03-21 16:05:39 +08:00
    补充一个视频版本的,gif 图看起来慢一点。

    jaredyam
        6
    jaredyam  
       2022-03-21 16:10:43 +08:00
    注释下 export 看看是不是 export 加载的问题?如果是的话可以放到 login 时加载 export ,启动 terminal 时就会快很多。远程另说。
    crysislinux
        7
    crysislinux  
       2022-03-21 16:14:11 +08:00 via Android
    nvm 感觉怎么改最后体验都不会好,先试试 fnm 或者 n 替换 nvm
    minbaby
        8
    minbaby  
       2022-03-21 16:32:03 +08:00
    @A3m0n 加上去之后执行 profzsh 呀
    A3m0n
        9
    A3m0n  
    OP
       2022-03-21 16:44:16 +08:00
    @minbaby 一个函数都没有。

    profzsh
    num calls time self name
    -----------------------------------------------------------------------------------
    Kobayashi
        10
    Kobayashi  
       2022-03-21 22:19:54 +08:00 via Android   ❤️ 1
    1. 懒加载 nvm ,或者换到 asdf 、nodenv 等使用 shim 机制的 node 管理器。
    2. 干掉 brew shellenv 。其实你的两行 brew shellenv 第一行完全不应该有,错的离谱,会导致 .zprofile 不断增大。直接把 /opt/homebrew/bin/brew shellenv 的输出结果替换掉现有的两行。
    Kobayashi
        11
    Kobayashi  
       2022-03-21 22:20:50 +08:00 via Android
    @Kobayashi 和 login 没什么关系,你可能没搞清 login 命令,login 基本就是在 zsh -il.
    A3m0n
        12
    A3m0n  
    OP
       2022-03-22 09:26:19 +08:00
    @Kobayashi 万分感谢!问题确实出在 brew shellenv ,我去除了第一行 echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/aemon/.zprofile ,然后清空了 .zprofile 成功解决了问题。

    nvm 我没去动它,没什么问题。

    万分感谢!!!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1039 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 19:48 · PVG 03:48 · LAX 11:48 · JFK 14:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.