-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
956 lines (603 loc) · 28.6 KB
/
index.html
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>shoyu of lysu</title>
<meta name="author" content="lysu">
<meta name="description" content="0. Purpose I’m a freshman to use gdb or c, because I mainly use java, but there is some situation for java programer to use gdb tools to solve …">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href="/atom.xml" rel="alternate" title="shoyu of lysu" type="application/atom+xml">
<link rel="canonical" href="">
<link href="/favicon.png" rel="shortcut icon">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
<!--[if lt IE 9]><script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<script async="true" src="//libs.baidu.com/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<header id="header" class="inner"><h1><a href="/">shoyu of lysu</a></h1>
<nav id="main-nav"><ul class="main">
<li><a href="/">Blog</a></li>
<li><a href="/blog/archives">Archives</a></li>
</ul>
</nav>
<nav id="mobile-nav">
<div class="alignleft menu">
<a class="button">Menu</a>
<div class="container"><ul class="main">
<li><a href="/">Blog</a></li>
<li><a href="/blog/archives">Archives</a></li>
</ul>
</div>
</div>
<div class="alignright search">
<a class="button"></a>
<div class="container">
<form action="https://www.google.com/search" method="get">
<input type="text" name="q" results="0">
<input type="hidden" name="q" value="site:lysu.github.io">
</form>
</div>
</div>
</nav>
<nav id="sub-nav" class="alignright">
<div class="social">
<a class="github" href="https://github.com/lysu" title="GitHub">GitHub</a>
<a class="rss" href="/atom.xml" title="RSS">RSS</a>
</div>
<form class="search" action="https://www.google.com/search" method="get">
<input class="alignright" type="text" name="q" results="0">
<input type="hidden" name="q" value="site:lysu.github.io">
</form>
</nav>
</header>
<div id="content" class="inner">
<article class="post">
<h2 class="title">
<a href="/blog/2015/03/14/introduce-to-gdb/">
Introduce to Gdb</a>
</h2>
<div class="entry-content">
<h3>0. Purpose</h3>
<p>I’m a freshman to use gdb or c, because I mainly use java, but there is some situation for java programer to use gdb tools to solve problem. So I ‘mark’ this article~</p>
<h3>1. Debug Target</h3>
<p>gdb could debug many kinds of programming languages, like c, c++, objective-c, and so on. It doesn’t like debug tools in java, we often use it’s command-line interface instead of GUI.</p>
<p>Before we start to debug, we should take code compile with debug information.</p>
<p>for example, using <code>gcc</code> we should add <code>-g</code> flag</p>
<pre><code>$ gcc example.c -g -o example
</code></pre>
<p>This is the reason why if we want to debug jvm, we must build it with debug info first.</p>
<h3>2. Run as debug</h3>
<p>using</p>
<pre><code>$ gdb ./example
</code></pre>
<p>then we enter the gdb-commandline, and we can use <code>tab</code> key to complete or list all command</p>
<p>there are some basic command</p>
<table>
<thead>
<tr>
<th></th>
<th> command</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td> dir [directories]</td>
<td> set source code folder </td>
</tr>
<tr>
<td></td>
<td> r </td>
<td> run it </td>
</tr>
<tr>
<td></td>
<td> b [where] </td>
<td> add a break, where is an express like ‘xx_method’ or linenum </td>
</tr>
<tr>
<td></td>
<td> b [where] if [condition] </td>
<td> break when condition </td>
</tr>
<tr>
<td></td>
<td> l </td>
<td> list code </td>
</tr>
<tr>
<td></td>
<td>step </td>
<td> step into a method </td>
</tr>
<tr>
<td></td>
<td>c</td>
<td> continue </td>
</tr>
<tr>
<td></td>
<td>finish</td>
<td> finish current method </td>
</tr>
<tr>
<td></td>
<td>p [var name]</td>
<td>watch a variable value </td>
</tr>
<tr>
<td></td>
<td>x/? [address] </td>
<td> watch the memory address </td>
</tr>
<tr>
<td></td>
<td>dump memory [file] [sadd] [eadd] </td>
<td> dump memory between to address into file </td>
</tr>
<tr>
<td></td>
<td>info thread </td>
<td> list threads </td>
</tr>
<tr>
<td></td>
<td>info break </td>
<td> list all break </td>
</tr>
<tr>
<td></td>
<td>thread [tid]</td>
<td> switch thread </td>
</tr>
<tr>
<td></td>
<td>bt </td>
<td> show stacktrace </td>
</tr>
<tr>
<td></td>
<td>[enter]</td>
<td> redo last command </td>
</tr>
</tbody>
</table>
<p>and so on…</p>
<h3>2. Attach to running process</h3>
<p>using</p>
<pre><code>$ gdb ./example [pid]
</code></pre>
<p>to attach to pid process..</p>
<h3>3. Run with coredump file</h3>
<p>using</p>
<pre><code>$ gdb ./example [coredump file path]
</code></pre>
<p>the <a href="http://lysu.github.io/blog/2015/03/07/Troubleshooting-tools/">previous blog</a> tell some detail of debug coredump file.</p>
<p>Also see</p>
<ul>
<li><a href="http://blog.csdn.net/haoel/article/details/2879">GDB调试程序</a></li>
</ul>
</div>
<div class="meta">
<div class="date">
<time datetime="2015-03-14T22:01:22+08:00" pubdate data-updated="true">Mar 14<span>th</span>, 2015</time></div>
<div class="tags">
<a class='category' href='/blog/categories/debug/'>debug</a>, <a class='category' href='/blog/categories/os/'>os,</a>
</div>
<div class="comments"><a href="/blog/2015/03/14/introduce-to-gdb/#disqus_thread">Comments</a></div>
</div>
</article>
<article class="post">
<h2 class="title">
<a href="/blog/2015/03/07/Troubleshooting-tools/">
Troubleshooting Tools</a>
</h2>
<div class="entry-content">
<h3>0. The purpose</h3>
<hr />
<p>There are too many tools for us to track the system problem. It’s just like a detector’s work, at first, we must find ‘evidence’ as quickly as possible. Beside the awareness, using the right tools for problem is vital. Let’s record it in here.</p>
<h3>1. Core dump</h3>
<hr />
<h4>when</h4>
<p>First ‘tool’ is not a tools, it’s just a snapshot file that record memory/processor/register and so on when our program crashed as OS reason. Just like javadump file, it’s very useful for us diagnose and debug the problem.</p>
<p>When can we get a core dump file?</p>
<p>Linux use signal as one kind of asynchronous event handling mechanism, each signal has its default action, like Ingore (ignore signal ), Stop(suspend process), Terminate (terminate process), and Core (termination and core dump) etc..</p>
<p>So when <code>Core</code> action is triggered, linux will generate core dump.</p>
<table>
<thead>
<tr>
<th>Signal</th>
<th>Action</th>
<th>Addition</th>
</tr>
</thead>
<tbody>
<tr>
<td>SIGQUIT</td>
<td> CORE </td>
<td> Quit from keyboard, e.g. <code>"Ctrl+\"</code></td>
</tr>
<tr>
<td>SIGILL</td>
<td> CORE </td>
<td> Illegal Instruction, e.g. <code>kill -ll $$</code></td>
</tr>
<tr>
<td>SIGABRT</td>
<td>CORE</td>
<td>Abort signal from abort </td>
</tr>
<tr>
<td>SIGSEGV</td>
<td>CORE</td>
<td>Invalid memory reference, e.g. write to null pointer memory area or overflowstack</td>
</tr>
<tr>
<td>SIGTRAP</td>
<td>CORE</td>
<td>Trace/breakpoint trap </td>
</tr>
</tbody>
</table>
<p>For full sigal list, plean see <a href="http://man7.org/linux/man-pages/man7/signal.7.html">signal manual page</a>. And signal we show in core dump to help using recognize the problem type.</p>
<h4>how</h4>
<h5>I. enable configuation</h5>
<p>En~…But we often hear that ‘I could not find any core dump when it crashed’.</p>
<p>There some <code>switcher</code> to let system generate a core dump.</p>
<p>Enter <code>ulimit -c</code> command and get the result value as <code>0</code>, it indicate that core dump is disabled by default, it would not generate core dump file.
We can use the command <code>ulimit -c unlimited</code> to enable the core dump function, and does not limit the core dump file size;</p>
<p>Using the above command will only effective for terminal current environment, if you want to be permanent, you can modify the file /etc/security/limits.conf file</p>
<pre><code>#/etc/security/limits.conf
#Each line describes a limit for a user in the form:
#<domain> <type> <item> <value>
* soft core unlimited
</code></pre>
<h5>II. dump file path</h5>
<ul>
<li>The default generated core file is saved in the executable file’s directory, file name is core.</li>
<li>Using <code>sysctl-a |grep core</code>, and <code>kernel.core_pattern</code> will indiate the dump file path, and <code>kernel.core_uses_pid</code> using <code>1</code> to let core file contains process id</li>
<li>To modify it using <code>sysctl -w</code> or <code>sysctl -p</code> as sudoer.</li>
</ul>
<h5>III. debug core file</h5>
<p>use the command <code>gdb [program] [coredump]</code> to view the core file</p>
<pre><code>vagrant@vagrant-ubuntu-trusty:~/test$ gdb seg core
GNU gdb (Ubuntu 7.8-1ubuntu4) 7.8.0.20141001-cvs
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from seg...(no debugging symbols found)...done.
[New LWP 12887]
Core was generated by `./seg'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x0000000000400506 in main ()
(gdb) where
#0 0x0000000000400506 in main ()
(gdb) info frame
Stack level 0, frame at 0x7fff961538c0:
rip = 0x400506 in main; saved rip = 0x7f17f9d0eec5
Arglist at 0x7fff961538b0, args:
Locals at 0x7fff961538b0, Previous frame's sp is 0x7fff961538c0
Saved registers:
rbp at 0x7fff961538b0, rip at 0x7fff961538b8
(gdb) Quit
</code></pre>
<h5>IV. generate dump with gdb</h5>
<p>we can use <code>gdb</code> to generate a core dump easily..</p>
<p>first, connect to java pid</p>
<pre><code>gdb -q --pid=xxx
</code></pre>
<p>then</p>
<pre><code>(gdb) generate-core-file
</code></pre>
<p>at last, don’t forget to detail from process</p>
<pre><code>(gdb) detach
</code></pre>
<h5>V. for java process</h5>
<p>if core dump is come from java process</p>
<p>we can transform it to java heap file use <code>jmap</code></p>
<pre><code>jmap -dump:format=b,file=heap.hprof $JAVA_HOME/java core.xxx
</code></pre>
<p>then use tools like <code>MAT</code> to analysis memory.</p>
<p>or we can use <code>jstack</code> to see java stack</p>
<pre><code>jstack -m $JAVA_HOME/bin/java core.xxx
</code></pre>
<p>(ps: jstack maybe meet a <a href="http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7133122">bug</a> )</p>
<p>Also see</p>
<ul>
<li><a href="http://www.itsprite.com/how-to-enablegeneratedebug-core-dump-in-linux/">How to: Enable/Generate/Debug Core Dump In Linux?</a></li>
<li><a href="http://www.itsprite.com/linux-core-file-configuration/">Linux Core File Configuration</a></li>
</ul>
<h3>2. dmesg/messages</h3>
<hr />
<h4>when</h4>
<p>When process crashed, we need to confirm is it killed by oom-killer?</p>
<h4>how</h4>
<p>We can use <code>dmesg</code> and <code>/var/adm/messages.*</code> to see kernel messages.(dmesg for kernel ring buffer, and messages for all)</p>
<p>expect boot info…</p>
<p>oom-killer will in it, we can use</p>
<pre><code>sudo dmesg | grep java | grep -i oom-killer
</code></pre>
<p>to see confirm that is program killed by oom-killer.</p>
<p>Also see</p>
<ul>
<li><a href="http://blog.chinaunix.net/uid-29242873-id-3942763.html">Linux内核OOM机制的详细分析</a></li>
<li><a href="http://unix.stackexchange.com/questions/35851/whats-the-difference-of-dmesg-output-and-var-log-messages">What’s the difference of dmesg output and /var/log/messages?</a></li>
<li><a href="http://lysu.github.io/blog/2015/02/02/how-to-deal-with-non-heap-or-native-memory-leak/">How to Deal With Non-heap or Native Memory Leak</a></li>
</ul>
<h3>3. strace</h3>
<hr />
<h4>when</h4>
<p>“strace – trace system calls and signals”</p>
<p>En~…we can use <code>strace</code> to see system call and signals in program.</p>
<p>we can use <code>strace</code> to see param or return value of system call and signals.</p>
<h4>how</h4>
<h5>I. follow call and signals</h5>
<p>use strace command to see system call</p>
<pre><code>strace ./[program]
</code></pre>
<p>we will see system call param and return value and signals</p>
<pre><code>mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f90c0139000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f90c0137000
arch_prctl(ARCH_SET_FS, 0x7f90c0137740) = 0
mprotect(0x7f90bff19000, 16384, PROT_READ) = 0
mprotect(0x600000, 4096, PROT_READ) = 0
mprotect(0x7f90c0146000, 4096, PROT_READ) = 0
munmap(0x7f90c013a000, 39504) = 0
fstat(0, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 1), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f90c0143000
read(0, 0x7f90c0143000, 1024) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
--- SIGTERM {si_signo=SIGTERM, si_code=SI_USER, si_pid=13689, si_uid=1000} ---
+++ killed by SIGTERM +++
</code></pre>
<h5>II. count system call</h5>
<pre><code>strace -c ./[program]
</code></pre>
<p>we will see count as this</p>
<pre><code>vagrant@vagrant-ubuntu-trusty:~/test$ strace -c ./test_strace
^CProcess 13742 detached
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
32.09 0.000060 8 8 mmap
23.53 0.000044 15 3 fstat
14.44 0.000027 7 4 mprotect
9.63 0.000018 18 1 munmap
8.56 0.000016 8 2 open
6.95 0.000013 4 3 3 access
1.60 0.000003 3 1 read
1.07 0.000002 1 2 close
1.07 0.000002 2 1 execve
0.53 0.000001 1 1 brk
0.53 0.000001 1 1 arch_prctl
------ ----------- ----------- --------- --------- ----------------
100.00 0.000187 27 3 total
</code></pre>
<h5>III. other options</h5>
<table>
<thead>
<tr>
<th>option </th>
<th> effect </th>
</tr>
</thead>
<tbody>
<tr>
<td>-o </td>
<td> output result to file</td>
</tr>
<tr>
<td>-T </td>
<td> track the system call time</td>
</tr>
<tr>
<td>-p </td>
<td> trace a running procee, using <code>-p [pid]</code></td>
</tr>
<tr>
<td>-e </td>
<td> A qualifying expression which modifies which events to trace or how to trace them, e.g. <code>-e trace=signal</code> to only trace signal, or <code>-e write</code> write IO operation.</td>
</tr>
</tbody>
</table>
<p>Also see</p>
<ul>
<li><a href="http://linux.die.net/man/1/strace">strace – Linux man page</a></li>
<li><a href="http://www.ibm.com/developerworks/aix/library/au-unix-strace.html">Fun with strace and the GDB Debugger</a></li>
<li><a href="http://www.slideshare.net/jamesgolick/how-to-debug-anything">How to Debug Anything</a></li>
</ul>
<h3>4. ulimit</h3>
<hr />
<h3>when</h3>
<p>In linux, every process has its resource limit, like sub-process number, open file number, dump file size, etc..</p>
<p>If our process more than its max value will lead a crash.</p>
<h3>how</h3>
<p>Limit is in resource level, but using count is in user level.</p>
<p>so limit like ‘file number’ will use number that AProc open file number plus BProc open file number to check is over limit.</p>
<p>This is the reason why some software like ‘apache’ using standalone user to run it..</p>
<p>To see process limit, use</p>
<pre><code>cat /proc/[pid]/limits
</code></pre>
<p>what the limit value come from?</p>
<ul>
<li>init proc: set by kernal</li>
<li>system service: set by <code>setrlimit</code></li>
<li>shell proc: per login use <code>/etc/security/limits.conf</code> or set by pre <code>ulimit -Sx</code> command</li>
<li>shell executed: inherite from shell process</li>
</ul>
<p>To modify process limit..(only soft limit we can modify)</p>
<p>Use <code>ulimit</code> we can modify shell process limit and continue process forked from shell.</p>
<p>Use <code>echo -n \"Max processes=xx:yy\" >/proc/<pid>/limits</code></p>
<p>we can modify limit for running process(need root)</p>
<p>Also see:</p>
<ul>
<li><a href="http://lwn.net/Articles/475406/">CGroup Task Counter</a></li>
</ul>
</div>
<div class="meta">
<div class="date">
<time datetime="2015-03-07T16:05:00+08:00" pubdate data-updated="true">Mar 7<span>th</span>, 2015</time></div>
<div class="tags">
<a class='category' href='/blog/categories/os/'>os</a>
</div>
<div class="comments"><a href="/blog/2015/03/07/Troubleshooting-tools/#disqus_thread">Comments</a></div>
</div>
</article>
<article class="post">
<h2 class="title">
<a href="/blog/2015/02/02/how-to-deal-with-non-heap-or-native-memory-leak/">
How to Deal With Non-heap or Native Memory Leak</a>
</h2>
<div class="entry-content">
<h3>when question occur out of heap</h3>
<hr />
<p>This week, I meet two very strange memory problems. Althought they are in different system, they have the same appearance —– Java process use much more memory over that JVM option setted.(e.g. Jvm option use 1.5G, but in <code>top</code> command, java consume 2.9G memory).</p>
<h3>direct buffer memory</h3>
<hr />
<p>First system was a data-transfer one. It uses Netty heavily. When get the Heap dump we see many DirectByteBuffer objects, it’s one of signals that we have direct buffer memory problem…then we use java.nio.Bits(<a href="http://stackoverflow.com/questions/3908520/looking-up-how-much-direct-buffer-memory-is-available-to-java">see this</a>) to confirm that direct buffer memory increase quickly when many request came.</p>
<p>At last, we found that netty sender product much requst that over receiver’s ability. So, we recheck our netty configuaration, and set <code>WATER_MARK</code> option and check <code>channel.isWritable</code> before write data to confirm that Netty has avalible to handle that request, if not we will try it later. Then direct buffer memory is under our control.</p>
<h3>native memory</h3>
<hr />
<p>The other system was a Lib-scan one. It will scan many <code>.jar</code> file every day.</p>
<p>At first, we try to dump heap many times, but we can not found any footmark in heap dump.</p>
<p>So, we change tool and use <code>pmap -x [pid]</code> and <code>cat /proc/[pid]/smaps</code> to see java memory usage.</p>
<p>First, we see find many big <code>anon</code> blocks using 65500+ memory, like this..</p>
<pre><code>00007f8b00000000 65512 40148 40148 rwx-- [ anon ]
00007f8b03ffa000 24 0 0 ----- [ anon ]
00007f8b04000000 65520 59816 59816 rwx-- [ anon ]
00007f8b07ffc000 16 0 0 ----- [ anon ]
</code></pre>
<p>Then we using <code>gdb</code> to dump the content of these blocks</p>
<pre><code>gdp -pid [pid]
dump memory mem.bin 0x00007f8b00000000 0x00007f8b00000000+65512
</code></pre>
<p>Then let it visuable</p>
<pre><code>cat mem.bin | strings
</code></pre>
<p>It’s suprise that the block content is <code>MANIFEST.MD</code> file.. at this time, we start to suspect jar..</p>
<pre><code>pmap -x [pid] | grep '.jar' | sort -k6 | uniq -c
</code></pre>
<p>After do some statistics, we found that many scanned jar file using memory more then once and never give back it…It’s the problem.</p>
<p>Then open JVM code — <code>jdk/src/share/native/java/util/zip/zip_util.c</code> and <code>ZipFile.c</code> will found ZipFile/JarFile will use <code>mmap</code> and only free then when <code>close()</code> be called..</p>
<p>So, after recheck and fix unclosed <code>ZipFile/JarFile</code>.. The problem was solved.</p>
<p>UPDATE: except un-close Jar file, before jdk1.7 URLClassLoader close JarFile lazy also lead mmap leak… we can use 1.7+ or hack it like this <a href="http://snipplr.com/view/24224/class-loader-which-close-opened-jar-files/">http://snipplr.com/view/24224/class-loader-which-close-opened-jar-files/</a> or try System.gc</p>
<p>UPDATE: the other accessible way may be let JarFile/ZipFile doesn’t use mmap…we can use jvm opt <code>-Dsun.zip.disableMemoryMapping</code> to disable it.<a href="http://www.oracle.com/us/technologies/java/overview-156328.html">http://www.oracle.com/us/technologies/java/overview-156328.html</a></p>
<h3>Summary</h3>
<hr />
<p>Using heap dump, we can find java heap problem, but Java and Java-Libary also have chance to make non-heap memory leak.</p>
<p>When use nio-libary, we must take care the direct memory usage, using nio libary carefully, and should better let <code>java.nio.Bits</code> be monited(also this way is trick.)</p>
<p>In addition to <code>nio</code> problem, mis-used JNI api also lead the non-heap memory leak, too. We should try <code>pmap or /proc/[pid]/smaps</code> to found which jar/lib/stack/file might using much memory..then use <code>gdb</code> to watch content of blocks to help our analysis.</p>
</div>
<div class="meta">
<div class="date">
<time datetime="2015-02-02T00:07:15+08:00" pubdate data-updated="true">Feb 2<span>nd</span>, 2015</time></div>
<div class="tags">
<a class='category' href='/blog/categories/jvm/'>jvm</a>
</div>
<div class="comments"><a href="/blog/2015/02/02/how-to-deal-with-non-heap-or-native-memory-leak/#disqus_thread">Comments</a></div>
</div>
</article>
<article class="post">
<h2 class="title">
<a href="/blog/2014/11/23/tunning-cms-gc/">
Tunning CMS GC</a>
</h2>
<div class="entry-content">
<h3>Introduce to CMS Collector</h3>
<hr />
<p>‘CMS’(Concurrent Mark-Sweep) is a gc algorithm that widely used by ‘reponse-time critical’ application.</p>
<p>To young generation, minor gc, CMS will stop all application threads too, but ‘ParNew’ performs scan in multi-threads and scan young generation is quick, because it’s small.</p>
<p>To old generation, instead of stopping the application threads during full gc, it take more background threads to periodically scan old generation. application threads only pause when it is realy nesessary. so its overall pause time is mush less than collector like ‘thoughput collector’.</p>
<p>The trade-off is CMS will take more CPU usage to drive scan threads. In addition, background scan don’t compact old generation, so there will be many fragments in old generation. When CPU is shortage or too fragemented to allocate new object, CMS will trigger full gc to resolve the problem.</p>
<p>CMS is enabled by <code>-XX:+UseConcMarkSweepGC -XX:+UseParNewGC</code>.</p>
<h3>Common attention</h3>
<hr />
<h4>1. Never specify a heap that is larger than physical memory</h4>
<p>it will make system do swap to hard disk, it’s very very slow.</p>
<h4>2. Set both initial and max heap size to same value</h4>
<p>it will reduce the heap resize operation, although it will hold bigger heap for it’s real needing.</p>
<h4>3. Make a right size Generations</h4>
<p>Heap is divided to many regions as we know. The Larger young generation used, the less chances young gc will occur(although slower). The smaller old generation used more full gcs will take place.</p>
<h4>4. Make Perm Generation doesn’t full</h4>
<p>Resizing perm-gen will require a full-gc which is expensive.</p>
<h4>5. Control gc threads number</h4>
<p>set flag <code>-XX:ParallelGCThreads=X</code> will effects the number of threads used for young generation collect and stop-world-phase of old generation. Using more threads will lead to shortern stop time, it’s take more CPU usage in consequence.</p>
<h3>Attention for CMS</h3>
<hr />
<h4>1. minor gc</h4>
<p>The bigger young generation, the less gc will occur. The bigger young generation, the longer GC cycles will be take.</p>
<p>Survivor size and tenuring</p>
<p>TLABS..todo.</p>
<h4>2. concurrent cycle detail</h4>
<p>Concurrent cycle will be trigger when it’s sufficiently full.</p>
<p>It starts with <code>initial mark phase</code>, which stop application threads to find GC root object in heap.</p>
<p>The next is <code>mark phase</code> that will not stop application and run conncurrent with application</p>
<p>Then <code>preclean phase</code> will concurrent running too, then <code>abortable preclean phase</code> will be taken to wait until the young generation is about 50% full to reduce the posibility for ygc occurs before remark.</p>
<p>Then <code>remark phase</code> will stop application threads again.</p>
<p>The <code>sweep phase</code> will swap concurrently.</p>
<p>Concurrent cycle doesn’t collect young generation directly, but it will have at least one ygc because of abortable preclean phase.</p>
<h4>3. CMS failure</h4>
<h5>a. concurrent mode failure</h5>
<p>When ygc occurs and no enough room in old generaition for promoted objects. CMS will trigger a full gc.</p>
<h5>b. promotion fail for too fragements</h5>
<p>When ygc occur, but old-gen is too fragements for promented objects. in the middle of ygc(ParNew) will collect and compact entrie old generation, It will take much time then concurrent mode failure, because compact is expensive.</p>
<h5>c. full gc without concurrent failure</h5>
<p>Normally, CMS will never meet full gc, expect concurrent failure or too fragements, but when perm-gen full, will take full gc, too. so take care of PerGen.</p>
<h5>d. a race</h5>
<p>When old-gen was filled for <code>CMSInitiatingOccupancyFraction</code>(default 70%) percent, concurrent cycle start a race, CMS must complete scan and free object befor remainder(100% – CMSInitiatingOccupancyFraction) be filled up. If not will meet a concurrent mode failure..</p>
<h4>4. Solve concurrent failure problem</h4>
<h5>a. run concurrent cycle more often</h5>
<p>Set <code>-XX:+UseCMSInitiatingOccupancyOnly</code> and <code>-XX:CMSInitiatingOccupancyFraction=N</code> will control the time for cycle start.</p>
<p>Small CMSInitiatingOccupancyFraction value will let cycle start sooner. But too small value will let concurrent cycle consume much CPU time, and start time become uncontrol when CPU resource is shortage; and so much concurrent cycle may take overall pause time increased.</p>
<h5>b. run concurrent cycle more quick</h5>
<p>Set <code>-XX:ConcGCThreads=N</code> will control the threads to run cycle. the more threads the quick cycle will have, as well as more CPU usage.</p>
<p>If concurrent mode failure doesn’t often take, set less value will save our CPU resouce.</p>
<h4>5. CMS PermGen</h4>
<p>PermGen will not be collect by default when using CMS.</p>
<p>By set <code>-XX:CMSPermGenSweepingEnable</code> and <code>-XX:CMSClassUnloadingEnable</code> will let cms threads to collect PermGen and free class metadata.</p>
<h4>6. Use G1</h4>
<p>If u are using big heap and fragement problem disturb u, G1 is valuable to explore.</p>
</div>
<div class="meta">
<div class="date">
<time datetime="2014-11-23T21:56:35+08:00" pubdate data-updated="true">Nov 23<span>rd</span>, 2014</time></div>
<div class="tags">
<a class='category' href='/blog/categories/jvm/'>jvm</a>
</div>
<div class="comments"><a href="/blog/2014/11/23/tunning-cms-gc/#disqus_thread">Comments</a></div>
</div>
</article>
<nav id="pagenavi">
<div class="center"><a href="/blog/archives">Blog Archives</a></div>
</nav>
</div>
<footer id="footer" class="inner">Copyright © 2015
lysu
</footer>
<script src="/javascripts/slash.js"></script>
<script src="/javascripts/jquery.fancybox.pack.js"></script>
<script type="text/javascript">
(function($){
$('.fancybox').fancybox();
})(jQuery);
</script> <!-- Delete or comment this line to disable Fancybox -->
<script type="text/javascript">
var disqus_shortname = 'lysuzz';
var disqus_script = 'count.js';
(function () {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//go.disqus.com/' + disqus_script;
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
}());
</script>
</body>
</html>