Some times ago there was a recommendation in the Google Quality Guide to use 100 as a limit for the number of links on a given webpage, have no idea if they still suggest this or it has been changed since that time.
Considering that you have 4000 pages I think you should make display only the product category structure in the static (html) sitemap which would ease the navigation for the visitors.
Regarding the xml sitemap I think you may want to read
this article about creating xml sitemap without using fopen.