Yuchen Cao


  • Home

  • Archives

JVM Memory Space

Posted on 2019-09-11 | In Engineering

I start to write this post because I have been assigned a ticket at work. My mentor wanted me to collect some heap stats when the JVM old-gen memory usage is beyond threshold%. When I first lookat at this ticket, what is old-gen? So I began searching for more info about it and write a post to help me understand and review in the future.

JVM memory space is divided into two parts: HEAP and non-HEAP.

HEAP space could be divided into three parts: Eden Space, Survivor Space, Old-gen.

Non-HEAP space could be divided into Code Cache, Perm Gen, JVM Stack, Local Method Stack.

Eden Space: As the meaning of the name, when an object is created, it is placed into this space at first. After GC(garbage collection), objects which cannot be reclaimed would be thrown into Survivor Space.

Survivor Space: As we can see from Eden Space, objects which cannot be reclaimed in Eden Space during GC would be put in this space. There are two Survivor Space: To Survivor and From Survivor, with the same size. To Survivor would always be empty. Objects cannot be reclaimed during GC would be thrown into To Survivor. These objects came from not only Eden Space, but also From Survivor. You may be curious that I have mentioned To Survivor would always be empty. But we just throw some objects into it. This is because after each iteration, To Survivor and From Survivor would exchange to make sure To Survivor would always be empty.

Eden Space and Survivor Space are both classified into New-gen. GC in New-gen is called Minor GC or Young GC. The age of objects would be recorded.

Old-gen is to store objects that are still alive after many GC. Also large objects that cannot be allocated would enter Old-gen directly. Once Olg-gen is full, virtual machine would do a GC called Major GC. It is also called Full GC because it will scan the whole heap and reclaim.

The size of HEAP = the size of Old-gen + the size of New-gen.

Code cache is used to store the code compiled by JIT. Its size is set as 32M by default in client mode, 48M in server mode. It could be changed by changing JVM parameter. Code cache may be full and if it was full, there would be a warning message.

Perm Gen is to store the information about Class and Meta. Class would be put into this space when loaded. It would be never be reclaimed during GC.

Zsh Config

Posted on 2019-08-26 | In Engineering

Life is short! Use Zsh!
First check if your system has zsh and what shell is your system is using.

1
2
3
4
5
6
7
8
9
10
11
12
13
$ cat /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
$ echo $SHELL
/bin/bash

If your system has zsh already, we are fine. If not, you shall go to install zsh first.
https://github.com/robbyrussell/oh-my-zsh/wiki/Installing-ZSH

1
$ chsh -s /bin/zsh

Then you can logout and open a new terminal.

1
2
$ echo $SHELL
/bin/zsh

Next, we are going to install oh-my-zsh. Check it on the github README, Basic Installation.
https://github.com/robbyrussell/oh-my-zsh

After install oh-my-zsh, we need to change the config ~/.zshrc to make your zsh more powerful. I feel the most useful is auto-completion. Here is my personal zsh config.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Antigen: https://github.com/zsh-users/antigen
ANTIGEN="$HOME/.local/bin/antigen.zsh"

# Install antigen.zsh if not exist
if [ ! -f "$ANTIGEN" ]; then
echo "Installing antigen ..."
[ ! -d "$HOME/.local" ] && mkdir -p "$HOME/.local" 2> /dev/null
[ ! -d "$HOME/.local/bin" ] && mkdir -p "$HOME/.local/bin" 2> /dev/null
[ ! -f "$HOME/.z" ] && touch "$HOME/.z"
URL="http://git.io/antigen"
TMPFILE="/tmp/antigen.zsh"
if [ -x "$(which curl)" ]; then
curl -L "$URL" -o "$TMPFILE"
elif [ -x "$(which wget)" ]; then
wget "$URL" -O "$TMPFILE"
else
echo "ERROR: please install curl or wget before installation !!"
exit
fi
if [ ! $? -eq 0 ]; then
echo ""
echo "ERROR: downloading antigen.zsh ($URL) failed !!"
exit
fi;
echo "move $TMPFILE to $ANTIGEN"
mv "$TMPFILE" "$ANTIGEN"
fi


# Initialize command prompt
export PS1="%n@%m:%~%# "

# Initialize antigen
source "$ANTIGEN"

# Load local bash/zsh compatible settings
[ -f "$HOME/.local/etc/init.sh" ] && source "$HOME/.local/etc/init.sh"


# Initialize oh-my-zsh
antigen use oh-my-zsh


# default bundles
# visit https://github.com/unixorn/awesome-zsh-plugins
antigen bundle git
antigen bundle heroku
antigen bundle pip
antigen bundle svn-fast-info
# antigen bundle command-not-find

antigen bundle colorize
antigen bundle github
antigen bundle python
antigen bundle z

antigen bundle zsh-users/zsh-autosuggestions
antigen bundle zsh-users/zsh-completions
antigen bundle supercrabtree/k
antigen bundle Vifon/deer

# uncomment the line below to enable theme
antigen theme avit


# check login shell
if [[ -o login ]]; then
[ -f "$HOME/.local/etc/login.sh" ] && source "$HOME/.local/etc/login.sh"
[ -f "$HOME/.local/etc/login.zsh" ] && source "$HOME/.local/etc/login.zsh"
fi

# syntax color definition
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)

typeset -A ZSH_HIGHLIGHT_STYLES

# ZSH_HIGHLIGHT_STYLES[command]=fg=white,bold
# ZSH_HIGHLIGHT_STYLES[alias]='fg=magenta,bold'

