Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7918605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:33:56+00:00 2026-06-03T15:33:56+00:00

As library docs say CString created with newCString must be freed with free function.

  • 0

As library docs say CString created with newCString must be freed with free function. I have been expecting that when CString is created it would take some memory and when it is released with free memory usage would go down, but it didn’t! Here is example code:

module Main where

import Foreign
import Foreign.C.String
import System.IO

wait = do
  putStr "Press enter" >> hFlush stdout
  _ <- getLine
  return ()

main = do
  let s = concat $ replicate 1000000 ['0'..'9']
  cs <- newCString s
  cs `seq` wait   -- (1)

  free cs
  wait   -- (2)

When program stopped at (1), htop program showed that memory usage is somewhere around 410M – this is OK. I press enter and the program stops at line (2), but memory usage is still 410M despite cs has been freed!

How is this possible? Similar program written in C behaves as it should. What am I missing here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:33:57+00:00Added an answer on June 3, 2026 at 3:33 pm

    The issue is that free just indicates to the garbage collector that it can now collect the string. That doesn’t actually force the garbage collector to run though — it just indicates that the CString is now garbage. It is still up to the GC to decide when to run, based on heap pressure heuristics.

    You can force a major collection by calling performGC straight after the call to free, which immediately reduces the memory to 5M or so.

    E.g. this program:

    import Foreign
    import Foreign.C.String
    import System.IO
    import System.Mem
    
    wait = do
      putStr "Press enter" >> hFlush stdout
      _ <- getLine
      return ()
    
    main = do
      let s = concat $ replicate 1000000 ['0'..'9']
      cs <- newCString s
      cs `seq` wait   -- (1)
    
      free cs
      performGC
      wait   -- (2)
    

    Behaves as expected, with the following memory profile – the first red dot is the call to performGC, immediately deallocating the string. The program then hovers around 5M until terminated.

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a compiled Python library and API docs that I would like to
I have looked at the documentation here: http://docs.python.org/dev/library/xml.etree.elementtree.html#xml.etree.ElementTree.SubElement The parent and tag argument seems
Microsoft's docs say: Specifies that the corresponding point in lpPoints is a control point
I would like a function that can generate a pseudo-random sequence of values, but
The GMP docs say that static linking may provide a small performance improvement. I
I am trying to create a DLL that uses the CURL library. The docs
I have read the Apple docs - http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/TabBarControllers/TabBarControllers.html#//apple_ref/doc/uid/TP40007457-CH102-SW1 about creating TabBar programmatically. I want
Reference - http://docs.python.org/library/unittest.html#assert-methods assertEqual(a, b) # checks that a == b assertIs(a, b) #
http://docs.python.org/library/imaplib.html states that: exception IMAP4.error Exception raised on any errors. The reason for the
The Goal Say I have a collection/folder of documents in Google Docs/Drive, and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.