Thread: why lcc made programs excessively use memory?

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    11

    Question why lcc made programs excessively use memory?

    Hello!

    I made a chat server using lcc-win32, a compiler that I'm happy with, but yesterday I discovered a very unusual thing. A 21kB large executable server takes up ~1.8 MB of RAM as reported by Task Manager ! Now, I may be a control freak about memory, but this is just not acceptable. My application almost has no static data, basically binds a server socket, awaits incoming chat/connections through WM_USER+ messages(async select) , where I make CreateThread() to create thread which reads off data, redistribute it to other clients and ends. I make careful use of my memory, so there is NO memleaks . How can I find out what the hell is going on? Does anyone have an idea? Thanks in advance !
    Last edited by xlines; 05-11-2008 at 10:35 AM. Reason: typos

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    CreateThread creates a stack which by default is 1 MB. Perhaps there is a big culprit.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Quote Originally Posted by Elysia View Post
    CreateThread creates a stack which by default is 1 MB. Perhaps there is a big culprit.
    I was just reading documentation on pthread calls and I remembered that I did not explicitly set thread stack, maybe that's it! But, it is a stable 1,8MB usage *even* no thread is created - before incoming connection, that is. I'll look at that anyway, thanks for the hint!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, then there's runtime components.
    Why don't you step through the program using a debugger and monitor the memory use as you step through?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    Quote Originally Posted by xlines View Post
    I was just reading documentation on pthread calls and I remembered that I did not explicitly set thread stack, maybe that's it! But, it is a stable 1,8MB usage *even* no thread is created - before incoming connection, that is. I'll look at that anyway, thanks for the hint!
    Perhaps the main thread's stack is 1MB?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Which 'size' of memory are you measuring, there are several with very different meanings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Just because your binary is 21 kilobytes doesn't mean there is only 21 kilobytes of code. Unless you never call any library functions, you are at the mercy of what the libraries do. And modern libraries aren't terribly concerned with using as little memory as possible.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Doodle77 View Post
    Perhaps the main thread's stack is 1MB?
    Yes, it certainly would be unless you go out of your way to assign a smaller stack-size. Also, if your application is 21KB, then it's most likely importing quite a bit of functionality from DLL's, which in themselves will take up some memory.

    It's also worth noting the point that Salem is indicating - there's two sizes of memory:
    1. The virtual size, which is the "maximum of memory this application could use at present" - but it's not necessarily using all of that, because some of the memory is just "reserved" so that there's a known place to populate with physical memory should it be needed
    2. The actual physical memory used. This is how much memory is ACTUALLY really taken up in the RAM memory in your machine.

    One example of this is the stack(s) of the thread(s) in the system. The default size is 1MB. But most applications use only a few kilobytes of that stack, so there's no point in the OS actually giving physical for the whole 1MB. It will do that as the memory is needed. Likewise, DLL's that are only partly used will only be partly loaded - but the memory space for the entire DLL is being reserved at load-time, because it would be horribly complicated if we for some reason started to use some part of that memory for other purposes and then needed the rest of the DLL to be loaded.

    --
    Mats

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Well,thank to you all !!! I've learn about things that I didn't pay much attention earlier.

    Quote Originally Posted by matsp View Post
    Yes, it certainly would be unless you go out of your way to assign a smaller stack-size. Also, if your application is 21KB, then it's most likely importing quite a bit of functionality from DLL's, which in themselves will take up some memory.
    Indeed ! Application start at 1,6MB ( stack and libraries, I think ) and, as more APIs (winsock, commctrl ) are called, reserved memory grows another 0,2MB , due to library needs,I guess.

    Quote Originally Posted by matsp View Post
    Likewise, DLL's that are only partly used will only be partly loaded - but the memory space for the entire DLL is being reserved at load-time, because it would be horribly complicated if we for some reason started to use some part of that memory for other purposes and then needed the rest of the DLL to be loaded.
    Yes, it would be hard to convince unsuspecting program to update it's pointers and not to write all over DLL body =)

    thx!

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Main thread + runtime libraries.

    Main thread stack = 1MB
    ws2_32.dll = 80k
    kernel32.dll = 967k


    There is your 1.8MB right there

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  2. memory frames in programs
    By cs32 in forum C Programming
    Replies: 15
    Last Post: 01-11-2008, 04:06 AM
  3. Memory handler
    By Dr. Bebop in forum C Programming
    Replies: 7
    Last Post: 09-15-2002, 04:14 PM
  4. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM
  5. Replies: 1
    Last Post: 05-13-2002, 06:41 PM