ZSH_HIGHLIGHT_STYLES[default]=none
ZSH_HIGHLIGHT_STYLES[unknown-token]=fg=009
ZSH_HIGHLIGHT_STYLES[reserved-word]=fg=009,standout
ZSH_HIGHLIGHT_STYLES[alias]=fg=cyan,bold
ZSH_HIGHLIGHT_STYLES[builtin]=fg=cyan,bold
ZSH_HIGHLIGHT_STYLES[function]=fg=cyan,bold
ZSH_HIGHLIGHT_STYLES[command]=fg=white,bold
ZSH_HIGHLIGHT_STYLES[precommand]=fg=white,underline
ZSH_HIGHLIGHT_STYLES[commandseparator]=none
ZSH_HIGHLIGHT_STYLES[hashed-command]=fg=009
ZSH_HIGHLIGHT_STYLES[path]=fg=214,underline
ZSH_HIGHLIGHT_STYLES[globbing]=fg=063
ZSH_HIGHLIGHT_STYLES[history-expansion]=fg=white,underline
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]=none
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]=none
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]=none
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]=fg=063
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]=fg=063
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]=fg=009
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]=fg=009
ZSH_HIGHLIGHT_STYLES[assign]=none

# load local config
[ -f "$HOME/.local/etc/config.zsh" ] && source "$HOME/.local/etc/config.zsh"
[ -f "$HOME/.local/etc/local.zsh" ] && source "$HOME/.local/etc/local.zsh"

# enable syntax highlighting
antigen bundle zsh-users/zsh-syntax-highlighting

antigen apply

# setup for deer
autoload -U deer
zle -N deer

# default keymap
bindkey -s '\ee' 'vim\n'
bindkey '\eh' backward-char
bindkey '\el' forward-char
bindkey '\ej' down-line-or-history
bindkey '\ek' up-line-or-history
bindkey '\eu' undo
bindkey '\eH' backward-word
bindkey '\eL' forward-word
bindkey '\eJ' beginning-of-line
bindkey '\eK' end-of-line

bindkey -s '\eo' 'cd ..\n'
bindkey -s '\e;' 'lk\n'

bindkey '\e[1;3D' backward-word
bindkey '\e[1;3C' forward-word
bindkey '\e[1;3A' beginning-of-line
bindkey '\e[1;3B' end-of-line

bindkey '\ev' deer

alias lk='k --no-vcs'

# alias atom='/Applications/Atom.app/Contents/MacOS/Atom'
# alias subl='/Applications/SublimeText.app/Contents/SharedSupport/bin/subl'
# alias code='/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code'

You can directly copy it and use it. Also if you have some environment variables settings in your previous shell, for example bash. You may look at your bash config file, such as ~/.bash_profile or ~/.bashrc and copy them into your zsh config. You may feel it very convenient to directly add a source ~/.bash_profile in your zsh config, but I suggest you do not do this.


Here is a problem I meet in my work. My work VM lost a lot of environment variables such as $JAVA_HOME when I switch to zsh, this is very strange because this environment variable is configured by IT and it should be automatically loaded despite which shell I am using.
I began searching and found it lives in a script in /etc/profile.d/.
If you look at /etc/profile, it will run the script in /etc/profile.d/ to load all the personal setups.

1
2
3
4
5
6
7
8
9
for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null 2>&1
fi
fi
done

When you login, /etc/profile should be started at first. However, zsh is unique, it will not load this. However, my colleague has never met this problem before. So we began searching why this happened. There must be a script zsh will run the scripts under /etc/profile.d.
After hours, we found the key problem. In /etc/zshrc, his file contains

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
_src_etc_profile_d()
{
# Make the *.sh things happier, and have possible ~/.zshenv options like
# NOMATCH ignored.
emulate -L ksh


# from bashrc, with zsh fixes
if [[ ! -o login ]]; then # We're not a login shell
for i in /etc/profile.d/*.sh; do
if [ -r "$i" ]; then
. $i
fi
done
unset i
fi
}
_src_etc_profile_d

However, my /etc/zshrc does not have this! Why?
Because my company is still using Centos 6. And I used

1
$ yum -y install zsh

Its version still begins with 4. Now the zsh version would begin with 5. After I downloaded the zsh with version 5, I could find /etc/zshrc has the function to load the scripts under /etc/profile and my problem was solved.

Life is short! Why use Centos 6!

Git Config

Posted on 2019-08-21 | In Engineering

When I started my first full-time software engineer job at Snowflake Computing, I felt that the speed of typing terminal commands is very annoying. Sometimes it took more than 2 seconds to finish the command. Finally I found the reason is that I was using zsh. Zsh has the git plugin and it will scan the dirty change after running each command. Snowflake Computing is using a big shared repository, so it is very expensive to scan the whole repo. The only thing I need to do is:

1
2
$ git config --global --add oh-my-zsh.hide-dirty 0
# --global is added for chaning the config for all git repos

Below is some personal shortcut of git usage. This may help save some time.

1
2
3
4
5
6
7
8
9
10
11
12
$ git config --global alias.br branch
$ git config --global alias.a add
$ git config --global alias.d diff
$ git config --global alias.ci commit
$ git config --global alias.st status
$ git config --global alias.s show
$ git config --global alias.pl pull
$ git config --global alias.ps push
$ git config --global alias.rb rebase
$ git config --global alias.cp cherry-pick
$ git config --global alias.l log
$ git config --global alias.co checkout
Yuchen Cao

Yuchen Cao

Software Engineer @ Snowflake Computing || Master of Computer Science @ Carnegie Mellon University || Bachlor of Mathematics @ Zhejiang University

3 posts
1 categories
5 tags
GitHub E-Mail FB Page YouTube Instagram Resume
© 2019 Yuchen Cao
Powered by Hexo
|
Theme — NexT.Pisces v5.1.4