{"id":773,"date":"2014-03-05T08:41:00","date_gmt":"2014-03-05T08:41:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2014\/03\/05\/shrink-my-program-database-pdb-file\/"},"modified":"2019-02-18T18:40:38","modified_gmt":"2019-02-18T18:40:38","slug":"shrink-my-program-database-pdb-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/shrink-my-program-database-pdb-file\/","title":{"rendered":"Shrink my Program Database (PDB) file"},"content":{"rendered":"<h2>Overview<\/h2>\n<p>PDB&#8217;s (Program Database file), <span style=\"color: black;background-color: white\">PDB stands for&nbsp;<strong>Program Database<\/strong>, a&nbsp;<a title=\"Proprietary software\" href=\"http:\/\/en.wikipedia.org\/wiki\/Proprietary_software\"><span style=\"color: #0b0080\">proprietary<\/span><\/a>&nbsp;file format (developed by&nbsp;<a title=\"Microsoft\" href=\"http:\/\/en.wikipedia.org\/wiki\/Microsoft\"><span style=\"color: #0b0080\">Microsoft<\/span><\/a>) for storing debugging information about a program (or, commonly, binaries such as a&nbsp;<a title=\"Dynamic-link library\" href=\"http:\/\/en.wikipedia.org\/wiki\/Dynamic-link_library\"><span style=\"color: #0b0080\">DLL<\/span><\/a>&nbsp;or&nbsp;<a title=\"EXE\" href=\"http:\/\/en.wikipedia.org\/wiki\/EXE\"><span style=\"color: #0b0080\">EXE<\/span><\/a>). PDB files commonly have a .pdb <a title=\"Filename extension\" href=\"http:\/\/en.wikipedia.org\/wiki\/Filename_extension\"><span style=\"color: #0b0080\">extension<\/span><\/a>. A PDB file is typically created from source files during compilation although other variants exist (also created by the linker when \/Z7 is used). It stores a list of all symbols in a module with their addresses and possibly the name of the file and the line on which the symbol was declared. This symbol information is not stored in the module itself, because it takes up a lot of space. <\/span><\/p>\n<p>This blog in particular goes over a few ways to shrink your PDB size, so let&#8217;s get started. For demonstrating the effectiveness of these ways I have used the popular <strong>BingMaps (bingmaps.dll) <\/strong>windows store application.<br \/>&nbsp;<\/p>\n<h2>#1. The \/OPT:REF and \/OPT:ICF effect<\/h2>\n<p><span style=\"color: #424242;font-size: 12px\">Linker has a good view of all the modules that will be linked together, so linker is in a good position to optimize away unused global data and unreferenced functions. The linker however manipulates on a OBJ section level, so if the unreferenced data\/functions are mixed with other data or functions in a section, linker won&#8217;t be able to extract it out and remove it. In order to equip the linker to remove unused global data and functions, <\/span><em style=\"color: #424242;font-size: 12px\">we need to put each global data or function in a separate section, and we call these sections &#8220;<strong>COMDATs<\/strong><\/em><span style=\"color: #424242;font-size: 12px\">&#8220;. (The COMDAT construction is enabled by the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/xsa71f43.aspx\">\/Gy<\/a> and \/Gw compiler flags). COMDATs and usage of these flags <\/span><em style=\"color: #424242;font-size: 12px\">\/OPT: REF and \/OPT: ICF<\/em><span style=\"color: #424242;font-size: 12px\"> enable (here&#8217;s <\/span><a style=\"font-size: 12px\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bxwfs976.aspx\">how<\/a><span style=\"color: #424242;font-size: 12px\"> to do this) linker optimizations. \/OPT:<\/span><em style=\"color: #424242;font-size: 12px\">REF<\/em><span style=\"color: #424242;font-size: 12px\"> eliminates functions and data that are never referenced and \/OPT:<\/span><em style=\"color: #424242;font-size: 12px\">ICF<\/em><span style=\"color: #424242;font-size: 12px\"> performs identical COMDAT folding. <\/span><em style=\"color: #424242;font-size: 12px\">The two together form a strong force and the result is a smaller binary and hence also a smaller PDB.<\/em><\/p>\n<p><span style=\"color: #424242\"><em>Please note, enabling linker optimizations today disables<a title=\"incremental linking\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/4khtbfyf.aspx\"> incremental linking<\/a> however. <\/p>\n<p><\/em><\/span><\/p>\n<h2>#2. The \/d2Zi+ effect<\/h2>\n<p>The <a href=\"mailto:http:\/\/randomascii.wordpress.com\/2013\/09\/11\/debugging-optimized-codenew-in-visual-studio-2012\/\">usage of cryptic but undocumented switch<\/a> is common especially for&nbsp;debugging optimized code. In particular, it provides more debug information for locals and inline behaviour. The side-effect of using this flag for all scenarios however results in PDB size growth. The exact specifics of the size increase are application dependent.<br \/>&nbsp;<\/p>\n<h2>#3. Compress the PDB using \/PDBCompress<\/h2>\n<p>For clean link scenarios \/PDBCOMPRESS instructs the linker to open the target PDB file in a mode that will lead to the operating system compressing the file content automatically as debug records are being written into the PDB file. &nbsp;This will result in a smaller PDB.&nbsp; This switch won&#8217;t have any impact if the operating system&#8217;s file system does not support compression, or the linker is asked to update an existing PDB file to which compression by OS&#8217; file system hasn&#8217;t been applied.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/00\/00\/00\/65\/69\/metablogapi\/6747.030514_1640_ShrinkmyPro1.png\" alt=\"\" \/><\/p>\n<p style=\"margin-left: 108pt\"><span style=\"color: #44546a;font-size: 9pt\"><em>Figure 1: Effect of \/pdbcompress on BingMaps PDB <\/em><\/span><\/p>\n<p>Please note, the impact of this compression can be observed by looking at the &#8216;size on disk&#8217;. If looking in windows explorer, compressed PDB&#8217;s will light up in blue.&nbsp;<br \/>&nbsp;<\/p>\n<h2>#4. Incremental Update to PDB&#8217;s<\/h2>\n<p>During incremental linking, we don&rsquo;t remove unreferenced type records (which is same as in full linking), also for public and global records, we don&rsquo;t remove obsolete ones (which is for throughput purpose)..&nbsp; Over extensive use which is numerous rebuild\/relink iterations the size of the PDB grows. We recommend a clean link (build) when possible for reducing the size of PDB&#8217;s. <\/p>\n<h2>Sum it all up<\/h2>\n<p>To conclude, attached below is the result of enabling the above techniques on the popular BingMaps Windows Store application.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/00\/00\/00\/65\/69\/2047.PDB%20Size%20with%20Various%20Build%20Flags.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/00\/00\/00\/65\/69\/2047.PDB%20Size%20with%20Various%20Build%20Flags.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<div><a href=\"https:\/\/devblogs.microsoft.com\/00\/00\/00\/65\/69\/1641.table-pdbsize.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/00\/00\/00\/65\/69\/1641.table-pdbsize.png\" alt=\"\" border=\"0\" \/><\/a><\/div>\n<div>As you can see there are some clear wins with the methodologies described. Please note the vanilla build setting here is an optimized (<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/8f8h5cxt.aspx\">\/O2<\/a>) build with <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/958x11bc.aspx\">\/Zi<\/a> (Program Database) enabled.<\/div>\n<p>Reach out to <a href=\"mailto:aasthan@microsoft.com\">us<\/a> if you have questions, concerns or feature requests w.r.t the linker and PDB&#8217;s. <\/p>\n<p>Additionally, if you would like us to blog about some other compiler technology or compiler optimization please let us know we are always interested in learning from your feedback.<span style=\"color: #424242;font-family: Segoe UI;font-size: 9pt;background-color: white\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview PDB&#8217;s (Program Database file), PDB stands for&nbsp;Program Database, a&nbsp;proprietary&nbsp;file format (developed by&nbsp;Microsoft) for storing debugging information about a program (or, commonly, binaries such as a&nbsp;DLL&nbsp;or&nbsp;EXE). PDB files commonly have a .pdb extension. A PDB file is typically created from source files during compilation although other variants exist (also created by the linker when \/Z7 [&hellip;]<\/p>\n","protected":false},"author":265,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-773","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus"],"acf":[],"blog_post_summary":"<p>Overview PDB&#8217;s (Program Database file), PDB stands for&nbsp;Program Database, a&nbsp;proprietary&nbsp;file format (developed by&nbsp;Microsoft) for storing debugging information about a program (or, commonly, binaries such as a&nbsp;DLL&nbsp;or&nbsp;EXE). PDB files commonly have a .pdb extension. A PDB file is typically created from source files during compilation although other variants exist (also created by the linker when \/Z7 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/773","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/265"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=773"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/773\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}